[K Relay][Source] Sniffing all packets and logging them
Posts 1–1 of 1 · Page 1 of 1
[K Relay][Source] Sniffing all packets and logging them
Hi,
I wanted to share this code, because it have been using it a lot while trying things out in K Relay.
All this does is hook all packets and log them nicely in file (in the same folder as the K Relay EXE).
According to which packets you want to see you will have to change up the switch statement as explained in the comments.
Maybe somebody will find this useful.
Code:
using Lib_K_Relay;
using Lib_K_Relay.Interface;
using Lib_K_Relay.Networking;
using Lib_K_Relay.Networking.Packets;
using Lib_K_Relay.Networking.Packets.Client;
using Lib_K_Relay.Networking.Packets.DataObjects;
using Lib_K_Relay.Networking.Packets.Server;
using Lib_K_Relay.Utilities;
using System;
namespace PlanSnif
{
public class PlanSnif:IPlugin
{
System****.StreamWriter file;
public string GetAuthor()
{ return "Plann"; }
public string GetName()
{ return "PlanSnif"; }
public string GetDescription()
{ return "This plugin sniffs and logs all the packets send and received."; }
public string[] GetCommands()
{ return new string[0]; }
public void Initialize(Proxy proxy)
{
//Open the log file
file = new System****.StreamWriter("snif.txt");
/*
Place a hook on all PacketTypes.
You can't hook on UNKNOWN so we filter this out right here.
All other packets are handled later, even if we want to ignore them.
*/
foreach(var i in Enum.GetValues(typeof(PacketType))) {
PacketType p = (PacketType)i;
if (!p.Equals(PacketType.UNKNOWN)) {
proxy.HookPacket(p, HandlePacket);
}
}
}
private void HandlePacket(Client client, Packet packet)
{
//Handle the packets in a switch case for ease of use.
switch (packet.Type)
{
case PacketType.TEXT:
TextPacket t = (TextPacket)packet;
Log(packet.Type + " " + t.Name + ": " + t.Text);
break;
case PacketType.UPDATE:
break;
case PacketType.UPDATEACK:
break;
default:
Log((packet.Type.ToString()));
break;
}
}
private void Log(string line)
{
//Get a nice timestamp
string timestamp = DateTime.Now.ToString("HH:mm:ss");
//Write everything to the file
file.WriteLine("[" + timestamp + "] " + line);
}
}
}