slitheen-proxy.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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. //Write key to file
  109. fp = fopen("sharedkey", "wb");
  110. if (fp == NULL) {
  111. perror("fopen");
  112. exit(1);
  113. }
  114. for(i=0; i<16;i++){
  115. fprintf(fp, "%02x", key[i]);
  116. }
  117. fclose(fp);
  118. /* Save flow in table */
  119. printf("saving flow..\n");
  120. add_flow(f);
  121. printf("saved\n");
  122. }
  123. } else {
  124. int index = check_flow(f);
  125. update_flow(index, code);
  126. }
  127. }
  128. void got_packet(u_char *args, const struct pcap_pkthdr *header, const u_char *packet){
  129. pcap_t *handle;
  130. char errbuf[BUFSIZ];
  131. char *writedev = (char *) args;
  132. unsigned char *p;
  133. int index;
  134. const struct ip_header *ip_hdr;
  135. const struct tcp_header *tcp_hdr;
  136. u_int size_ip;
  137. u_int size_tcp;
  138. flow newFlow;
  139. handle = pcap_open_live(writedev, BUFSIZ, 1, 1000, errbuf);
  140. if (handle == NULL){
  141. fprintf(stderr, "Couldn't open device %s: %s\n", writedev, errbuf);
  142. }
  143. /* check for clientHello */
  144. p = (unsigned char *) packet;
  145. p += ETHER_HEADER_LEN; //skip ethernet header
  146. ip_hdr = (struct ip_header*) p;
  147. size_ip = IP_HEADER_LEN(ip_hdr);
  148. if (ip_hdr->proto == IPPROTO_TCP){
  149. p += size_ip; //skip IP header
  150. tcp_hdr = (struct tcp_header*) p;
  151. size_tcp = TCP_HEADER_LEN(tcp_hdr);
  152. p += size_tcp;
  153. newFlow.src_ip = ip_hdr->src;
  154. newFlow.dst_ip = ip_hdr->dst;
  155. newFlow.src_port = tcp_hdr->src_port;
  156. newFlow.dst_port = tcp_hdr->dst_port;
  157. newFlow.seq_num = tcp_hdr->sequence_num;
  158. if (p[0] == 0x16){ /* This is a TLS handshake */
  159. p += RECORD_HEADER_LEN;
  160. check_handshake(tcp_hdr, newFlow, p);
  161. }
  162. }
  163. if((index = check_flow(newFlow))){
  164. flow *observed = get_flow(index-1);
  165. /* Update TCP state */
  166. if(tcp_hdr->flags & (FIN | RST) ){
  167. /* Remove flow from table, connection ended */
  168. remove_flow(index);
  169. }
  170. /* Check to see if TLS finished message has passed */
  171. if(observed->tls_state & TLS_FINISHED){
  172. //decrypt packet?
  173. printf("TLS finished received.\n");
  174. } else {
  175. //check to see if tls_finished message
  176. if((observed->tls_state & TLS_NEW_SESS) && !observed->encrypted){
  177. //packet should be encrypted
  178. observed->encrypted = 1;
  179. } else if(observed->encrypted){ /* decrypt tls finished message */
  180. printf("need to decrypt this finished message\n");
  181. update_flow(index, 20);
  182. }
  183. }
  184. /* Hand packet to relay module */
  185. if((pcap_inject(handle, packet, header->len)) < 0 ){
  186. fprintf(stderr, "Error: %s\n", pcap_geterr(handle));
  187. }
  188. //handle_flow(handle, packet, header->len);
  189. } else {
  190. if((pcap_inject(handle, packet, header->len)) < 0 ){
  191. fprintf(stderr, "Error: %s\n", pcap_geterr(handle));
  192. }
  193. }
  194. pcap_close(handle);
  195. }
  196. void usage(void){
  197. printf("Usage: slitheen-proxy [internal network interface] [NAT interface]\n");
  198. }
  199. int main(int argc, char *argv[]){
  200. pthread_t t1, t2;
  201. char filter1[33] = "ether src host 08:00:27:e8:9d:d4";
  202. char filter2[33] = "ether src host 08:00:27:e8:9d:d4";
  203. char *dev1 = NULL; /* Device that leads to the internal network */
  204. char *dev2 = NULL; /* Device that leads out to the world */
  205. struct sniff_args outbound;
  206. struct sniff_args inbound;
  207. if (argc != 3) {
  208. usage();
  209. return(2);
  210. }
  211. dev1 = argv[1];
  212. dev2 = argv[2];
  213. snprintf(filter1, 33, "ether src host %s", macaddr);
  214. snprintf(filter2, 33, "ether dst host %s", macaddr);
  215. init_flow_table();
  216. /* Create threads */
  217. outbound.readdev = dev1;
  218. outbound.writedev = dev2;
  219. outbound.filter = filter1;
  220. inbound.readdev = dev2;
  221. inbound.writedev = dev1;
  222. inbound.filter = filter2;
  223. pthread_create(&t1, NULL, sniff_packets, (void *) &outbound);
  224. pthread_create(&t2, NULL, sniff_packets, (void *) &inbound);
  225. pthread_join(t1, NULL);
  226. pthread_join(t2, NULL);
  227. return(0);
  228. }
  229. void *sniff_packets(void *args){
  230. pcap_t *handle;
  231. char errbuf[BUFSIZ];
  232. struct bpf_program fp;
  233. bpf_u_int32 mask;
  234. bpf_u_int32 net;
  235. char *readdev, *writedev, *filter;
  236. struct sniff_args *arg_st = (struct sniff_args *) args;
  237. readdev = arg_st->readdev;
  238. writedev = arg_st->writedev;
  239. filter = arg_st->filter;
  240. if (pcap_lookupnet(readdev, &net, &mask, errbuf) == -1){
  241. fprintf(stderr, "Can't get netmask for device %s\n", readdev);
  242. exit(2);
  243. }
  244. handle = pcap_open_live(readdev, BUFSIZ, 1, 1000, errbuf);
  245. if (handle == NULL){
  246. fprintf(stderr, "Couldn't open device %s: %s\n", readdev, errbuf);
  247. }
  248. if(pcap_datalink(handle) != DLT_EN10MB) {
  249. fprintf(stderr, "Device %s does not provide Ethernet headers - not supported\n", readdev);
  250. exit(2);
  251. }
  252. if(pcap_compile(handle, &fp, filter, 0 , net) == -1){
  253. fprintf(stderr, "Couldn't parse filter %s: %s\n", filter, pcap_geterr(handle));
  254. exit(2);
  255. }
  256. if (pcap_setfilter(handle, &fp) == -1) {
  257. fprintf(stderr, "Couldn't install filter %s: %s\n", filter, pcap_geterr(handle));
  258. exit(2);
  259. }
  260. /*callback function*/
  261. pcap_loop(handle, -1, got_packet, (unsigned char *) writedev);
  262. /*Sniff a packet*/
  263. pcap_close(handle);
  264. return NULL;
  265. }