|
@@ -7,84 +7,36 @@
|
|
|
#include "ptwist.h"
|
|
|
#include "rserv.h"
|
|
|
#include "flow.h"
|
|
|
-
|
|
|
-#define macaddr "08:00:27:e8:9d:d4"
|
|
|
-
|
|
|
-/* Ethernet addresses are 6 bytes */
|
|
|
-#define ETHER_ADDR_LEN 6
|
|
|
-#define ETHER_HEADER_LEN 2*ETHER_ADDR_LEN + 2
|
|
|
-
|
|
|
-/* Definitions for parsing packet data */
|
|
|
-struct ip_header {
|
|
|
- u_char versionihl; /* Version >> 4 | IHL & 0x0f */
|
|
|
- u_char dscpecn; /* DSCP >> 2 | ECN & 0x03 */
|
|
|
- u_short len; /* Total Length */
|
|
|
- u_short id; /* Identification */
|
|
|
- u_short flagsoff; /* Flags >> 13 | Fragment Offset & 0x1fff */
|
|
|
-#define RF 0x8000 /* Reserved; must be zero */
|
|
|
-#define DF 0x4000 /* Dont Fragment */
|
|
|
-#define MF 0x2000 /* More Fragments */
|
|
|
- u_char ttl; /* Time To Live */
|
|
|
- u_char proto; /* Protocol */
|
|
|
- u_short chksum; /* Header Checksum */
|
|
|
- struct in_addr src, dst; /* Source and Destination addresses */
|
|
|
-};
|
|
|
-#define IP_HEADER_LEN(ip) (((ip)->versionihl) & 0x0f)*4
|
|
|
-
|
|
|
-struct tcp_header {
|
|
|
- u_short src_port; /* source port */
|
|
|
- u_short dst_port; /* destination port */
|
|
|
- u_int sequence_num; /* sequence number */
|
|
|
- u_int ack_num; /* acknowledgement number */
|
|
|
- u_char offset_res_ns; /*Data offset >> 4 | res >> 1 | NS 0x01 */
|
|
|
- u_char flags; /* Flags */
|
|
|
-#define FIN 0x01
|
|
|
-#define RST 0x04
|
|
|
- u_short win_size; /* Window size*/
|
|
|
- u_short chksum; /* Checksum */
|
|
|
- u_short urg; /* Urgent pointer */
|
|
|
-};
|
|
|
-#define TCP_HEADER_LEN(tcp) (((tcp)->offset_res_ns) >> 4)*4
|
|
|
-
|
|
|
-struct tls_header {
|
|
|
- u_char type; /* Content Type */
|
|
|
-#define CCS 0x14
|
|
|
-#define A 0x15
|
|
|
-#define HS 0x16
|
|
|
-#define APP 0x17
|
|
|
-#define HB 0x18
|
|
|
- u_short version; /* Version */
|
|
|
- u_short len; /* Length */
|
|
|
- u_char msg; /* Message Type */
|
|
|
-#define CLIENT_HELLO 0x01
|
|
|
-#define FINISHED 0x14
|
|
|
-};
|
|
|
-#define RECORD_HEADER_LEN 5
|
|
|
-#define CLIENT_HELLO_HEADER_LEN 6
|
|
|
-
|
|
|
-struct sniff_args {
|
|
|
- char *readdev;
|
|
|
- char *writedev;
|
|
|
- char *filter;
|
|
|
-};
|
|
|
+#include "slitheen.h"
|
|
|
+#include "util.h"
|
|
|
|
|
|
void got_packet(u_char *args, const struct pcap_pkthdr *header, const u_char *packet);
|
|
|
-void check_handshake(const struct tcp_header *tcp_hdr, flow f, unsigned char *p);
|
|
|
+void check_handshake(const struct tcp_header *tcp_hdr, flow f, unsigned char *p, u_short length);
|
|
|
void *sniff_packets(void *);
|
|
|
+void process_packet(const u_char *packet);
|
|
|
|
|
|
-void check_handshake(const struct tcp_header *tcp_hdr, flow f, unsigned char *ptr){
|
|
|
+/** Checks a handshake message to see if it is tagged or a
|
|
|
+ * recognized flow.
|
|
|
+ * Inputs: The tcp header for the message, its associated flow,
|
|
|
+ * a pointer to the handshake header, and the length of
|
|
|
+ * the record.
|
|
|
+ */
|
|
|
+void check_handshake(const struct tcp_header *tcp_hdr, flow f, unsigned char *ptr, u_short length){
|
|
|
|
|
|
FILE *fp;
|
|
|
int res, i, code;
|
|
|
+ u_int size_hs;
|
|
|
unsigned char *p;
|
|
|
+ const struct handshake_header *handshake_hdr;
|
|
|
|
|
|
byte privkey[PTWIST_BYTES];
|
|
|
byte key[16];
|
|
|
|
|
|
p = ptr;
|
|
|
- code = p[0];
|
|
|
+ handshake_hdr = (struct handshake_header*) p;
|
|
|
|
|
|
- printf("handshake code: %d\n", code);
|
|
|
+ code = handshake_hdr->type;
|
|
|
+ size_hs = HANDSHAKE_MESSAGE_LEN(handshake_hdr);
|
|
|
|
|
|
if (code == 0x01){
|
|
|
p += CLIENT_HELLO_HEADER_LEN;
|
|
@@ -118,6 +70,7 @@ void check_handshake(const struct tcp_header *tcp_hdr, flow f, unsigned char *pt
|
|
|
fprintf(fp, "%02x ", p[i]);
|
|
|
}
|
|
|
fclose(fp);
|
|
|
+ //TODO: this might not be okay...
|
|
|
strncpy((char *) f.key, (char *) key, 16);
|
|
|
|
|
|
//Write key to file
|
|
@@ -132,14 +85,10 @@ void check_handshake(const struct tcp_header *tcp_hdr, flow f, unsigned char *pt
|
|
|
fclose(fp);
|
|
|
|
|
|
/* Save flow in table */
|
|
|
- printf("saving flow..\n");
|
|
|
add_flow(f);
|
|
|
- printf("saved\n");
|
|
|
+ printf("Saved new flow\n");
|
|
|
|
|
|
}
|
|
|
- } else {
|
|
|
- int index = check_flow(f);
|
|
|
- update_flow(index, code);
|
|
|
}
|
|
|
|
|
|
}
|
|
@@ -149,82 +98,55 @@ void got_packet(u_char *args, const struct pcap_pkthdr *header, const u_char *pa
|
|
|
char errbuf[BUFSIZ];
|
|
|
char *writedev = (char *) args;
|
|
|
unsigned char *p;
|
|
|
- int index;
|
|
|
|
|
|
const struct ip_header *ip_hdr;
|
|
|
const struct tcp_header *tcp_hdr;
|
|
|
|
|
|
- u_int size_ip;
|
|
|
- u_int size_tcp;
|
|
|
-
|
|
|
- flow newFlow;
|
|
|
-
|
|
|
handle = pcap_open_live(writedev, BUFSIZ, 1, 1000, errbuf);
|
|
|
if (handle == NULL){
|
|
|
fprintf(stderr, "Couldn't open device %s: %s\n", writedev, errbuf);
|
|
|
}
|
|
|
- /* check for clientHello */
|
|
|
+
|
|
|
p = (unsigned char *) packet;
|
|
|
p += ETHER_HEADER_LEN; //skip ethernet header
|
|
|
ip_hdr = (struct ip_header*) p;
|
|
|
- size_ip = IP_HEADER_LEN(ip_hdr);
|
|
|
-
|
|
|
- if (ip_hdr->proto == IPPROTO_TCP){
|
|
|
- p += size_ip; //skip IP header
|
|
|
-
|
|
|
- tcp_hdr = (struct tcp_header*) p;
|
|
|
- size_tcp = TCP_HEADER_LEN(tcp_hdr);
|
|
|
- p += size_tcp;
|
|
|
-
|
|
|
- newFlow.src_ip = ip_hdr->src;
|
|
|
- newFlow.dst_ip = ip_hdr->dst;
|
|
|
- newFlow.src_port = tcp_hdr->src_port;
|
|
|
- newFlow.dst_port = tcp_hdr->dst_port;
|
|
|
-
|
|
|
- newFlow.seq_num = tcp_hdr->sequence_num;
|
|
|
-
|
|
|
- if (p[0] == 0x16){ /* This is a TLS handshake */
|
|
|
- p += RECORD_HEADER_LEN;
|
|
|
- check_handshake(tcp_hdr, newFlow, p);
|
|
|
+ p += IP_HEADER_LEN(ip_hdr);//TODO: remove
|
|
|
+ tcp_hdr = (struct tcp_header*) p;//TODO: remove
|
|
|
+
|
|
|
+ /* Check to make sure it is an IP packet */
|
|
|
+ if( (ip_hdr->versionihl >>4) != 4)
|
|
|
+ goto end;
|
|
|
+
|
|
|
+ /* Packet may be split over multiple frames. In this case,
|
|
|
+ * reconstruct the packet before trying to read TLS records
|
|
|
+ */
|
|
|
+ if((htons(ip_hdr->flagsoff)&MF) || (htons(ip_hdr->flagsoff) &0x1fff) ){ /* this is a fragment */
|
|
|
+ printf("MF: %d, OFF: %d.\n", htons(ip_hdr->flagsoff)&MF, htons(ip_hdr->flagsoff)&0x1fff);
|
|
|
+ printf("Received packet fragment.\n");
|
|
|
+ printf("%d packet bytes: \n", IP_HEADER_LEN(ip_hdr));
|
|
|
+ for(int i=0; i< IP_HEADER_LEN(ip_hdr); i++){
|
|
|
+ printf("%02x ", p[i]);
|
|
|
}
|
|
|
- }
|
|
|
-
|
|
|
- if((index = check_flow(newFlow))){
|
|
|
- flow *observed = get_flow(index-1);
|
|
|
-
|
|
|
- /* Update TCP state */
|
|
|
- if(tcp_hdr->flags & (FIN | RST) ){
|
|
|
- /* Remove flow from table, connection ended */
|
|
|
- remove_flow(index);
|
|
|
- }
|
|
|
-
|
|
|
- /* Check to see if TLS finished message has passed */
|
|
|
- if(observed->tls_state & TLS_FINISHED){
|
|
|
- //decrypt packet?
|
|
|
- printf("TLS finished received.\n");
|
|
|
- } else {
|
|
|
- //check to see if tls_finished message
|
|
|
- if((observed->tls_state & TLS_NEW_SESS) && !observed->encrypted){
|
|
|
- //packet should be encrypted
|
|
|
- observed->encrypted = 1;
|
|
|
- } else if(observed->encrypted){ /* decrypt tls finished message */
|
|
|
- printf("need to decrypt this finished message\n");
|
|
|
- update_flow(index, 20);
|
|
|
- }
|
|
|
-
|
|
|
+ printf("\n");
|
|
|
+ u_char *complete_packet;
|
|
|
+ //save packet fragment
|
|
|
+ complete_packet = malloc(65535);
|
|
|
+
|
|
|
+ int success = report_fragment(p, complete_packet, header->len);
|
|
|
+ if(success){
|
|
|
+ process_packet(complete_packet);
|
|
|
}
|
|
|
+ /* TODO: handle errors */
|
|
|
+
|
|
|
+ } else { /*not a fragment, add to packet chain */
|
|
|
+ process_packet(packet);
|
|
|
+ }
|
|
|
+
|
|
|
+end:
|
|
|
|
|
|
- /* Hand packet to relay module */
|
|
|
- if((pcap_inject(handle, packet, header->len)) < 0 ){
|
|
|
- fprintf(stderr, "Error: %s\n", pcap_geterr(handle));
|
|
|
- }
|
|
|
- //handle_flow(handle, packet, header->len);
|
|
|
-
|
|
|
- } else {
|
|
|
-
|
|
|
- if((pcap_inject(handle, packet, header->len)) < 0 ){
|
|
|
- fprintf(stderr, "Error: %s\n", pcap_geterr(handle));
|
|
|
- }
|
|
|
+ /* Hand packet to relay module */
|
|
|
+ if((pcap_inject(handle, packet, header->len)) < 0 ){
|
|
|
+ fprintf(stderr, "Error: %s\n", pcap_geterr(handle));
|
|
|
}
|
|
|
|
|
|
pcap_close(handle);
|
|
@@ -257,6 +179,7 @@ int main(int argc, char *argv[]){
|
|
|
snprintf(filter2, 33, "ether dst host %s", macaddr);
|
|
|
|
|
|
init_flow_table();
|
|
|
+ init_fragment_table();
|
|
|
|
|
|
/* Create threads */
|
|
|
outbound.readdev = dev1;
|
|
@@ -320,3 +243,95 @@ void *sniff_packets(void *args){
|
|
|
|
|
|
return NULL;
|
|
|
}
|
|
|
+
|
|
|
+/* This function receives a full ip packet and then:
|
|
|
+ * 1) identifies the flow
|
|
|
+ * 2) adds the packet to the flow's data chain
|
|
|
+ * 3) updates the packet's state
|
|
|
+ */
|
|
|
+void process_packet(const u_char *packet){
|
|
|
+ unsigned char *p;
|
|
|
+ int index, i;
|
|
|
+
|
|
|
+ const struct ip_header *ip_hdr;
|
|
|
+ const struct tcp_header *tcp_hdr;
|
|
|
+ const struct record_header *record_hdr;
|
|
|
+
|
|
|
+ u_int size_ip;
|
|
|
+ u_int size_tcp;
|
|
|
+
|
|
|
+ flow newFlow;
|
|
|
+
|
|
|
+ p = (unsigned char *) packet;
|
|
|
+ p += ETHER_HEADER_LEN; //skip ethernet header
|
|
|
+ ip_hdr = (struct ip_header*) p;
|
|
|
+ size_ip = IP_HEADER_LEN(ip_hdr);
|
|
|
+
|
|
|
+ if (ip_hdr->proto == IPPROTO_TCP){
|
|
|
+ p += size_ip; //skip IP header
|
|
|
+
|
|
|
+ tcp_hdr = (struct tcp_header*) p;
|
|
|
+ size_tcp = TCP_HEADER_LEN(tcp_hdr);
|
|
|
+ p += size_tcp;
|
|
|
+
|
|
|
+ newFlow.src_ip = ip_hdr->src;
|
|
|
+ newFlow.dst_ip = ip_hdr->dst;
|
|
|
+ newFlow.src_port = tcp_hdr->src_port;
|
|
|
+ newFlow.dst_port = tcp_hdr->dst_port;
|
|
|
+
|
|
|
+ newFlow.seq_num = tcp_hdr->sequence_num;
|
|
|
+ record_hdr = (struct record_header*) p;
|
|
|
+
|
|
|
+ /* Checks to see if this is a possibly tagged hello msg */
|
|
|
+ if (record_hdr->type == HS){ /* This is a TLS handshake */
|
|
|
+ //printf("record type: %d.\n", record_hdr->type);
|
|
|
+ //printf("record version(major): %d.\n", (record_hdr->version&0xFF00)>>8);
|
|
|
+ //printf("record version(minor): %d.\n", record_hdr->version&0xFF);
|
|
|
+ //printf("record length: %d.\n", RECORD_LEN(record_hdr));
|
|
|
+ //printf("record header bytes:\n");
|
|
|
+ //for(i=0; i<RECORD_HEADER_LEN; i++){
|
|
|
+ // printf("%02x ", p[i]);
|
|
|
+ //}
|
|
|
+ //printf("\n");
|
|
|
+ p += RECORD_HEADER_LEN;
|
|
|
+ check_handshake(tcp_hdr, newFlow, p, RECORD_LEN(record_hdr));
|
|
|
+ //printf("Length of packet: %d.\n", htons(ip_hdr->len));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /* Now if flow is in table, update state */
|
|
|
+ if((index = check_flow(newFlow))){
|
|
|
+ flow *observed = get_flow(index-1);
|
|
|
+
|
|
|
+
|
|
|
+ /* Pass data to packet chain */
|
|
|
+ add_packet(observed, packet);
|
|
|
+
|
|
|
+ /* Update flow state */
|
|
|
+ if(observed->packet_chain != NULL){
|
|
|
+ update_flow(observed);
|
|
|
+ }
|
|
|
+
|
|
|
+ /* Update TCP state */
|
|
|
+ if(tcp_hdr->flags & (FIN | RST) ){
|
|
|
+ /* Remove flow from table, connection ended */
|
|
|
+ remove_flow(index);
|
|
|
+ }
|
|
|
+
|
|
|
+ /* Check to see if TLS finished message has passed */
|
|
|
+ if(observed->state == TLS_FINISHED){
|
|
|
+ //decrypt packet?
|
|
|
+ printf("TLS finished received.\n");
|
|
|
+ } else {
|
|
|
+ //check to see if tls_finished message
|
|
|
+ if((observed->state == TLS_NEW_SESS) && !observed->encrypted){
|
|
|
+ //packet should be encrypted
|
|
|
+ observed->encrypted = 1;
|
|
|
+ } else if(observed->encrypted){ /* decrypt tls finished message */
|
|
|
+ printf("need to decrypt this finished message\n");
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+}
|