-
Notifications
You must be signed in to change notification settings - Fork 2
/
pcap-change-ip.c
158 lines (145 loc) · 3.33 KB
/
pcap-change-ip.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*
* pcap-change-ip
*
* If either src/dst IP falls within the given network, the IP will be
* changed to a new value.
*/
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <pcap.h>
#include <err.h>
#include <string.h>
#include <time.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/ip6.h>
#include <arpa/inet.h>
#include "pcap_layers.h"
struct in_addr old4;
struct in_addr mask4;
struct in_addr to4;
struct in6_addr old6;
struct in6_addr mask6;
struct in6_addr to6;
int
match4(struct in_addr check)
{
if ((check.s_addr & mask4.s_addr) != old4.s_addr)
return 0;
return 1;
}
int
match6(struct in6_addr check)
{
int k;
for (k = 0; k < 16; k++)
if ((check.s6_addr[k] & mask6.s6_addr[k]) != old6.s6_addr[k])
return 0;
return 1;
}
/*
* this will only be called if 'ip' is a complete IPv header
*/
int
my_ip4_handler(const struct ip *ip4, int len, void *userdata)
{
if (match4(ip4->ip_src))
memcpy((void *) &ip4->ip_src, &to4, sizeof(to4));
if (match4(ip4->ip_dst))
memcpy((void *) &ip4->ip_dst, &to4, sizeof(to4));
return 0;
}
int
my_ip6_handler(const struct ip6_hdr *ip6, int len, void *userdata)
{
if (match6(ip6->ip6_src))
memcpy((void *) &ip6->ip6_src, &to6, sizeof(to6));
if (match6(ip6->ip6_dst))
memcpy((void *) &ip6->ip6_dst, &to6, sizeof(to6));
return 0;
}
void
usage(void)
{
fprintf(stderr, "usage: pcap-change-ip from-ip4/prefixlen new-ip4 from-ip6/prefixlen new-ip6\n");
exit(1);
}
int
main(int argc, char *argv[])
{
pcap_t *in = NULL;
pcap_dumper_t *out = NULL;
char errbuf[PCAP_ERRBUF_SIZE + 1];
struct pcap_pkthdr hdr;
const u_char *data;
char *t;
unsigned int ml;
unsigned int k;
if (argc < 5)
usage();
if (NULL == (t = strchr(argv[1], '/')))
usage();
*t = 0;
if (inet_pton(AF_INET, argv[1], &old4) != 1) {
fprintf(stderr, "bad IPv4 address: %s\n", argv[1]);
usage();
}
ml = atoi(t + 1);
if (ml > 32) {
fprintf(stderr, "IPv4 prefixlen (%u) out of range\n", ml);
usage();
}
mask4.s_addr = htonl(~0 << (32 - ml));
if (inet_pton(AF_INET, argv[2], &to4) != 1) {
fprintf(stderr, "bad IPv4 address: %s\n", argv[2]);
usage();
}
if (NULL == (t = strchr(argv[3], '/')))
usage();
*t = 0;
if (inet_pton(AF_INET6, argv[3], &old6) != 1) {
fprintf(stderr, "bad IPv6 address: %s\n", argv[3]);
exit(1);
}
ml = atoi(t + 1);
if (ml > 128) {
fprintf(stderr, "IPv6 prefixlen (%u) out of range\n", ml);
usage();
}
memset(&mask6, 0, sizeof(mask6));
for (k = 0; k < 16; k++, ml -= 8) {
if (ml >= 8) {
mask6.s6_addr[k] = 0xff;
} else {
mask6.s6_addr[k] = 0xff << (8 - ml);
break;
}
}
if (inet_pton(AF_INET6, argv[4], &to6) != 1) {
fprintf(stderr, "bad IPv6 address: %s\n", argv[4]);
usage();
}
in = pcap_open_offline("-", errbuf);
if (NULL == in) {
fprintf(stderr, "stdin: %s", errbuf);
exit(1);
}
out = pcap_dump_open(in, "-");
if (NULL == out) {
perror("stdout");
exit(1);
}
pcap_layers_init(pcap_datalink(in), 0);
callback_ipv4 = my_ip4_handler;
callback_ipv6 = my_ip6_handler;
while ((data = pcap_next(in, &hdr))) {
handle_pcap(NULL, &hdr, data);
pcap_dump((void *) out, &hdr, data);
}
pcap_close(in);
pcap_dump_close(out);
exit(0);
}