Skip to content

Instantly share code, notes, and snippets.

@hiroto-takatoshi
Created March 9, 2018 00:01
Show Gist options
  • Save hiroto-takatoshi/72222501b673015f58483fe3ce6d031c to your computer and use it in GitHub Desktop.
Save hiroto-takatoshi/72222501b673015f58483fe3ce6d031c to your computer and use it in GitHub Desktop.
cpp ver
#include <tins/tins.h>
#include <iostream>
#include <vector>
#include "tins/network_interface.h"
using namespace std;
using namespace Tins;
size_t counter(0);
IPv4Address pubg_client;
IPv4Address pubg_server;
bool bFoundConnection = false;
bool bLogfull = false;
bool bLogClient = true;
bool bLogServer = false;
bool callback(const PDU& pdu)
{
counter++;
UDP udp = pdu.rfind_pdu<UDP>();
IP ip = pdu.rfind_pdu<IP>();
auto packet = pdu.rfind_pdu<RawPDU>();
auto pckPayload = packet.payload();
if (bLogfull)
{
std::cout << dec << counter << " "<< ip.src_addr() << ":" << udp.sport() << " -> " << ip.dst_addr() << ":" << udp.dport() << " len:" << pckPayload.size() << std::endl;
}
if(!bFoundConnection)
{
// UE4 connection initialization packet length
if (pckPayload.size() == 25)
{
// UE4 connection initialization packet layout
if (pckPayload[0] == 0x01 && pckPayload[24] == 0x04)
{
pubg_client = ip.src_addr();
pubg_server = ip.dst_addr();
bFoundConnection = true;
}
}
}
if(bFoundConnection)
{
if (bLogClient && ip.src_addr() == pubg_client)
{
cout << dec << "C [" << std::setw(4) << std::setfill('0') << pckPayload.size() << "] -> ";
for (std::vector<uint8_t>::iterator it = pckPayload.begin(); it != pckPayload.end(); ++it)
{
std::cout << std::hex << std::setw(2) << std::setfill('0') << static_cast<unsigned int>(*it) << " ";
}
cout << std::endl;
}
if (bLogServer && ip.src_addr() == pubg_server)
{
cout << dec << "S [" << std::setw(4) << std::setfill('0') << pckPayload.size() << "] -> ";
for (std::vector<uint8_t>::iterator it = pckPayload.begin(); it != pckPayload.end(); ++it)
{
std::cout << std::hex << std::setw(2) << std::setfill('0') << static_cast<unsigned int>(*it) << " ";
}
cout << std::endl;
}
}
Sleep(100);
return true;
}
int main(int argc, char* argv[])
{
FileSniffer sniffer("d:\\cap2.pcap");
sniffer.sniff_loop(callback);
std::cout << "There are " << counter << " packets in the pcap file\n";
/*
if(argc != 2) {
vector<NetworkInterface> interfaces = NetworkInterface::all();
for (const NetworkInterface& iface : interfaces)
{
cout << "Interface name: " << iface.name();
wcout << " (" << iface.friendly_name() << ")" << endl;
}
NetworkInterface iface = NetworkInterface::default_interface();
cout << "Default interface: " << iface.name() << " (" << iface.addresses().ip_addr << ")" << endl;
cout << "Usage: " <<* argv << " <interface>" << endl;
return 1;
}
SnifferConfiguration config;
config.set_promisc_mode(true);
config.set_filter("udp portrange 7000-7999");
Sniffer sniffer(argv[1], config);
sniffer.sniff_loop(callback);
*/
}
@hiroto-takatoshi
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment