initial commit

This commit is contained in:
2017-03-12 12:42:19 +01:00
parent fedf2b7b90
commit dcb9ea4d44
12 changed files with 1435 additions and 0 deletions

27
net/IP.h Normal file
View File

@@ -0,0 +1,27 @@
#ifndef IP_H
#define IP_H
#define Port uint16_t
struct IP {
uint32_t val;
/** empty ctor */
IP() : val(0) {
;
}
/** ctor with IP-string */
IP(const char* ipStr) {
set(ipStr);
}
/** set the IP */
void set(const char* ipStr) {
val = ipaddr_addr(ipStr);
}
};
#endif // IP_H

73
net/MAC.h Normal file
View File

@@ -0,0 +1,73 @@
#ifndef MAC_H
#define MAC_H
#include <string>
#include <cstdint>
#include <string.h>
namespace WiFiRaw {
struct MACAddress {
uint8_t a;
uint8_t b;
uint8_t c;
uint8_t d;
uint8_t e;
uint8_t f;
/** empty ctor */
MACAddress() {;}
/** ctor from distinct values */
MACAddress(const uint8_t a, const uint8_t b, const uint8_t c, const uint8_t d, const uint8_t e, const uint8_t f) :
a(a), b(b), c(c), d(d), e(e), f(f) {;}
/** ctor from memory region */
MACAddress(const uint8_t* data) {
memcpy(this, data, 6);
}
/** equal to the given mac? */
bool operator == (const MACAddress& o) const {
return (a == o.a) && (b == o.b) && (c == o.c) && (d == o.d) && (e == o.e) && (f == o.f);
}
std::string asString() const {
std::string mac; mac.resize(17);
mac[0] = toHex(a >> 4);
mac[1] = toHex(a >> 0);
mac[2] = ':';
mac[3] = toHex(b >> 4);
mac[4] = toHex(b >> 0);
mac[5] = ':';
mac[6] = toHex(c >> 4);
mac[7] = toHex(c >> 0);
mac[8] = ':';
mac[9] = toHex(d >> 4);
mac[10] = toHex(d >> 0);
mac[11] = ':';
mac[12] = toHex(e >> 4);
mac[13] = toHex(e >> 0);
mac[14] = ':';
mac[15] = toHex(f >> 4);
mac[16] = toHex(f >> 0);
return mac;
}
private:
inline char toHex(uint8_t c) const {
const uint8_t val = c & 0xF;
return (val >= 10) ? ('A' + val - 10) : ('0' + val);
}
};
}
#endif // MAC_H

52
net/Promiscuous.h Normal file
View File

@@ -0,0 +1,52 @@
#ifndef PROMISCUOUS_H
#define PROMISCUOUS_H
struct RxControl {
signed rssi:8; // signal intensity of packet
unsigned rate:4;
unsigned is_group:1;
unsigned:1;
unsigned sig_mode:2; // 0:is 11n packet; 1:is not 11n packet;
unsigned legacy_length:12; // if not 11n packet, shows length of packet.
unsigned damatch0:1;
unsigned damatch1:1;
unsigned bssidmatch0:1;
unsigned bssidmatch1:1;
unsigned MCS:7; // if is 11n packet, shows the modulation // and code used (range from 0 to 76)
unsigned CWB:1; // if is 11n packet, shows if is HT40 packet or not
unsigned HT_length:16;// if is 11n packet, shows length of packet.
unsigned Smoothing:1;
unsigned Not_Sounding:1;
unsigned:1;
unsigned Aggregation:1;
unsigned STBC:2;
unsigned FEC_CODING:1; // if is 11n packet, shows if is LDPC packet or not.
unsigned SGI:1;
unsigned rxend_state:8;
unsigned ampdu_cnt:8;
unsigned channel:4; //which channel this packet in.
unsigned:12;
};
struct LenSeq{
u16 len; // length of packet
u16 seq; // serial number of packet, the high 12bits are serial number,
// low 14 bits are Fragment number (usually be 0)
u8 addr3[6]; // the third address in packet
};
struct sniffer_buf{
struct RxControl rx_ctrl;
u8 buf[36 ]; // head of ieee80211 packet
u16 cnt; // number count of packet
struct LenSeq lenseq[1]; //length of packet
};
struct sniffer_buf2{
struct RxControl rx_ctrl;
uint8_t buf[112];
uint16_t cnt;
uint16_t len;
};
#endif // PROMISCUOUS_H

103
net/UDP.h Normal file
View File

@@ -0,0 +1,103 @@
#ifndef UDP_H
#define UDP_H
extern "C" {
#include "mem.h"
#include "espconn.h"
}
typedef void (*UDPCallback)(void* arg, char* data, unsigned short len);
#include "../Debug.h"
#include "IP.h"
class UDP {
private:
static constexpr const char* NAME = "UDP";
espconn* con;
public:
UDP() {
init();
}
/** dtor */
~UDP() {
cleanup();
}
/** bind the socket to the given local port */
void bind(const Port localPort) {
debugMod1(NAME, "binding to local port %d", localPort);
// set the local port to listen on
con->proto.udp->local_port = localPort;
// todo: check? 0=OK
const int res = espconn_create(con);
os_printf("create: %d\r\n", res);
}
bool send(const IP ip, const Port port, const void* data, const uint16_t dataLen) {
debugMod1(NAME, "sending packet to remote port %d", port);
// set remote port and IP
con->proto.udp->remote_port = port;
os_memcpy(con->proto.udp->remote_ip, &(ip.val), 4);
//os_printf("send %d bytes\r\n", dataLen);
//os_printf("IP: %d.%d.%d.%d\n\r", con->proto.udp->remote_ip[0], con->proto.udp->remote_ip[1], con->proto.udp->remote_ip[2], con->proto.udp->remote_ip[3]);
// send. TODO: check. 0=OK
const int res = espconn_sent(con, (unsigned char*)data, dataLen);
return (res == 0);
}
/** set the callback to call whenever a packet is received */
void setRecvCallback(UDPCallback callback) {
espconn_regist_recvcb(con, callback);
}
private:
/** initialize the UDP "connection" */
void init() {
debugMod(NAME, "init()");
// allocate connection-objects
con = (espconn*) os_zalloc(sizeof(espconn));
ets_memset( con, 0, sizeof( espconn ) );
// configure
con->type = ESPCONN_UDP;
//con->state = ESPCONN_NONE;
con->proto.udp = (esp_udp*) os_zalloc(sizeof(esp_udp));
}
/** cleanup everything */
void cleanup() {
debugMod(NAME, "cleanup()");
espconn_delete(con);
os_free(con->proto.udp);
os_free(con);
}
};
#endif // UDP_H

195
net/WiFiRaw.h Normal file
View File

