slitheen-proxy.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. #include <pcap.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <unistd.h>
  5. #include <string.h>
  6. #include <pthread.h>
  7. #include "ptwist.h"
  8. #include "rserv.h"
  9. #include "flow.h"
  10. #define macaddr "08:00:27:e8:9d:d4"
  11. /* Ethernet addresses are 6 bytes */
  12. #define ETHER_ADDR_LEN 6
  13. #define ETHER_HEADER_LEN 2*ETHER_ADDR_LEN + 2
  14. /* Definitions for parsing packet data */
  15. struct ip_header {
  16. u_char versionihl; /* Version >> 4 | IHL & 0x0f */
  17. u_char dscpecn; /* DSCP >> 2 | ECN & 0x03 */
  18. u_short len; /* Total Length */
  19. u_short id; /* Identification */
  20. u_short flagsoff; /* Flags >> 13 | Fragment Offset & 0x1fff */
  21. #define RF 0x8000 /* Reserved; must be zero */
  22. #define DF 0x4000 /* Dont Fragment */
  23. #define MF 0x2000 /* More Fragments */
  24. u_char ttl; /* Time To Live */
  25. u_char proto; /* Protocol */
  26. u_short chksum; /* Header Checksum */
  27. struct in_addr src, dst; /* Source and Destination addresses */
  28. };
  29. #define IP_HEADER_LEN(ip) (((ip)->versionihl) & 0x0f)*4
  30. struct tcp_header {
  31. u_short src_port; /* source port */
  32. u_short dst_port; /* destination port */
  33. u_int sequence_num; /* sequence number */
  34. u_int ack_num; /* acknowledgement number */
  35. u_char offset_res_ns; /*Data offset >> 4 | res >> 1 | NS 0x01 */
  36. u_char flags; /* Flags */
  37. #define FIN 0x01
  38. #define RST 0x04
  39. u_short win_size; /* Window size*/
  40. u_short chksum; /* Checksum */
  41. u_short urg; /* Urgent pointer */
  42. };
  43. #define TCP_HEADER_LEN(tcp) (((tcp)->offset_res_ns) >> 4)*4
  44. struct tls_header {
  45. u_char type; /* Content Type */
  46. #define CCS 0x14
  47. #define A 0x15
  48. #define HS 0x16
  49. #define APP 0x17
  50. #define HB 0x18
  51. u_short version; /* Version */
  52. u_short len; /* Length */
  53. u_char msg; /* Message Type */
  54. #define CLIENT_HELLO 0x01
  55. #define FINISHED 0x14
  56. };
  57. #define RECORD_HEADER_LEN 5
  58. #define CLIENT_HELLO_HEADER_LEN 6
  59. struct sniff_args {
  60. char *readdev;
  61. char *writedev;
  62. char *filter;
  63. };
  64. void got_packet(u_char *args, const struct pcap_pkthdr *header, const u_char *packet);
  65. void check_handshake(const struct tcp_header *tcp_hdr, flow f, unsigned char *p);
  66. void *sniff_packets(void *);
  67. void check_handshake(const struct tcp_header *tcp_hdr, flow f, unsigned char *ptr){
  68. FILE *fp;
  69. int res, i, code;
  70. unsigned char *p;
  71. byte privkey[PTWIST_BYTES];
  72. byte key[16];
  73. p = ptr;
  74. code = p[0];
  75. printf("handshake code: %d\n", code);
  76. if (code == 0x01){
  77. p += CLIENT_HELLO_HEADER_LEN;
  78. //now pointing to hello random :D
  79. p += 4; //skipping time bytes
  80. /* Load the private key */
  81. fp = fopen("privkey", "rb");
  82. if (fp == NULL) {
  83. perror("fopen");
  84. exit(1);
  85. }
  86. res = fread(privkey, PTWIST_BYTES, 1, fp);
  87. if (res < 1) {
  88. perror("fread");
  89. exit(1);
  90. }
  91. fclose(fp);
  92. /* check tag*/
  93. res = check_tag(key, privkey, p, (const byte *)"context", 7);
  94. if (res) {
  95. printf("Untagged\n");
  96. } else {
  97. fp = fopen("tags", "wb");
  98. if (fp == NULL) {
  99. perror("fopen");
  100. exit(1);
  101. }
  102. //Write tag to file
  103. for(i=0; i< 28; i++){
  104. fprintf(fp, "%02x ", p[i]);
  105. }
  106. fclose(fp);
  107. strncpy((char *) f.key, (char *) key, 16);
  108. /* Save flow in table */
  109. printf("saving flow..\n");
  110. add_flow(f);
  111. printf("saved\n");
  112. }
  113. } else {
  114. int index = check_flow(f);
  115. update_flow(index, code);
  116. }
  117. }
  118. void got_packet(u_char *args, const struct pcap_pkthdr *header, const u_char *packet){
  119. pcap_t *handle;
  120. char errbuf[BUFSIZ];
  121. char *writedev = (char *) args;
  122. unsigned char *p;
  123. int index;
  124. const struct ip_header *ip_hdr;
  125. const struct tcp_header *tcp_hdr;
  126. u_int size_ip;
  127. u_int size_tcp;
  128. flow newFlow;
  129. handle = pcap_open_live(writedev, BUFSIZ, 1, 1000, errbuf);
  130. if (handle == NULL){
  131. fprintf(stderr, "Couldn't open device %s: %s\n", writedev, errbuf);
  132. }
  133. /* check for clientHello */
  134. p = (unsigned char *) packet;
  135. p += ETHER_HEADER_LEN; //skip ethernet header
  136. ip_hdr = (struct ip_header*) p;
  137. size_ip = IP_HEADER_LEN(ip_hdr);
  138. if (ip_hdr->proto == IPPROTO_TCP){
  139. p += size_ip; //skip IP header
  140. tcp_hdr = (struct tcp_header*) p;
  141. size_tcp = TCP_HEADER_LEN(tcp_hdr);
  142. p += size_tcp;
  143. newFlow.src_ip = ip_hdr->src;
  144. newFlow.dst_ip = ip_hdr->dst;
  145. newFlow.src_port = tcp_hdr->src_port;
  146. newFlow.dst_port = tcp_hdr->dst_port;
  147. newFlow.seq_num = tcp_hdr->sequence_num;
  148. if (p[0] == 0x16){ /* This is a TLS handshake */
  149. p += RECORD_HEADER_LEN;
  150. check_handshake(tcp_hdr, newFlow, p);
  151. }
  152. }
  153. if((index = check_flow(newFlow))){
  154. flow *observed = get_flow(index-1);
  155. /* Update TCP state */
  156. if(tcp_hdr->flags & (FIN | RST) ){
  157. /* Remove flow from table, connection ended */
  158. remove_flow(index);
  159. }
  160. /* Check to see if TLS finished message has passed */
  161. if(observed->tls_state & TLS_FINISHED){
  162. //decrypt packet?
  163. printf("TLS finished received.\n");
  164. } else {
  165. //check to see if tls_finished message
  166. if((observed->tls_state & TLS_NEW_SESS) && !observed->encrypted){
  167. //packet should be encrypted
  168. observed->encrypted = 1;
  169. } else if(observed->encrypted){ /* decrypt tls finished message */
  170. printf("need to decrypt this finished message\n");
  171. update_flow(index, 20);
  172. }
  173. }
  174. /* Hand packet to relay module */
  175. if((pcap_inject(handle, packet, header->len)) < 0 ){
  176. fprintf(stderr, "Error: %s\n", pcap_geterr(handle));
  177. }
  178. //handle_flow(handle, packet, header->len);
  179. } else {
  180. if((pcap_inject(handle, packet, header->len)) < 0 ){
  181. fprintf(stderr, "Error: %s\n", pcap_geterr(handle));
  182. }
  183. }
  184. pcap_close(handle);
  185. }
  186. void usage(void){
  187. printf("Usage: slitheen-proxy [internal network interface] [NAT interface]\n");
  188. }
  189. int main(int argc, char *argv[]){
  190. pthread_t t1, t2;
  191. char filter1[33] = "ether src host 08:00:27:e8:9d:d4";
  192. char filter2[33] = "ether src host 08:00:27:e8:9d:d4";
  193. char *dev1 = NULL; /* Device that leads to the internal network */
  194. char *dev2 = NULL; /* Device that leads out to the world */
  195. struct sniff_args outbound;
  196. struct sniff_args inbound;
  197. if (argc != 3) {
  198. usage();
  199. return(2);
  200. }
  201. dev1 = argv[1];
  202. dev2 = argv[2];
  203. snprintf(filter1, 33, "ether src host %s", macaddr);
  204. snprintf(filter2, 33, "ether dst host %s", macaddr);
  205. init_flow_table();
  206. /* Create threads */
  207. outbound.readdev = dev1;
  208. outbound.writedev = dev2;
  209. outbound.filter = filter1;
  210. inbound.readdev = dev2;
  211. inbound.writedev = dev1;
  212. inbound.filter = filter2;
  213. pthread_create(&t1, NULL, sniff_packets, (void *) &outbound);
  214. pthread_create(&t2, NULL, sniff_packets, (void *) &inbound);
  215. pthread_join(t1, NULL);
  216. pthread_join(t2, NULL);
  217. return(0);
  218. }
  219. void *sniff_packets(void *args){
  220. pcap_t *handle;
  221. char errbuf[BUFSIZ];
  222. struct bpf_program fp;
  223. bpf_u_int32 mask;
  224. bpf_u_int32 net;
  225. char *readdev, *writedev, *filter;
  226. struct sniff_args *arg_st = (struct sniff_args *) args;
  227. readdev = arg_st->readdev;
  228. writedev = arg_st->writedev;
  229. filter = arg_st->filter;
  230. if (pcap_lookupnet(readdev, &net, &mask, errbuf) == -1){
  231. fprintf(stderr, "Can't get netmask for device %s\n", readdev);
  232. exit(2);
  233. }
  234. handle = pcap_open_live(readdev, BUFSIZ, 1, 1000, errbuf);
  235. if (handle == NULL){
  236. fprintf(stderr, "Couldn't open device %s: %s\n", readdev, errbuf);
  237. }
  238. if(pcap_datalink(handle) != DLT_EN10MB) {
  239. fprintf(stderr, "Device %s does not provide Ethernet headers - not supported\n", readdev);
  240. exit(2);
  241. }
  242. if(pcap_compile(handle, &fp, filter, 0 , net) == -1){
  243. fprintf(stderr, "Couldn't parse filter %s: %s\n", filter, pcap_geterr(handle));
  244. exit(2);
  245. }
  246. if (pcap_setfilter(handle, &fp) == -1) {
  247. fprintf(stderr, "Couldn't install filter %s: %s\n", filter, pcap_geterr(handle));
  248. exit(2);
  249. }
  250. /*callback function*/
  251. pcap_loop(handle, -1, got_packet, (unsigned char *) writedev);
  252. /*Sniff a packet*/
  253. pcap_close(handle);
  254. return NULL;
  255. }