-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshroud.c
288 lines (228 loc) · 9.65 KB
/
shroud.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
/*
Shroud - Proof of concept of a false-positive-based anti SYN scan
Copyright (C) 2017 Francesco Marano (@mrnfrancesco)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <libnet.h>
#include <pcap/pcap.h>
#include <inttypes.h>
#include <err.h>
#ifndef __GNUC__
# define __attribute__(x)
#endif
struct packet_handler_args {
libnet_t *l;
libnet_ptag_t ipv4_tag;
libnet_ptag_t tcp_tag;
};
static void packet_handler(u_char *user_args, const struct pcap_pkthdr *cap_header, const u_char *packet);
static void exit_with_error(pcap_t *pcap_handle, libnet_t *l, const char *err_buff) __attribute__((noreturn));
static int get_tcp_open_ports(uint16_t *port_lst);
int main(void) {
const char * const device = "lo";
#ifdef DEBUG
printf("Using device: \"%s\"\n", device);
#endif
int port_cnt;
uint16_t port_lst[65535] = {0};
if ((port_cnt = get_tcp_open_ports(port_lst)) == -1) {
exit_with_error(NULL, NULL, "Unable to get open ports");
}
#ifdef DEBUG
printf("Discovered open ports (%d): [ ", port_cnt);
for (int i = 0; i < port_cnt; ++i) {
printf("%hu ", port_lst[i]);
}
printf("]\n");
#endif
/* Init libnet with raw IPv4 socket */
char libnet_err_buff[LIBNET_ERRBUF_SIZE];
libnet_t *l = libnet_init(LIBNET_RAW4, device, libnet_err_buff);
if (l == NULL) {
exit_with_error(NULL, NULL, libnet_err_buff);
}
/* Enable device to sniff traffic */
char pcap_err_buff[PCAP_ERRBUF_SIZE] = { 0 };
pcap_t *pcap_handle = pcap_open_live(
libnet_getdevice(l), /* device name */
1500, /* snapshot length */
0, /* non-promiscuous mode */
2000, /* read timeout (ms) */
pcap_err_buff /* error buffer */
);
if (pcap_handle == NULL) {
exit_with_error(NULL, l, pcap_err_buff);
}
if (strlen(pcap_err_buff) != 0) {
fprintf(stderr, "[WARN] pcap: %s\n", pcap_err_buff);
}
/* Finds the IPv4 network number and netmask */
bpf_u_int32 netp, maskp; /* netmask and ip address */
if (pcap_lookupnet(device, &netp, &maskp, pcap_err_buff) == -1) {
exit_with_error(pcap_handle, l, pcap_err_buff);
}
/* Compiles filter expression to speed-up execution */
struct bpf_program fp;
const char * const base_filter = ""
"(not src and dst host 0.0.0.0) and "
"(tcp[tcpflags] & tcp-syn != 0) and "
"(tcp[tcpflags] & tcp-ack = 0)";
const char * const port_exclude_fmt = " and (not dst port %h"PRIu16")";
char * filter = calloc(strlen(base_filter) + (port_cnt * 26), sizeof(char));
if (filter == NULL) {
exit_with_error(pcap_handle, l, "Cannot allocate enough memory for filter string");
}
strncpy(filter, base_filter, strlen(base_filter));
for(int i = 0; i < port_cnt; ++i) {
snprintf(filter + strlen(filter), 26, port_exclude_fmt, port_lst[i]);
}
#ifdef DEBUG
printf("Applying filter: \"%s\"\n", filter);
#endif
if (pcap_compile(pcap_handle, &fp, filter, 1, maskp) == -1) {
exit_with_error(pcap_handle, l, pcap_geterr(pcap_handle));
}
/* Applies filter expression */
if (pcap_setfilter(pcap_handle, &fp) == -1) {
exit_with_error(pcap_handle, l, pcap_geterr(pcap_handle));
}
/* Free BPF program and filter */
pcap_freecode(&fp);
/* Seeds the pseudo-random number generator */
if (libnet_seed_prand(l) == -1) {
exit_with_error(pcap_handle, l, "Cannot seeds the pseudo-random number generator");
}
struct packet_handler_args args = {
.l = l,
.ipv4_tag = 0,
.tcp_tag = 0
};
#ifdef DEBUG
puts("Waiting for incoming SYN packets...\n");
#endif
/* Process packets from a filtered live capture */
if (pcap_loop(pcap_handle, 0, packet_handler, (u_char *) &args) == -1) {
exit_with_error(pcap_handle, l, pcap_geterr(pcap_handle));
}
/* Shuts down the libnet session */
pcap_close(pcap_handle);
libnet_destroy(l);
exit(EXIT_SUCCESS);
}
static void packet_handler(u_char *user_args, const struct pcap_pkthdr *cap_header, const u_char *packet) {
struct packet_handler_args *args = (struct packet_handler_args *) user_args;
libnet_t *l = args->l;
struct libnet_ipv4_hdr *ip = (struct libnet_ipv4_hdr *)(packet + LIBNET_ETH_H);
struct libnet_tcp_hdr *tcp = (struct libnet_tcp_hdr *)((u_char *)ip + (ip->ip_hl << 2));
#ifdef DEBUG
printf(
"%s:%"PRIu16" > %s:%"PRIu16"\t[seq: %"PRIu32"\tack: %"PRIu32"]\n",
libnet_addr2name4(ip->ip_src.s_addr, LIBNET_DONT_RESOLVE),
ntohs(tcp->th_sport),
libnet_addr2name4(ip->ip_dst.s_addr, LIBNET_DONT_RESOLVE),
ntohs(tcp->th_dport),
ntohl(tcp->th_seq), ntohl(tcp->th_ack)
);
#endif
/* Build SYN-ACK response */
libnet_ptag_t tcp_tag, ipv4_tag;
tcp_tag = libnet_build_tcp(
htons(tcp->th_dport), /* source port */
htons(tcp->th_sport), /* destination port */
libnet_get_prand(LIBNET_PRu32), /* sequence number */
htonl((tcp->th_seq) + 1), /* acknowledgement number */
TH_SYN | TH_ACK, /* control flags */
(uint16_t) libnet_get_prand(LIBNET_PRu16), /* window size */
0, /* checksum */
0, /* urgent pointer */
LIBNET_TCP_H, /* total length of the TCP packet */
NULL, /* payload */
0, /* payload length */
l, /* pointer to a libnet context */
args->tcp_tag /* protocol tag */
);
if (tcp_tag == -1) {
fprintf(stderr, "[ERR] Unable to build TCP header: %s\n", libnet_geterror(l));
} else {
args->tcp_tag = tcp_tag;
}
ipv4_tag = libnet_build_ipv4(
LIBNET_IPV4_H + LIBNET_TCP_H, /* total length of the IP packet */
0, /* type of service bits */
(uint16_t) libnet_get_prand(LIBNET_PRu16), /* IP identification number */
0, /* fragmentation bits and offset */
(uint8_t) libnet_get_prand(LIBNET_PR8), /* time to live in the network */
IPPROTO_TCP, /* upper layer protocol */
0, /* checksum */
ip->ip_dst.s_addr, /* source IPv4 address */
ip->ip_src.s_addr, /* destination IPv4 address */
NULL, /* payload */
0, /* payload length */
l, /* pointer to a libnet context */
args->ipv4_tag /* protocol tag */
);
if (ipv4_tag == -1) {
fprintf(stderr, "[ERR] Unable to build IPv4 header: %s\n", libnet_geterror(l));
} else {
args->ipv4_tag = ipv4_tag;
}
if (libnet_write(l) == -1) {
fprintf(stderr, "[ERR] Unable to send packet: %s\n", libnet_geterror(l));
}
}
static void exit_with_error(pcap_t *pcap_handle, libnet_t *l, const char *err_buff) {
if (pcap_handle != NULL) {
pcap_close(pcap_handle);
}
if (l != NULL) {
libnet_destroy(l);
}
errx(EXIT_FAILURE, "%s", (err_buff != NULL && strlen(err_buff) != 0) ? err_buff : "Unknown error");
}
static int get_tcp_open_ports(uint16_t *port_lst) {
FILE *fp = NULL;
if ((fp = fopen("/proc/net/tcp", "r")) == NULL) {
return EXIT_FAILURE;
}
/*
* Line format example is the following:
* sl local_address rem_address st tx_queue rx_queue tr tm->when retrnsmt uid timeout inode
* 0: 0100007F:1B1E 00000000:0000 0A 00000000:00000000 00:00000000 00000000 1000 0 30575 1 ffff88021a2c8000 99 0 0 10 0
*/
char line[256];
/* Skip header */
if (fgets(line, sizeof(line), fp) == NULL) {
return EXIT_FAILURE;
}
uint16_t port_cnt = 0;
while (fgets(line, sizeof(line), fp) != NULL) {
size_t n = strlen(line);
if (n == 0 || line[n-1] != '\n') {
return EXIT_FAILURE;
}
line[n-1] = 0;
char *tmp = NULL;
uint16_t lport, rport;
/* Ignore first colon */
if ((tmp = strchr(line, ':')) == NULL) { continue; }
/* Retrieve local port */
if ((tmp = strchr(tmp + 2, ':')) == NULL) { continue; }
sscanf(tmp + 1, "%hx", &lport);
/* Retrieve remote port */
if ((tmp = strchr(tmp + 2, ':')) == NULL) { continue; }
sscanf(tmp + 1, "%hx", &rport);
if (rport == 0) {
port_lst[port_cnt++] = lport;
}
}
return port_cnt;
}