Beautifier + Checkpoint 28/2/2012
This commit is contained in:
parent
990e0e6bac
commit
4ebfe88f1b
9 changed files with 401 additions and 419 deletions
136
Cigarette.cpp
136
Cigarette.cpp
|
@ -28,77 +28,77 @@ using namespace boost;
|
|||
using namespace boost::program_options;
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
options_description desc("Cigarette - Network Packet Parser");
|
||||
desc.add_options()
|
||||
("help", "prints this")
|
||||
("verbose", "enable verbose mode") // TODO
|
||||
;
|
||||
options_description desc("Cigarette - Network Packet Parser");
|
||||
desc.add_options()
|
||||
("help", "prints this")
|
||||
("verbose", "enable verbose mode") // TODO
|
||||
;
|
||||
|
||||
variables_map vm;
|
||||
store(parse_command_line(argc, argv, desc), vm);
|
||||
notify(vm);
|
||||
variables_map vm;
|
||||
store(parse_command_line(argc, argv, desc), vm);
|
||||
notify(vm);
|
||||
|
||||
if(vm.count("help"))
|
||||
{
|
||||
cout<<desc<<"\n";
|
||||
return 1;
|
||||
}
|
||||
if (vm.count("help"))
|
||||
{
|
||||
cout<<desc<<"\n";
|
||||
return 1;
|
||||
}
|
||||
|
||||
while(1)
|
||||
{
|
||||
try
|
||||
{
|
||||
string r_packet;
|
||||
getline(cin,r_packet);
|
||||
if(cin.eof()) break;
|
||||
while (1)
|
||||
{
|
||||
try
|
||||
{
|
||||
string r_packet;
|
||||
getline(cin,r_packet);
|
||||
if (cin.eof()) break;
|
||||
|
||||
std::vector< std::string > line;
|
||||
boost::algorithm::split(line, r_packet, boost::algorithm::is_any_of("!"));
|
||||
std::vector< std::string > line;
|
||||
boost::algorithm::split(line, r_packet, boost::algorithm::is_any_of("!"));
|
||||
|
||||
packet* pkg = packet::factory(lexical_cast<int>(line[0]), lexical_cast<int>(line[1]), line[2]);
|
||||
|
||||
cout << "[" << std::dec << pkg->getEpoch() << " "<< pkg->getMillis() << "] Size: " << pkg->getLength() << " byte" << endl;
|
||||
cout << " From " << pkg->getSenderMac().print() << " to "<< pkg->getTargetMac().print() << endl;
|
||||
cout << " EtherType: 0x" << std::hex << pkg->getEtherType() << " ("<< ether_type_decode(pkg->getEtherType()) << ")" << endl;
|
||||
cout << endl;
|
||||
|
||||
if(pkg->isArp())
|
||||
{
|
||||
|
||||
if(((ARPpacket*)pkg)->getOpCode() == 1)
|
||||
{
|
||||
cout << " Who has " << ((ARPpacket*)pkg)->getTargetIp().to_string() << " ? Tell "<< ((ARPpacket*)pkg)->getSenderIp().to_string() << endl;
|
||||
cout << endl;
|
||||
|
||||
} else {
|
||||
|
||||
cout << " " << ((ARPpacket*)pkg)->getSenderIp().to_string() << " is at "<< pkg->getSenderMac().print() << endl;
|
||||
cout << endl;
|
||||
|
||||
}
|
||||
|
||||
} else if(pkg->isIPv4())
|
||||
{
|
||||
|
||||
|
||||
|
||||
} else {
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
catch(packet::Overflow)
|
||||
{
|
||||
std::cerr<<"Overflow! :-P"<<std::endl;
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
catch(packet::HeaderFault)
|
||||
{
|
||||
std::cerr<<"HeaderFault! :-P"<<std::endl;
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
}
|
||||
return EXIT_SUCCESS;
|
||||
packet* pkg = packet::factory(lexical_cast<int>(line[0]), lexical_cast<int>(line[1]), line[2]);
|
||||
|
||||
cout << "[" << std::dec << pkg->getEpoch() << " "<< pkg->getMillis() << "] Size: " << pkg->getLength() << " byte" << endl;
|
||||
cout << " From " << pkg->getSenderMac().print() << " to "<< pkg->getTargetMac().print() << endl;
|
||||
cout << " EtherType: 0x" << std::hex << pkg->getEtherType() << " ("<< ether_type_decode(pkg->getEtherType()) << ")" << endl;
|
||||
cout << endl;
|
||||
|
||||
if (pkg->isArp())
|
||||
{
|
||||
|
||||
if (((ARPpacket*)pkg)->getOpCode() == 1)
|
||||
{
|
||||
cout << " Who has " << ((ARPpacket*)pkg)->getTargetIp().to_string() << " ? Tell "<< ((ARPpacket*)pkg)->getSenderIp().to_string() << endl;
|
||||
cout << endl;
|
||||
|
||||
} else {
|
||||
|
||||
cout << " " << ((ARPpacket*)pkg)->getSenderIp().to_string() << " is at "<< pkg->getSenderMac().print() << endl;
|
||||
cout << endl;
|
||||
|
||||
}
|
||||
|
||||
} else if (pkg->isIPv4())
|
||||
{
|
||||
|
||||
|
||||
|
||||
} else {
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
catch (packet::Overflow)
|
||||
{
|
||||
std::cerr<<"Overflow! :-P"<<std::endl;
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
catch (packet::HeaderFault)
|
||||
{
|
||||
std::cerr<<"HeaderFault! :-P"<<std::endl;
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
}
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
|
2
README
2
README
|
@ -21,6 +21,6 @@ Legge output di Riddle e restituisce a video informazioni in tempo reale sui pac
|
|||
|
||||
** TODO:
|
||||
|
||||
- Filtro pacchetti avanzato (più potente del filtro pcap) con anche filtro livello applicativo.
|
||||
- Filtro pacchetti avanzato (piu' potente del filtro pcap) con anche filtro livello applicativo.
|
||||
- Follower TCP con ricostruzione file. (Hendirx)
|
||||
- Scanner passivo (ARP...)
|
||||
|
|
135
Riddle.cpp
135
Riddle.cpp
|
@ -22,89 +22,90 @@ using namespace std;
|
|||
using namespace boost::program_options;
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
options_description desc("Riddle - Network Sniffer");
|
||||
desc.add_options()
|
||||
("help", "prints this")
|
||||
("dump", "enable dump mode")
|
||||
("iface", value<string>(), "interface to sniff from")
|
||||
("limit", value<int>(), "set max number of packet")
|
||||
("filter", value<string>(), "use to filter packet with bpf")
|
||||
;
|
||||
options_description desc("Riddle - Network Sniffer");
|
||||
desc.add_options()
|
||||
("help", "prints this")
|
||||
("dump", "enable dump mode")
|
||||
("iface", value<string>(), "interface to sniff from")
|
||||
("limit", value<int>(), "set max number of packet")
|
||||
("filter", value<string>(), "use to filter packet with bpf")
|
||||
;
|
||||
|
||||
variables_map vm;
|
||||
store(parse_command_line(argc, argv, desc), vm);
|
||||
notify(vm);
|
||||
variables_map vm;
|
||||
store(parse_command_line(argc, argv, desc), vm);
|
||||
notify(vm);
|
||||
|
||||
if(vm.count("help"))
|
||||
{
|
||||
cout<<desc<<"\n";
|
||||
return 1;
|
||||
}
|
||||
if (vm.count("help"))
|
||||
{
|
||||
cout<<desc<<"\n";
|
||||
return 1;
|
||||
}
|
||||
|
||||
string pcap_device;
|
||||
char error_buffer[PCAP_ERRBUF_SIZE];
|
||||
string pcap_device;
|
||||
char error_buffer[PCAP_ERRBUF_SIZE];
|
||||
|
||||
if(vm.count("iface"))
|
||||
{
|
||||
pcap_device=vm["iface"].as<string>();
|
||||
} else {
|
||||
// Cerca e restituisce interfaccia
|
||||
char *dev=pcap_lookupdev(error_buffer);
|
||||
if(dev!=NULL) pcap_device = dev;
|
||||
else pcap_fatal("pcap_lookupdev", error_buffer);
|
||||
}
|
||||
if (vm.count("iface"))
|
||||
{
|
||||
pcap_device=vm["iface"].as<string>();
|
||||
} else {
|
||||
// Cerca e restituisce interfaccia
|
||||
char *dev=pcap_lookupdev(error_buffer);
|
||||
if (dev!=NULL) pcap_device = dev;
|
||||
else pcap_fatal("pcap_lookupdev", error_buffer);
|
||||
}
|
||||
|
||||
pcap_t *pcap_handle;
|
||||
pcap_t *pcap_handle;
|
||||
|
||||
// Apre il device in mod promiscua
|
||||
pcap_handle = pcap_open_live(pcap_device.c_str(), 4096, 1, 0, error_buffer);
|
||||
if(pcap_handle == NULL){
|
||||
pcap_fatal("pcap_open_live", error_buffer);
|
||||
}
|
||||
// Apre il device in mod promiscua
|
||||
pcap_handle = pcap_open_live(pcap_device.c_str(), 4096, 1, 0, error_buffer);
|
||||
if (pcap_handle == NULL) {
|
||||
pcap_fatal("pcap_open_live", error_buffer);
|
||||
}
|
||||
|
||||
cerr<<"Sniffing on device "<<pcap_device<<endl;
|
||||
cerr<<"Sniffing on device "<<pcap_device<<endl;
|
||||
|
||||
if(vm.count("filter"))
|
||||
{
|
||||
string filter = vm["filter"].as<string>();
|
||||
struct bpf_program fp;
|
||||
bpf_u_int32 net;
|
||||
if (vm.count("filter"))
|
||||
{
|
||||
string filter = vm["filter"].as<string>();
|
||||
struct bpf_program fp;
|
||||
bpf_u_int32 net;
|
||||
|
||||
cerr<<"Filtering with '"<<filter<<"'"<<endl;
|
||||
cerr<<"Filtering with '"<<filter<<"'"<<endl;
|
||||
|
||||
if (pcap_compile(pcap_handle, &fp, filter.c_str(), 0, net) == -1)
|
||||
{
|
||||
cerr<< "Couldn't parse filter '"<<filter<<"': "<<pcap_geterr(pcap_handle)<<endl;
|
||||
return(2);
|
||||
}
|
||||
if (pcap_compile(pcap_handle, &fp, filter.c_str(), 0, net) == -1)
|
||||
{
|
||||
cerr<< "Couldn't parse filter '"<<filter<<"': "<<pcap_geterr(pcap_handle)<<endl;
|
||||
return(2);
|
||||
}
|
||||
|
||||
if (pcap_setfilter(pcap_handle, &fp) == -1) {
|
||||
cerr<< "Couldn't install filter '"<<filter<<"': "<<pcap_geterr(pcap_handle)<<endl;
|
||||
return(2);
|
||||
}
|
||||
}
|
||||
if (pcap_setfilter(pcap_handle, &fp) == -1) {
|
||||
cerr<< "Couldn't install filter '"<<filter<<"': "<<pcap_geterr(pcap_handle)<<endl;
|
||||
return(2);
|
||||
}
|
||||
}
|
||||
|
||||
int maxpacket = numeric_limits<int>::max();
|
||||
int maxpacket = numeric_limits<int>::max();
|
||||
|
||||
if(vm.count("limit"))
|
||||
{
|
||||
maxpacket=vm["limit"].as<int>();
|
||||
}
|
||||
if (vm.count("limit"))
|
||||
{
|
||||
maxpacket=vm["limit"].as<int>();
|
||||
}
|
||||
|
||||
void (*dumper)(const unsigned char*,struct pcap_pkthdr);
|
||||
if(vm.count("dump")) dumper=hexDump; else dumper=rawDump;
|
||||
void (*dumper)(const unsigned char*,struct pcap_pkthdr);
|
||||
if (vm.count("dump")) dumper=hexDump;
|
||||
else dumper=rawDump;
|
||||
|
||||
const u_char *packet;
|
||||
pcap_pkthdr header;
|
||||
const u_char *packet;
|
||||
pcap_pkthdr header;
|
||||
|
||||
for(;maxpacket > 0;)
|
||||
{
|
||||
packet = pcap_next(pcap_handle, &header);
|
||||
dumper(packet, header);
|
||||
if(maxpacket!=numeric_limits<int>::max()) maxpacket--;
|
||||
}
|
||||
for (;maxpacket > 0;)
|
||||
{
|
||||
packet = pcap_next(pcap_handle, &header);
|
||||
dumper(packet, header);
|
||||
if (maxpacket!=numeric_limits<int>::max()) maxpacket--;
|
||||
}
|
||||
|
||||
pcap_close(pcap_handle);
|
||||
pcap_close(pcap_handle);
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
|
|
@ -16,47 +16,37 @@
|
|||
|
||||
mac_address::mac_address(const std::string& rawData)
|
||||
{
|
||||
int l = 0;
|
||||
std::string temp;
|
||||
temp.reserve(2);
|
||||
int l = 0;
|
||||
std::string temp;
|
||||
temp.reserve(2);
|
||||
|
||||
for(int i=0;i<=11;i++)
|
||||
{
|
||||
temp += rawData[i];
|
||||
if(i%2 != 0)
|
||||
{
|
||||
std::stringstream convert(temp);
|
||||
convert>>std::hex>>byte[l];
|
||||
l++;
|
||||
temp = "";
|
||||
}
|
||||
}
|
||||
|
||||
return;
|
||||
for (int i=0;i<=11;i++)
|
||||
{
|
||||
temp += rawData[i];
|
||||
if (i%2 != 0)
|
||||
{
|
||||
std::stringstream convert(temp);
|
||||
convert>>std::hex>>byte[l];
|
||||
l++;
|
||||
temp = "";
|
||||
}
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
std::string mac_address::print()
|
||||
{
|
||||
std::string stamp;
|
||||
std::stringstream temp;
|
||||
std::string stamp;
|
||||
std::stringstream temp;
|
||||
|
||||
for(int i=0;i<=5;i++)
|
||||
{
|
||||
temp<<std::setfill('0')<<std::setw(2)<<std::hex<<(int) byte[i];
|
||||
stamp += temp.str();
|
||||
temp.str("");
|
||||
if(i != 5) stamp += ':';
|
||||
}
|
||||
for (int i=0;i<=5;i++)
|
||||
{
|
||||
temp<<std::setfill('0')<<std::setw(2)<<std::hex<<(int) byte[i];
|
||||
stamp += temp.str();
|
||||
temp.str("");
|
||||
if (i != 5) stamp += ':';
|
||||
}
|
||||
|
||||
return stamp;
|
||||
}
|
||||
|
||||
void ipv6_address::set(std::string packet, int start)
|
||||
{
|
||||
//TODO
|
||||
}
|
||||
|
||||
std::string ipv6_address::print()
|
||||
{
|
||||
//TODO
|
||||
return stamp;
|
||||
}
|
||||
|
|
37
libAddress.h
37
libAddress.h
|
@ -18,28 +18,19 @@
|
|||
/* Class for managing MAC address */
|
||||
class mac_address
|
||||
{
|
||||
public:
|
||||
|
||||
/* Costruttore: riceve indirizzo in stringa codifica esadecimale. */
|
||||
mac_address() { std::memset(byte,0,6); }
|
||||
mac_address(const std::string&);
|
||||
|
||||
/* Restituisce stringa stampabile a schermo. */
|
||||
std::string print();
|
||||
|
||||
private:
|
||||
short int byte[6];
|
||||
public:
|
||||
|
||||
/* Costruttore: riceve indirizzo in stringa codifica esadecimale. */
|
||||
mac_address() {
|
||||
std::memset(byte,0,6);
|
||||
}
|
||||
mac_address(const std::string&);
|
||||
|
||||
/* Restituisce stringa stampabile a schermo. */
|
||||
std::string print();
|
||||
|
||||
private:
|
||||
short int byte[6];
|
||||
};
|
||||
|
||||
/* Class for managing IPv6 address */
|
||||
class ipv6_address
|
||||
{
|
||||
public:
|
||||
void set(std::string, int);
|
||||
std::string print();
|
||||
|
||||
private:
|
||||
short int byte[8];
|
||||
};
|
||||
|
||||
#endif //LIBADDRESS_H
|
||||
#endif //LIBADDRESS_H
|
||||
|
|
|
@ -16,38 +16,38 @@
|
|||
|
||||
std::string ether_type_decode(int start)
|
||||
{
|
||||
// Maggiore o uguale di 1536(0x0600) per Ethernet v2, minore per versione
|
||||
// Maggiore o uguale di 1536(0x0600) per Ethernet v2, minore per versione
|
||||
|
||||
if(start >= ETHER_V2_CODE)
|
||||
{
|
||||
switch(start)
|
||||
{
|
||||
case (ETHER_TYPE_IPV4):
|
||||
return "IPv4";
|
||||
case (ETHER_TYPE_ARP):
|
||||
return "ARP";
|
||||
case (ETHER_TYPE_IPV6):
|
||||
return "IPv6";
|
||||
case (ETHER_TYPE_PPP):
|
||||
return "PPP";
|
||||
case (ETHER_TYPE_IEEE802):
|
||||
return "IEEE 802.1Q";
|
||||
default:
|
||||
return "UNDEFINED";
|
||||
}
|
||||
}
|
||||
else return "Ethernet IEEE 802.3";
|
||||
if (start >= ETHER_V2_CODE)
|
||||
{
|
||||
switch (start)
|
||||
{
|
||||
case (ETHER_TYPE_IPV4):
|
||||
return "IPv4";
|
||||
case (ETHER_TYPE_ARP):
|
||||
return "ARP";
|
||||
case (ETHER_TYPE_IPV6):
|
||||
return "IPv6";
|
||||
case (ETHER_TYPE_PPP):
|
||||
return "PPP";
|
||||
case (ETHER_TYPE_IEEE802):
|
||||
return "IEEE 802.1Q";
|
||||
default:
|
||||
return "UNDEFINED";
|
||||
}
|
||||
}
|
||||
else return "Ethernet IEEE 802.3";
|
||||
}
|
||||
|
||||
std::string ipv4_type_decode(int start)
|
||||
{
|
||||
switch(start)
|
||||
{
|
||||
case (IPV4_TYPE_TCP):
|
||||
return "TCP";
|
||||
case (IPV4_TYPE_UDP):
|
||||
return "UDP";
|
||||
default:
|
||||
return "UNDEFINED";
|
||||
}
|
||||
switch (start)
|
||||
{
|
||||
case (IPV4_TYPE_TCP):
|
||||
return "TCP";
|
||||
case (IPV4_TYPE_UDP):
|
||||
return "UDP";
|
||||
default:
|
||||
return "UNDEFINED";
|
||||
}
|
||||
}
|
||||
|
|
250
libPacket.cpp
250
libPacket.cpp
|
@ -27,145 +27,145 @@ using namespace boost;
|
|||
|
||||
packet* packet::factory(int timeEpoch_i, int timeMillis_i, std::string rawData_i)
|
||||
{
|
||||
|
||||
int protocol_type;
|
||||
|
||||
std::string temp;
|
||||
temp.reserve(4);
|
||||
|
||||
for(int i = 24; i <= 27; i++)
|
||||
{
|
||||
temp += rawData_i[i];
|
||||
}
|
||||
std::stringstream convert ( temp );
|
||||
convert>>std::hex>>protocol_type;
|
||||
|
||||
packet *p;
|
||||
if(protocol_type == ETHER_TYPE_ARP)
|
||||
{
|
||||
|
||||
p = new ARPpacket(timeEpoch_i, timeMillis_i, rawData_i);
|
||||
|
||||
} else if(protocol_type == ETHER_TYPE_IPV4)
|
||||
{
|
||||
|
||||
p = IPv4packet::factory(timeEpoch_i, timeMillis_i, rawData_i);
|
||||
|
||||
} else {
|
||||
|
||||
p = new UnknownPacket(timeEpoch_i, timeMillis_i, rawData_i);
|
||||
|
||||
}
|
||||
|
||||
return p;
|
||||
|
||||
int protocol_type;
|
||||
|
||||
std::string temp;
|
||||
temp.reserve(4);
|
||||
|
||||
for (int i = 24; i <= 27; i++)
|
||||
{
|
||||
temp += rawData_i[i];
|
||||
}
|
||||
std::stringstream convert ( temp );
|
||||
convert>>std::hex>>protocol_type;
|
||||
|
||||
packet *p;
|
||||
if (protocol_type == ETHER_TYPE_ARP)
|
||||
{
|
||||
|
||||
p = new ARPpacket(timeEpoch_i, timeMillis_i, rawData_i);
|
||||
|
||||
} else if (protocol_type == ETHER_TYPE_IPV4)
|
||||
{
|
||||
|
||||
p = IPv4packet::factory(timeEpoch_i, timeMillis_i, rawData_i);
|
||||
|
||||
} else {
|
||||
|
||||
p = new UnknownPacket(timeEpoch_i, timeMillis_i, rawData_i);
|
||||
|
||||
}
|
||||
|
||||
return p;
|
||||
}
|
||||
|
||||
int packet::getLength()
|
||||
{
|
||||
return pkgLength;
|
||||
return pkgLength;
|
||||
}
|
||||
|
||||
long int packet::getEpoch()
|
||||
{
|
||||
return timeEpoch;
|
||||
return timeEpoch;
|
||||
}
|
||||
|
||||
int packet::getMillis()
|
||||
{
|
||||
return timeMillis;
|
||||
return timeMillis;
|
||||
}
|
||||
|
||||
std::string packet::getHexString(int string_cursor, int read_byte)
|
||||
{
|
||||
std::string temp;
|
||||
temp.reserve(read_byte * 2);
|
||||
|
||||
if(string_cursor + read_byte > this->getLength()) throw Overflow();
|
||||
|
||||
for(int i = string_cursor * 2; i < (string_cursor * 2) + (read_byte * 2); i++)
|
||||
{
|
||||
temp += rawData[i];
|
||||
}
|
||||
|
||||
return temp;
|
||||
std::string temp;
|
||||
temp.reserve(read_byte * 2);
|
||||
|
||||
if (string_cursor + read_byte > this->getLength()) throw Overflow();
|
||||
|
||||
for (int i = string_cursor * 2; i < (string_cursor * 2) + (read_byte * 2); i++)
|
||||
{
|
||||
temp += rawData[i];
|
||||
}
|
||||
|
||||
return temp;
|
||||
}
|
||||
|
||||
std::string packet::decodeIPaddress(int string_cursor)
|
||||
{
|
||||
std::string temp;
|
||||
std::string stamp;
|
||||
temp.reserve(2);
|
||||
std::string temp;
|
||||
std::string stamp;
|
||||
temp.reserve(2);
|
||||
|
||||
for(int i=0;i<=7;i++)
|
||||
{
|
||||
temp += rawData[(string_cursor*2)+i];
|
||||
if(i%2 != 0)
|
||||
{
|
||||
std::stringstream convert(temp);
|
||||
int a;
|
||||
convert>>std::hex>>a;
|
||||
stamp += lexical_cast<std::string>(a);
|
||||
if(i != 7) stamp += ".";
|
||||
temp = "";
|
||||
}
|
||||
}
|
||||
return stamp;
|
||||
for (int i=0;i<=7;i++)
|
||||
{
|
||||
temp += rawData[(string_cursor*2)+i];
|
||||
if (i%2 != 0)
|
||||
{
|
||||
std::stringstream convert(temp);
|
||||
int a;
|
||||
convert>>std::hex>>a;
|
||||
stamp += lexical_cast<std::string>(a);
|
||||
if (i != 7) stamp += ".";
|
||||
temp = "";
|
||||
}
|
||||
}
|
||||
return stamp;
|
||||
}
|
||||
|
||||
mac_address packet::getMacAddress(int string_cursor)
|
||||
{
|
||||
mac_address mac_temp(this->getHexString(string_cursor, 6));
|
||||
return mac_temp;
|
||||
{
|
||||
mac_address mac_temp(this->getHexString(string_cursor, 6));
|
||||
return mac_temp;
|
||||
}
|
||||
|
||||
bool packet::isArp()
|
||||
{
|
||||
int protocol_type;
|
||||
|
||||
std::stringstream convert ( this->getHexString(12, 2) );
|
||||
convert>>std::hex>>protocol_type;
|
||||
|
||||
return (protocol_type == ETHER_TYPE_ARP);
|
||||
int protocol_type;
|
||||
|
||||
std::stringstream convert ( this->getHexString(12, 2) );
|
||||
convert>>std::hex>>protocol_type;
|
||||
|
||||
return (protocol_type == ETHER_TYPE_ARP);
|
||||
}
|
||||
|
||||
bool packet::isIPv4()
|
||||
{
|
||||
int protocol_type;
|
||||
|
||||
std::stringstream convert ( this->getHexString(12, 2) );
|
||||
convert>>std::hex>>protocol_type;
|
||||
|
||||
return (protocol_type == ETHER_TYPE_IPV4);
|
||||
int protocol_type;
|
||||
|
||||
std::stringstream convert ( this->getHexString(12, 2) );
|
||||
convert>>std::hex>>protocol_type;
|
||||
|
||||
return (protocol_type == ETHER_TYPE_IPV4);
|
||||
}
|
||||
|
||||
bool packet::isIPv6()
|
||||
{
|
||||
int protocol_type;
|
||||
|
||||
std::stringstream convert ( this->getHexString(12, 2) );
|
||||
convert>>std::hex>>protocol_type;
|
||||
|
||||
return (protocol_type == ETHER_TYPE_IPV6);
|
||||
int protocol_type;
|
||||
|
||||
std::stringstream convert ( this->getHexString(12, 2) );
|
||||
convert>>std::hex>>protocol_type;
|
||||
|
||||
return (protocol_type == ETHER_TYPE_IPV6);
|
||||
}
|
||||
|
||||
mac_address packet::getSenderMac()
|
||||
{
|
||||
return this->getMacAddress(6);
|
||||
return this->getMacAddress(6);
|
||||
}
|
||||
|
||||
mac_address packet::getTargetMac()
|
||||
{
|
||||
return this->getMacAddress(0);
|
||||
return this->getMacAddress(0);
|
||||
}
|
||||
|
||||
int packet::getEtherType()
|
||||
{
|
||||
int protocol_type;
|
||||
|
||||
std::stringstream convert ( this->getHexString(12, 2) );
|
||||
convert>>std::hex>>protocol_type;
|
||||
|
||||
return protocol_type;
|
||||
int protocol_type;
|
||||
|
||||
std::stringstream convert ( this->getHexString(12, 2) );
|
||||
convert>>std::hex>>protocol_type;
|
||||
|
||||
return protocol_type;
|
||||
}
|
||||
|
||||
|
||||
|
@ -173,34 +173,34 @@ int packet::getEtherType()
|
|||
|
||||
ARPpacket::ARPpacket(int timeEpoch_i, int timeMillis_i, std::string rawData_i)
|
||||
{
|
||||
|
||||
timeEpoch = timeEpoch_i;
|
||||
timeMillis = timeMillis_i;
|
||||
rawData = rawData_i;
|
||||
pkgLength = rawData_i.length() / 2;
|
||||
return;
|
||||
|
||||
timeEpoch = timeEpoch_i;
|
||||
timeMillis = timeMillis_i;
|
||||
rawData = rawData_i;
|
||||
pkgLength = rawData_i.length() / 2;
|
||||
return;
|
||||
}
|
||||
|
||||
int ARPpacket::getOpCode()
|
||||
{
|
||||
int opcode;
|
||||
|
||||
std::stringstream convert ( this->getHexString(ARP_OFFSET+6, 2) );
|
||||
convert>>std::hex>>opcode;
|
||||
|
||||
return opcode;
|
||||
int opcode;
|
||||
|
||||
std::stringstream convert ( this->getHexString(ARP_OFFSET+6, 2) );
|
||||
convert>>std::hex>>opcode;
|
||||
|
||||
return opcode;
|
||||
}
|
||||
|
||||
boost::asio::ip::address ARPpacket::getSenderIp()
|
||||
{
|
||||
boost::asio::ip::address newaddr = boost::asio::ip::address::from_string(this->decodeIPaddress(ARP_OFFSET+14));
|
||||
return newaddr;
|
||||
boost::asio::ip::address newaddr = boost::asio::ip::address::from_string(this->decodeIPaddress(ARP_OFFSET+14));
|
||||
return newaddr;
|
||||
}
|
||||
|
||||
boost::asio::ip::address ARPpacket::getTargetIp()
|
||||
{
|
||||
boost::asio::ip::address newaddr = boost::asio::ip::address::from_string(this->decodeIPaddress(ARP_OFFSET+24));
|
||||
return newaddr;
|
||||
boost::asio::ip::address newaddr = boost::asio::ip::address::from_string(this->decodeIPaddress(ARP_OFFSET+24));
|
||||
return newaddr;
|
||||
}
|
||||
|
||||
/* IPV4 */
|
||||
|
@ -208,7 +208,7 @@ boost::asio::ip::address ARPpacket::getTargetIp()
|
|||
packet* IPv4packet::factory(int timeEpoch_i, int timeMillis_i, std::string rawData_i)
|
||||
{
|
||||
//TODO
|
||||
return new UnknownPacket(timeEpoch_i, timeMillis_i, rawData_i);
|
||||
return new UnknownPacket(timeEpoch_i, timeMillis_i, rawData_i);
|
||||
}
|
||||
|
||||
/* TCP */
|
||||
|
@ -216,7 +216,7 @@ return new UnknownPacket(timeEpoch_i, timeMillis_i, rawData_i);
|
|||
packet* TCPv4packet::factory(int timeEpoch_i, int timeMillis_i, std::string rawData_i)
|
||||
{
|
||||
//TODO
|
||||
return new UnknownTCP(timeEpoch_i,timeMillis_i,rawData_i);
|
||||
return new UnknownTCP(timeEpoch_i,timeMillis_i,rawData_i);
|
||||
}
|
||||
|
||||
/* UDP */
|
||||
|
@ -224,38 +224,38 @@ return new UnknownTCP(timeEpoch_i,timeMillis_i,rawData_i);
|
|||
packet* UDPv4packet::factory(int timeEpoch_i, int timeMillis_i, std::string rawData_i)
|
||||
{
|
||||
//TODO
|
||||
return new UnknownUDP(timeEpoch_i, timeMillis_i, rawData_i);
|
||||
return new UnknownUDP(timeEpoch_i, timeMillis_i, rawData_i);
|
||||
}
|
||||
|
||||
/* UNKNOWN */
|
||||
|
||||
UnknownPacket::UnknownPacket(int timeEpoch_i, int timeMillis_i, std::string rawData_i)
|
||||
{
|
||||
timeEpoch = timeEpoch_i;
|
||||
timeMillis = timeMillis_i;
|
||||
rawData = rawData_i;
|
||||
pkgLength = rawData_i.length() / 2;
|
||||
return;
|
||||
timeEpoch = timeEpoch_i;
|
||||
timeMillis = timeMillis_i;
|
||||
rawData = rawData_i;
|
||||
pkgLength = rawData_i.length() / 2;
|
||||
return;
|
||||
}
|
||||
|
||||
/* UNKNOWN TCP */
|
||||
|
||||
UnknownTCP::UnknownTCP(int timeEpoch_i, int timeMillis_i, std::string rawData_i)
|
||||
{
|
||||
timeEpoch = timeEpoch_i;
|
||||
timeMillis = timeMillis_i;
|
||||
rawData = rawData_i;
|
||||
pkgLength = rawData_i.length() / 2;
|
||||
return;
|
||||
timeEpoch = timeEpoch_i;
|
||||
timeMillis = timeMillis_i;
|
||||
rawData = rawData_i;
|
||||
pkgLength = rawData_i.length() / 2;
|
||||
return;
|
||||
}
|
||||
|
||||
/* UNKNOWN UDP */
|
||||
|
||||
UnknownUDP::UnknownUDP(int timeEpoch_i, int timeMillis_i, std::string rawData_i)
|
||||
{
|
||||
timeEpoch = timeEpoch_i;
|
||||
timeMillis = timeMillis_i;
|
||||
rawData = rawData_i;
|
||||
pkgLength = rawData_i.length() / 2;
|
||||
return;
|
||||
timeEpoch = timeEpoch_i;
|
||||
timeMillis = timeMillis_i;
|
||||
rawData = rawData_i;
|
||||
pkgLength = rawData_i.length() / 2;
|
||||
return;
|
||||
}
|
||||
|
|
80
libPacket.h
80
libPacket.h
|
@ -40,116 +40,116 @@
|
|||
/* Class for managing packets*/
|
||||
class packet
|
||||
{
|
||||
|
||||
protected:
|
||||
|
||||
protected:
|
||||
std::string rawData;
|
||||
long int timeEpoch;
|
||||
int timeMillis;
|
||||
int pkgLength;
|
||||
|
||||
public:
|
||||
|
||||
|
||||
public:
|
||||
|
||||
class Overflow {}; // Gestore overflow.
|
||||
class HeaderFault {}; // Gestore header sbagliato.
|
||||
|
||||
|
||||
/* Costruttore Pacchetto */
|
||||
static packet* factory(int timeEpoch_i, int timeMillis_i, std::string rawData_i);
|
||||
|
||||
|
||||
/* GENERAL FUNCTIONS */
|
||||
|
||||
|
||||
/* Ottieni lunghezza in byte */
|
||||
int getLength();
|
||||
|
||||
|
||||
/* Ottieni epoch */
|
||||
long int getEpoch();
|
||||
|
||||
|
||||
/* Ottienti millisecondi passati dopo epoch */
|
||||
int getMillis();
|
||||
|
||||
|
||||
/* Legge n byte a partire dal byte voluto e li restituisce in stringa. */
|
||||
std::string getHexString(int string_cursor, int read_byte);
|
||||
|
||||
|
||||
/* Legge IPv4 dal byte voluto e restituisce in formato decimale. */
|
||||
std::string decodeIPaddress(int string_cursor);
|
||||
|
||||
|
||||
/* Salva MAC address a partire da un punto (n° del byte) della stringa rawData */
|
||||
mac_address getMacAddress(int string_cursor);
|
||||
|
||||
|
||||
/* Salva IPv4 address a partire da un punto (n° del byte) della stringa rawData */
|
||||
boost::asio::ip::address getIPv4Address(int string_cursor);
|
||||
|
||||
|
||||
/* True se e' un pacchetto ARP */
|
||||
bool isArp();
|
||||
|
||||
|
||||
/* True se e' un pacchetto IPv4 */
|
||||
bool isIPv4();
|
||||
|
||||
|
||||
/* True se e' un pacchetto IPv6*/
|
||||
bool isIPv6();
|
||||
|
||||
|
||||
/* ETHERNET FUNCTIONS */
|
||||
|
||||
|
||||
/* Restituisce MAC della scheda di rete che ha inviato la trama*/
|
||||
mac_address getSenderMac();
|
||||
|
||||
|
||||
/* Restituisce MAC del destinatario della trama*/
|
||||
mac_address getTargetMac();
|
||||
|
||||
|
||||
/*Restituisce ethertype*/
|
||||
int getEtherType();
|
||||
|
||||
|
||||
};
|
||||
|
||||
/*Class for managing ARP packets*/
|
||||
class ARPpacket : public packet
|
||||
{
|
||||
public:
|
||||
ARPpacket(int timeEpoch_i, int timeMillis_i, std::string rawData_i);
|
||||
int getOpCode();
|
||||
boost::asio::ip::address getSenderIp();
|
||||
boost::asio::ip::address getTargetIp();
|
||||
public:
|
||||
ARPpacket(int timeEpoch_i, int timeMillis_i, std::string rawData_i);
|
||||
int getOpCode();
|
||||
boost::asio::ip::address getSenderIp();
|
||||
boost::asio::ip::address getTargetIp();
|
||||
};
|
||||
|
||||
/*Class for managing IPv4 packets*/
|
||||
class IPv4packet : public packet
|
||||
{
|
||||
public:
|
||||
static packet* factory(int timeEpoch_i, int timeMillis_i, std::string rawData_i);
|
||||
public:
|
||||
static packet* factory(int timeEpoch_i, int timeMillis_i, std::string rawData_i);
|
||||
};
|
||||
|
||||
/*Class for managing TCPv4 packets*/
|
||||
class TCPv4packet : public IPv4packet
|
||||
{
|
||||
public:
|
||||
static packet* factory(int timeEpoch_i, int timeMillis_i, std::string rawData_i);
|
||||
public:
|
||||
static packet* factory(int timeEpoch_i, int timeMillis_i, std::string rawData_i);
|
||||
};
|
||||
|
||||
/*Class for managing UDPv4 packets*/
|
||||
class UDPv4packet : public IPv4packet
|
||||
{
|
||||
public:
|
||||
static packet* factory(int timeEpoch_i, int timeMillis_i, std::string rawData_i);
|
||||
public:
|
||||
static packet* factory(int timeEpoch_i, int timeMillis_i, std::string rawData_i);
|
||||
};
|
||||
|
||||
/*Class for managing unknown packets*/
|
||||
class UnknownPacket : public packet
|
||||
{
|
||||
public:
|
||||
UnknownPacket(int timeEpoch_i, int timeMillis_i, std::string rawData_i);
|
||||
public:
|
||||
UnknownPacket(int timeEpoch_i, int timeMillis_i, std::string rawData_i);
|
||||
};
|
||||
|
||||
/*Class for managing unknown TCPv4 packets*/
|
||||
class UnknownTCP : public TCPv4packet
|
||||
{
|
||||
public:
|
||||
UnknownTCP(int timeEpoch_i, int timeMillis_i, std::string rawData_i);
|
||||
public:
|
||||
UnknownTCP(int timeEpoch_i, int timeMillis_i, std::string rawData_i);
|
||||
};
|
||||
|
||||
/*Class for managing unknown UDPv4 packets*/
|
||||
class UnknownUDP : public TCPv4packet
|
||||
{
|
||||
public:
|
||||
UnknownUDP(int timeEpoch_i, int timeMillis_i, std::string rawData_i);
|
||||
public:
|
||||
UnknownUDP(int timeEpoch_i, int timeMillis_i, std::string rawData_i);
|
||||
};
|
||||
|
||||
#endif //LIBHEADER_H
|
||||
#endif //LIBHEADER_H
|
||||
|
|
|
@ -20,46 +20,46 @@
|
|||
|
||||
void pcap_fatal(const char *error_in, const char *error_buffer)
|
||||
{
|
||||
std::stringstream ss;
|
||||
ss<<"Fatal Error in "<<error_in<<": "<<error_buffer;
|
||||
throw(std::runtime_error(ss.str()));
|
||||
std::stringstream ss;
|
||||
ss<<"Fatal Error in "<<error_in<<": "<<error_buffer;
|
||||
throw(std::runtime_error(ss.str()));
|
||||
}
|
||||
|
||||
static void memPrint(const unsigned char *start, char len, int index)
|
||||
{
|
||||
printf("0x%08x | ",index);
|
||||
int i;
|
||||
for(i=0;i<len;i++) printf("%02x ",start[i]);
|
||||
for(i=0;i<(16-len);i++) printf(" ");
|
||||
printf("| ");
|
||||
for(i=0;i<len;i++)
|
||||
{
|
||||
if((start[i]>32)&&(start[i]<128)) printf("%c",start[i]);
|
||||
else printf(".");
|
||||
}
|
||||
printf("\n");
|
||||
printf("0x%08x | ",index);
|
||||
int i;
|
||||
for (i=0;i<len;i++) printf("%02x ",start[i]);
|
||||
for (i=0;i<(16-len);i++) printf(" ");
|
||||
printf("| ");
|
||||
for (i=0;i<len;i++)
|
||||
{
|
||||
if ((start[i]>32)&&(start[i]<128)) printf("%c",start[i]);
|
||||
else printf(".");
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
void hexDump(const unsigned char *start, struct pcap_pkthdr header)
|
||||
{
|
||||
std::cout<<std::endl<<"[TS: "<<header.ts.tv_sec;
|
||||
std::cout<<" uS: "<<header.ts.tv_usec;
|
||||
std::cout<<"] Received "<<header.len<<" byte:"<<std::endl;
|
||||
int index=0;
|
||||
while(header.len>16)
|
||||
{
|
||||
memPrint(start,16,index);
|
||||
header.len-=16;
|
||||
start+=16;
|
||||
index+=16;
|
||||
}
|
||||
if(header.len>0) memPrint(start,header.len,index);
|
||||
std::cout<<std::endl<<"[TS: "<<header.ts.tv_sec;
|
||||
std::cout<<" uS: "<<header.ts.tv_usec;
|
||||
std::cout<<"] Received "<<header.len<<" byte:"<<std::endl;
|
||||
int index=0;
|
||||
while (header.len>16)
|
||||
{
|
||||
memPrint(start,16,index);
|
||||
header.len-=16;
|
||||
start+=16;
|
||||
index+=16;
|
||||
}
|
||||
if (header.len>0) memPrint(start,header.len,index);
|
||||
}
|
||||
|
||||
void rawDump(const unsigned char *start, struct pcap_pkthdr header)
|
||||
{
|
||||
std::cout<<header.ts.tv_sec<<"!";
|
||||
std::cout<<header.ts.tv_usec<<"!";
|
||||
for(int i=0;i<header.len;i++) printf("%02x",start[i]);
|
||||
std::cout<<std::endl;
|
||||
std::cout<<header.ts.tv_sec<<"!";
|
||||
std::cout<<header.ts.tv_usec<<"!";
|
||||
for (int i=0;i<header.len;i++) printf("%02x",start[i]);
|
||||
std::cout<<std::endl;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue