1 | #define _GNU_SOURCE |
---|
2 | #include "libtrace.h" |
---|
3 | #include <stdio.h> |
---|
4 | #include <unistd.h> |
---|
5 | #include <stdlib.h> |
---|
6 | #include <getopt.h> |
---|
7 | #include <stdbool.h> |
---|
8 | #include <stddef.h> |
---|
9 | #include <string.h> |
---|
10 | #include <pcap.h> |
---|
11 | #include <time.h> |
---|
12 | #include "ipenc.h" |
---|
13 | |
---|
14 | |
---|
15 | void usage(char *argv0) |
---|
16 | { |
---|
17 | fprintf(stderr,"Usage:\n" |
---|
18 | "%s flags inputfile outputfile\n" |
---|
19 | "-s --encrypt-source Encrypt the source addresses\n" |
---|
20 | "-d --encrypt-dest Encrypt the destination addresses\n" |
---|
21 | "-c --cryptopan=key Encrypt the addresses with the cryptopan\n" |
---|
22 | " prefix preserving\n" |
---|
23 | "-p --prefix=C.I.D.R/bits Substitute the prefix of the address\n" |
---|
24 | ,argv0); |
---|
25 | exit(1); |
---|
26 | } |
---|
27 | |
---|
28 | // Incrementally update a checksum |
---|
29 | void update_in_cksum(uint16_t *csum, uint16_t old, uint16_t new) |
---|
30 | { |
---|
31 | uint32_t sum = (~htons(*csum) & 0xFFFF) |
---|
32 | + (~htons(old) & 0xFFFF) |
---|
33 | + htons(new); |
---|
34 | sum = (sum & 0xFFFF) + (sum >> 16); |
---|
35 | *csum = htons(~(sum + (sum >> 16))); |
---|
36 | } |
---|
37 | |
---|
38 | void update_in_cksum32(uint16_t *csum, uint32_t old, uint32_t new) |
---|
39 | { |
---|
40 | update_in_cksum(csum,old>>16,new>>16); |
---|
41 | update_in_cksum(csum,old&0xFFFF,new&0xFFFF); |
---|
42 | } |
---|
43 | |
---|
44 | /* Ok this is remarkably complicated |
---|
45 | * |
---|
46 | * We want to change one, or the other IP address, while preserving the |
---|
47 | * checksum. TCP and UDP both include the faux header in their checksum |
---|
48 | * calculations, so you have to update them too. ICMP is even worse -- |
---|
49 | * it can include the original IP packet that caused the error! So anonymise |
---|
50 | * that too, but remember that it's travelling in the opposite direction so |
---|
51 | * we need to encrypt the destination and source instead of the source and |
---|
52 | * destination! |
---|
53 | */ |
---|
54 | void encrypt_ips(struct libtrace_ip *ip,bool enc_source,bool enc_dest) |
---|
55 | { |
---|
56 | struct libtrace_tcp *tcp; |
---|
57 | struct libtrace_udp *udp; |
---|
58 | struct libtrace_icmp *icmp; |
---|
59 | |
---|
60 | tcp=trace_get_tcp_from_ip(ip,NULL); |
---|
61 | udp=trace_get_udp_from_ip(ip,NULL); |
---|
62 | icmp=trace_get_icmp_from_ip(ip,NULL); |
---|
63 | |
---|
64 | if (enc_source) { |
---|
65 | uint32_t old_ip=ip->ip_src.s_addr; |
---|
66 | uint32_t new_ip=htonl(enc_ip( |
---|
67 | htonl(ip->ip_src.s_addr) |
---|
68 | )); |
---|
69 | update_in_cksum32(&ip->ip_sum,old_ip,new_ip); |
---|
70 | if (tcp) update_in_cksum(&tcp->check,old_ip,new_ip); |
---|
71 | if (udp) update_in_cksum(&udp->check,old_ip,new_ip); |
---|
72 | ip->ip_src.s_addr = new_ip; |
---|
73 | } |
---|
74 | |
---|
75 | if (enc_dest) { |
---|
76 | uint32_t old_ip=ip->ip_dst.s_addr; |
---|
77 | uint32_t new_ip=htonl(enc_ip( |
---|
78 | htonl(ip->ip_dst.s_addr) |
---|
79 | )); |
---|
80 | update_in_cksum32(&ip->ip_sum,old_ip,new_ip); |
---|
81 | if (tcp) update_in_cksum(&tcp->check,old_ip,new_ip); |
---|
82 | if (udp) update_in_cksum(&udp->check,old_ip,new_ip); |
---|
83 | ip->ip_dst.s_addr = new_ip; |
---|
84 | } |
---|
85 | |
---|
86 | if (icmp) { |
---|
87 | /* These are error codes that return the IP packet internally */ |
---|
88 | if (icmp->type == 3 || icmp->type == 5 || icmp->type == 11) { |
---|
89 | encrypt_ips( |
---|
90 | (struct libtrace_ip*)icmp+ |
---|
91 | sizeof(struct libtrace_icmp), |
---|
92 | enc_dest, |
---|
93 | enc_source); |
---|
94 | } |
---|
95 | } |
---|
96 | } |
---|
97 | |
---|
98 | int main(int argc, char *argv[]) |
---|
99 | { |
---|
100 | enum enc_type_t enc_type = ENC_NONE; |
---|
101 | char *key = NULL; |
---|
102 | struct libtrace_t *trace = 0; |
---|
103 | struct libtrace_packet_t packet; |
---|
104 | struct libtrace_out_t *writer = 0; |
---|
105 | bool enc_source = false; |
---|
106 | bool enc_dest = false; |
---|
107 | char *output = 0; |
---|
108 | |
---|
109 | if (argc<2) |
---|
110 | usage(argv[0]); |
---|
111 | |
---|
112 | while (1) { |
---|
113 | int option_index; |
---|
114 | struct option long_options[] = { |
---|
115 | { "encrypt-source", 0, 0, 's' }, |
---|
116 | { "encrypt-dest", 0, 0, 'd' }, |
---|
117 | { "cryptopan", 1, 0, 'c' }, |
---|
118 | { "prefix", 1, 0, 'p' }, |
---|
119 | { NULL, 0, 0, 0 }, |
---|
120 | }; |
---|
121 | |
---|
122 | int c=getopt_long(argc, argv, "sc:dp:", |
---|
123 | long_options, &option_index); |
---|
124 | |
---|
125 | if (c==-1) |
---|
126 | break; |
---|
127 | |
---|
128 | switch (c) { |
---|
129 | case 's': enc_source=true; break; |
---|
130 | case 'd': enc_dest =true; break; |
---|
131 | case 'c': |
---|
132 | if (key!=NULL) { |
---|
133 | fprintf(stderr,"You can only have one encryption type and one key\n"); |
---|
134 | usage(argv[0]); |
---|
135 | } |
---|
136 | key=strdup(optarg); |
---|
137 | enc_type = ENC_CRYPTOPAN; |
---|
138 | break; |
---|
139 | case 'p': |
---|
140 | if (key!=NULL) { |
---|
141 | fprintf(stderr,"You can only have one encryption type and one key\n"); |
---|
142 | usage(argv[0]); |
---|
143 | } |
---|
144 | key=strdup(optarg); |
---|
145 | enc_type = ENC_PREFIX_SUBSTITUTION; |
---|
146 | break; |
---|
147 | default: |
---|
148 | fprintf(stderr,"unknown option: %c\n",c); |
---|
149 | usage(argv[0]); |
---|
150 | |
---|
151 | } |
---|
152 | |
---|
153 | } |
---|
154 | |
---|
155 | enc_init(enc_type,key); |
---|
156 | |
---|
157 | // open input uri |
---|
158 | trace = trace_create(argv[optind]); |
---|
159 | if (!trace) { |
---|
160 | fprintf(stderr,"Cannot open %s\n",argv[optind]); |
---|
161 | trace_perror(argv[optind]); |
---|
162 | return 1; |
---|
163 | } |
---|
164 | |
---|
165 | if (optind == argc) { |
---|
166 | // no output specified, output in same format to stdout |
---|
167 | asprintf(&output,"%s:-","erf"); |
---|
168 | writer = trace_output_create(output); |
---|
169 | } else { |
---|
170 | writer = trace_output_create(argv[optind +1]); |
---|
171 | } |
---|
172 | if (!writer) { |
---|
173 | trace_perror("trace_output_create"); |
---|
174 | return 1; |
---|
175 | } |
---|
176 | |
---|
177 | |
---|
178 | for(;;) { |
---|
179 | struct libtrace_ip *ipptr; |
---|
180 | int psize; |
---|
181 | if ((psize = trace_read_packet(trace, &packet)) <= 0) { |
---|
182 | break; |
---|
183 | } |
---|
184 | |
---|
185 | ipptr = trace_get_ip(&packet); |
---|
186 | |
---|
187 | if (ipptr && (enc_source || enc_dest)) |
---|
188 | encrypt_ips(ipptr,enc_source,enc_dest); |
---|
189 | |
---|
190 | /* TODO: Encrypt IP's in ARP packets */ |
---|
191 | |
---|
192 | trace_write_packet(writer,&packet); |
---|
193 | } |
---|
194 | return 0; |
---|
195 | } |
---|