@@ -0,0 +1,195 @@
#ifndef WIFIRAW_H
#define WIFIRAW_H
#include "MAC.h"
// ifconfig wlp0s26u1u2u1 down && iw dev wlp0s26u1u2u1 set monitor none && ifconfig wlp0s26u1u2u1 up && iw dev wlp0s26u1u2u1 set channel 3
// ifconfig wlp0s26u1u2u1 down && iw dev wlp0s26u1u2u1 set monitor none && ifconfig wlp0s26u1u2u1 up iwconfig wlp0s26u1u2u1 channel 3
// https://supportforums.cisco.com/document/52391/80211-frames-starter-guide-learn-wireless-sniffer-traces
namespace WiFiRaw {
/** frame type */
enum Type {
MANAGEMENT = 0b00,
CONTROL = 0b01,
DATA = 0b10,
RESERVED = 0b11,
};
/** frame sub-type */
enum SubType {
DATA_DATA = 0b0000,
DATA_DATA_CF_ACK = 0b0001,
DATA_DATA_CF_POLL = 0b0010,
DATA_DATA_CF_ACK_POLL = 0b0011,
DATA_NULL = 0b0100,
DATA_QOS = 0b1000,
MGMT_ASSOC_REQUEST = 0b0000,
MGMT_PROBE_REQUEST = 0b0100,
MGMT_BEACON = 0b1000,
MGMT_DISASSOCIATION = 0b1010,
};
/**
* 2 byte segment and fragment number.
* usually set by the ESP itself
*/
struct Fragment {
uint8_t data[2];
/** empty ctor */
Fragment() : data() {;}
/** ctor with values */
Fragment(const uint8_t fragNr, const uint16_t seqNr) {
data[1] = seqNr >> 4;
data[0] = seqNr << 4 | fragNr;
}
};
// /**
// * 4 byte checksum.
// * usually set by the ESP itself
// */
// struct FCS {
// uint8_t data[4];
// FCS() : data() {;}
// };
struct Header {
uint8_t version : 2; // always 0
uint8_t type : 2; // see enum
uint8_t subType : 4; // see enum
struct Flags {
uint8_t toDS : 1; // always 0
uint8_t fromDS : 1; // unencrypted: 0
uint8_t moreFragments : 1; // no more data: 0 , more data: 1
uint8_t retransmission : 1; // no retransmission: 0, retransmission: 1
uint8_t powerManagement : 1; // i am fully active: 0, tell AP that i safe power: 1,
uint8_t moreData : 1; // ?
uint8_t protectedPkt : 1; // 1 = encrypted, 0 = unencrypted
uint8_t strictlyOrdered : 1; // 0 = not strictly ordered
Flags() :
toDS(0), fromDS(0), moreFragments(0), retransmission(0), powerManagement(0),
moreData(0), protectedPkt(0), strictlyOrdered(0) {;}
} flags;
uint16_t duration; // ??
Header(Type type, SubType subType) : version(0), type(type), subType(subType), duration(0) {
;
}
};
struct Timestamp {
uint8_t data[8];
Timestamp() : data() {;}
};
struct BeaconInterval {
uint8_t data[2];
BeaconInterval() {
data[0] = 0x64;
data[1] = 0x00;
}
};
struct Capabilities {
uint8_t isSTA : 1;
uint16_t empty : 15;
Capabilities() : isSTA(1), empty(0) {;}
};
/** base-struct for management frames */
struct ManagementFrame {
Timestamp ts;
BeaconInterval interval;
Capabilities capa;
ManagementFrame() : ts(), interval(), capa() {;}
};
struct UnknownPkt {
Header header;
WiFiRaw::MACAddress destination;
WiFiRaw::MACAddress transmitter;
WiFiRaw::MACAddress bssid;
};
/** beacon packet */
template <typename T> struct BeaconPkt {
Header header;
WiFiRaw::MACAddress destination; // usually broadcast ff:ff:ff:ff:ff:ff
WiFiRaw::MACAddress transmitter; // usually the AP's MAC
WiFiRaw::MACAddress bssid; // the AP's MAC
Fragment frag;
ManagementFrame mgmt;
T data;
//FCS fcs;
BeaconPkt(const WiFiRaw::MACAddress myMAC) :
header(MANAGEMENT, MGMT_BEACON), destination(0xFF,0xFF,0xFF,0xFF,0xFF,0xFF), transmitter(myMAC), bssid(myMAC) {
;
}
};
/** data packet */
template <int size> struct DataPkt {
Header header;
WiFiRaw::MACAddress bssid; // the AP's mac
WiFiRaw::MACAddress transmitter; // my mac
WiFiRaw::MACAddress destination; // the receiver's mac
Fragment frag;
uint8_t data[size];
//FCS fcs;
DataPkt(const WiFiRaw::MACAddress& mine, const WiFiRaw::MACAddress& receiver, const WiFiRaw::MACAddress& bssid) :
header(DATA, DATA_DATA_CF_ACK), bssid(bssid), transmitter(mine), destination(receiver) {
header.flags.toDS = 1;
}
};
WiFiRaw::MACAddress getMyMAC() {
WiFiRaw::MACAddress mine;
wifi_get_macaddr(0x00, (uint8_t*)&mine);
return mine;
}
};
#endif // WIFIRAW_H