Compare commits

...

19 Commits

Author SHA1 Message Date
4f1cb82f46 Remove unused include 2021-11-22 21:27:26 +01:00
b7b7f6edbb Fixed exception when signal-data could is missing 2021-11-22 21:27:07 +01:00
8d67321d4b Implemented payload-alanyzing (size) 2021-11-22 21:26:47 +01:00
61f3c432c9 Moved head-analyzing to own function 2021-11-22 21:26:17 +01:00
52119fe3cd Added handling of special packet-types with their respective data 2021-11-22 21:25:29 +01:00
f938f8dd53 Finished packet-type detection 2021-11-22 21:23:34 +01:00
5b8a330e69 Added package-type 2021-11-22 21:19:18 +01:00
35e71ef44e Fixed default value 2021-11-22 21:19:10 +01:00
8ef6318a56 Added method to convert hex-char to hex-value 2021-11-22 18:00:12 +01:00
9301c4e0fd Added Copyconstrucotr for base and made inheritance public 2021-11-22 17:00:37 +01:00
4d8597b1d6 Made pass const 2021-11-22 16:15:52 +01:00
3c379a1a80 Rename split.hpp to string-helper.hpp 2021-11-22 16:15:20 +01:00
6710442067 Fixed missing imports 2021-11-22 16:14:37 +01:00
e191085cbd Renamed ssid to lowercase 2021-11-22 15:39:14 +01:00
f834ec7134 Fixed fields not being public 2021-11-22 15:39:02 +01:00
035491668a Fixed wrong key used 2021-11-22 15:38:30 +01:00
16028daf10 Auto-Remove newline-char 2021-11-22 15:23:53 +01:00
1ed9c84802 Added array to retrieve package-type names from enum 2021-11-22 15:23:13 +01:00
a0ae919614 Removed asyncHandler and creating new threads now once buffer-vector is complete 2021-11-22 14:56:18 +01:00
10 changed files with 171 additions and 39 deletions

View File

@ -4,8 +4,15 @@
#include "./packet.hpp"
#include <string>
class BeaconPacket : Packet{
std::string SSID;
class BeaconPacket : public Packet{
public:
BeaconPacket()
{}
BeaconPacket(const Packet &packet)
: Packet(packet)
{}
std::string ssid;
};
#endif /* FDDB997A_BCD3_4056_BFEA_9FF6A548DACF */

View File

@ -15,6 +15,18 @@ enum PacketType {
NoData,
Unknown
};
const std::array<const char*, 10> PACKET_TYPE_NAMES({{
"Beacon",
"Probe Request",
"Probe Response",
"Data",
"Request to send",
"Clear to send",
"Acknowledgment",
"BlockAcknowledgment",
"NoData",
"Unknown"
}});
struct Packet {
uint64_t timestampMicros;

View File

@ -4,8 +4,15 @@
#include "./packet.hpp"
#include <string>
class ProbeRequestPacket : Packet{
std::string requestSSID;
class ProbeRequestPacket : public Packet{
public:
ProbeRequestPacket()
{}
ProbeRequestPacket(const Packet &packet)
: Packet(packet)
{}
std::string requestSsid;
};
#endif /* CD2BF199_8153_4F10_A85C_50883FAD66A8 */

View File

@ -0,0 +1,18 @@
#ifndef B199B4B3_BE27_4F0C_8DBE_5E78580AB1A9
#define B199B4B3_BE27_4F0C_8DBE_5E78580AB1A9
#include "./packet.hpp"
#include <string>
class ProbeResponsePacket : public Packet{
public:
ProbeResponsePacket()
{}
ProbeResponsePacket(const Packet &packet)
: Packet(packet)
{}
std::string responseSsid;
};
#endif /* B199B4B3_BE27_4F0C_8DBE_5E78580AB1A9 */

View File

@ -1,17 +0,0 @@
#ifndef EFFCCB40_3639_4BD4_9649_302F05987909
#define EFFCCB40_3639_4BD4_9649_302F05987909
#include <future>
#include <string.h>
#include "bufHandler.hpp"
void asyncHandler(char *buf){
// Create a copy of buf for our thread
char bufCopy[265];
strcpy(bufCopy, buf);
// \/ Surpress unused warning
(void)std::async(std::launch::async, bufHandler, bufCopy);
}
#endif /* EFFCCB40_3639_4BD4_9649_302F05987909 */

View File

@ -1,22 +1,28 @@
#ifndef C251BA62_6D80_4033_86B6_61F184E6F250
#define C251BA62_6D80_4033_86B6_61F184E6F250
#include <future>
#include <string>
#include "textPacketHandler.hpp"
using namespace std::string_literals;
std::vector<std::string> buffer;
void bufHandler(char *buf){
void bufHandler(const char *buf){
std::string line = buf;
// Remove last char which is \n
line = line.substr(0, line.size()-1);
// When first char of buf has text (no tab), we got a new packet
if(buf[0] != '\t'){
// Submit the just-read text-packet
if(buffer.size() != 0) textPacketHandler(buffer);
buffer = std::vector<std::string>();
// Submit the just-read text-packet in a new thread
if(buffer.size() != 0) {
(void)std::async(std::launch::async, textPacketHandler, buffer);
}
buffer = {line};
}
// Append part-packet
buffer.push_back(buf);
else
buffer.push_back(line); // Append part-packet
}
#endif /* C251BA62_6D80_4033_86B6_61F184E6F250 */

View File

@ -3,16 +3,20 @@
#include <string>
#include "../DTO/packet.hpp"
#include "../DTO/beaconPacket.hpp"
#include "../DTO/probeRequestPacket.hpp"
#include "../DTO/probeResponsePacket.hpp"
#include <vector>
#include <sstream>
#include <locale>
#include <iomanip>
#include "../helper/split.hpp"
#include "../helper/string-helper.hpp"
#include "../helper/timestampConvert.hpp"
#include "../helper/find.hpp"
#include "../helper/vector-stats.hpp"
#include <unordered_map>
using namespace std::string_literals;
const std::unordered_map<std::string, PacketType> PACKET_TYPE_MAP({
{"Beacon", PacketType::Beacon},
{"Probe Request", PacketType::ProbeRequest},
@ -24,11 +28,20 @@ const std::unordered_map<std::string, PacketType> PACKET_TYPE_MAP({
{"BA", PacketType::BlockAcknowledgment}
});
void textPacketHandler(std::vector<std::string> textPacket){
void parseHeader(Packet &packet, const std::vector<std::string> &textPacket);
void parsePayload(Packet &packet, const std::vector<std::string> &textPacket);
void textPacketHandler(const std::vector<std::string> textPacket){
/// Here we have to parse the packet
// Create empty packet
Packet packet;
parseHeader(packet, textPacket);
parsePayload(packet, textPacket);
}
void parseHeader(Packet &packet, const std::vector<std::string> &textPacket){
const std::string textHeader = textPacket[0];
const std::vector<std::string> headerData = split(textHeader, ' ');
@ -44,8 +57,14 @@ void textPacketHandler(std::vector<std::string> textPacket){
packet.frequency = std::stoi(headerData[frequencyIndex]);
int signalIndex = findIs(headerData, "signal", 1, 1);
if(signalIndex != -1){
std::string signalText = headerData[signalIndex].substr(0, 3);
packet.signal = std::stoi(signalText);
}
else {
fprintf(stderr, "Missing signal-data!\n");
packet.signal = -100;
}
// Addresses seem complicated at first, but just have many fields which might be available.
// SA and DA are src- and dst-Addresses
@ -62,8 +81,8 @@ void textPacketHandler(std::vector<std::string> textPacket){
int bssidIndex = findContains(headerData, "BSSID:", 1);
std::string bssidAddr = (bssidIndex != -1) ? headerData[bssidIndex].substr("BSSID:"s.length()) : "";
int taIndex = findContains(headerData, "SA:", 1);
std::string tAddr = (taIndex != -1) ? headerData[taIndex].substr("SA:"s.length()) : "";
int taIndex = findContains(headerData, "TA:", 1);
std::string tAddr = (taIndex != -1) ? headerData[taIndex].substr("TA:"s.length()) : "";
int raIndex = findContains(headerData, "RA:", 1);
std::string rAddr = (raIndex != -1) ? headerData[raIndex].substr("RA:"s.length()) : "";
@ -92,10 +111,72 @@ void textPacketHandler(std::vector<std::string> textPacket){
// If type is in map, use map-value, otherwise keep default
if(PACKET_TYPE_MAP.find(textType) != PACKET_TYPE_MAP.end())
type = PACKET_TYPE_MAP[textType];
type = PACKET_TYPE_MAP.at(textType);
if(type == PacketType::Unknown){
fprintf(stderr, "Unknown package-type: %s\n", textType.c_str());
}
}
packet.type = type;
//
// Read data for specializations
if(type == PacketType::Beacon){
// Create BeaconPacket from packet
BeaconPacket beaconPacket = BeaconPacket(packet);
packet = beaconPacket; // Overwrite packet
// Find ssid
int start = textHeader.find('(')+1;
std::string ssid = textHeader.substr(start, textHeader.find(')')-start);
// Write to packet
beaconPacket.ssid = ssid;
}
else if (type == PacketType::ProbeRequest){
// Create ProbeRequestPacket from packet
ProbeRequestPacket probeRequestPacket = ProbeRequestPacket(packet);
packet = probeRequestPacket; // Overwrite packet
// Find probe-request
int start = textHeader.find('(')+1;
std::string requestSsid = textHeader.substr(start, textHeader.find(')')-start);
// Write to packet
probeRequestPacket.requestSsid = requestSsid;
}
else if (type == PacketType::ProbeResponse){
// Create ProbeResponsePacket from packet
ProbeResponsePacket probeResponsePacket = ProbeResponsePacket(packet);
packet = probeResponsePacket; // Overwrite packet
// Find probe-request
int start = textHeader.find('(')+1;
std::string responseSsid = textHeader.substr(start, textHeader.find(')')-start);
// Write to packet
probeResponsePacket.responseSsid = responseSsid;
}
}
void parsePayload(Packet &packet, const std::vector<std::string> &textPacket){
// Expect max of 16byte per line of payload
unsigned int payloadSize = 16*(textPacket.size()-1);
// Go through last line
int line = textPacket.size()-1, charPos;
for(int f=0; f<8*2; ++f){
charPos = 10 + (f/2.0*5);
if(textPacket[line][charPos] == ' ') { // When our char is space, no more data is present
// Set size
payloadSize = 16*(textPacket.size()-2)+f;
break;
}
}
packet.payloadSize = payloadSize;
}
#endif /* EE781A91_6D07_47AC_B3C4_F99E29F3731F */

View File

@ -8,8 +8,8 @@
/// @param cmd is the command
/// @param handler is the handler(char*)-function
/// @return Return-code form command
int exec(const char* cmd, void (*handler)(char*) = nullptr){
const int buf_size = 256;
int exec(const char* cmd, void (*handler)(const char*) = nullptr){
const int buf_size = 512;
char buf[buf_size];
// Open execution-pipe

View File

@ -1,6 +1,10 @@
#ifndef F7CFE6A7_34BF_4E04_94CF_DB8374980631
#define F7CFE6A7_34BF_4E04_94CF_DB8374980631
#include <vector>
#include <string>
#include <sstream>
std::vector<std::string> split(const std::string& s, char delimiter)
{
std::vector<std::string> tokens;
@ -13,4 +17,18 @@ std::vector<std::string> split(const std::string& s, char delimiter)
return tokens;
}
char hex_char_to_int(const char &c) {
unsigned char result = 0;
if( ('0' <= c) && (c <= '9') ) {
result = c - '0';
}
else if( ('A' <= c) && (c <= 'F') ) {
result = 10 + c - 'A';
}
else if( ('a' <= c) && (c <= 'f') ) {
result = 10 + c - 'a';
}
return result;
}
#endif /* F7CFE6A7_34BF_4E04_94CF_DB8374980631 */

View File

@ -1,7 +1,7 @@
#include <stdio.h>
#include <string>
#include "./helper/exec.hpp"
#include "./handler/asyncHandler.hpp"
#include "./handler/bufHandler.hpp"
const std::string tcpdump_baseCmd = "tcpdump -vvv -e -n -X -s0 -i ";
@ -15,7 +15,7 @@ int main(int argc, char *args[]){
exit(1);
}
int exitCode = exec(tcpdump_cmd.c_str(), &asyncHandler);
int exitCode = exec(tcpdump_cmd.c_str(), &bufHandler);
if(exitCode){
fprintf(stderr, "\ntcpdump exited with non-zero ExitCode: %d\n Something went wrong! Check tcpdump-output for more information.\n", exitCode);