-
Notifications
You must be signed in to change notification settings - Fork 7
/
wntidect.c
291 lines (229 loc) · 7.51 KB
/
wntidect.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
//******************************************************************************
//
// *** WINNTI session setup detector ***
//
// This program detects initial connection attempts from WINNTI malware.
//
// AUTHOR: Stefan Ruester
// DATE: 2016-07-05
// TLP: WHITE
//
//******************************************************************************
/* #############################################################################
### INCLUDES
########################################################################## */
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/in_systm.h>
#include <arpa/inet.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <inttypes.h>
#include <unistd.h>
#include <time.h>
#include <sys/time.h>
#include <syslog.h>
#include "nids.h"
/* #############################################################################
### MACROS
########################################################################## */
#define WINTIDECT_VERISON "1.6"
#define int_ntoa(x) inet_ntoa(*((struct in_addr *)&x))
/* #############################################################################
### ENUMS
########################################################################## */
enum OP_MODE {
MODE_UNDEFINED,
MODE_READ_PCAP,
MODE_LIVE_INTERFACE
};
/* #############################################################################
### GLOBALS
########################################################################## */
int opt_SYSLOG = 0;
const char * PRGNAME = "";
enum OP_MODE mode = MODE_UNDEFINED;
/* #############################################################################
### FUNCTIONS
########################################################################## */
void fill_adr_strings (struct tuple4 addr, char src_ip[46], char dst_ip[46], int *sport, int *dport)
{
strncpy(src_ip, int_ntoa(addr.saddr), 46); src_ip[45] = '\0';
strncpy(dst_ip, int_ntoa(addr.daddr), 46); dst_ip[45] = '\0';
*sport = addr.source;
*dport = addr.dest;
}
/* Returns 0 if winnti signature matches. Returns != 0 otherwise */
uint16_t check_for_winnti(char *data, size_t len)
{
if(len < 16)
return 1;
uint16_t *w = (uint16_t *)data;
uint32_t *l = (uint32_t *)data;
// Three conditions hold for the first 8 words of winnti traffic:
// w[0] ^ w[4] ^ w[7] = 0x0000
// w[1] ^ w[5] ^ w[6] = 0x0000
// w[x] != 0
// l[2] usually contains a timestamp
if(!l[0] || !l[1] || !l[2] || !l[3])
return 1;
return (w[0] ^ w[4] ^ w[7]) | (w[1] ^ w[5] ^ w[6]);
}
void tcp_callback (struct tcp_stream *tcp, void **param)
{
struct half_stream *hlf;
char src_ip[46], dst_ip[46];
int sport, dport;
static char sbuf[1000];
switch(tcp->nids_state)
{
// Connection has just been established
case NIDS_JUST_EST:
tcp->server.collect++; // Collect data that is sent to server
break;
// Received some data
case NIDS_DATA:
hlf = &tcp->server;
// Stop collecting packets
tcp->server.collect = 0;
// We are only interested in the first bytes of a connection
if(tcp->server.offset != 0)
break;
// Check for signs of a winnti connection
if(check_for_winnti(hlf->data, tcp->server.count_new) != 0)
break;
// Get IP addresses and ports
fill_adr_strings(tcp->addr, src_ip, dst_ip, &sport, &dport);
// Build alert message
snprintf(sbuf, sizeof(sbuf), "Found WINNTI session setup: (TCP) %s:%i -> %s:%i",
src_ip, sport, dst_ip, dport);
// Make UTC timestamp of last PCAP packet
struct tm tm;
struct timeval *tv = &nids_last_pcap_header->ts;
gmtime_r(&tv->tv_sec, &tm);
// Get timestamp
char tstamp[64];
strftime(tstamp, 64, "%Y-%m-%d %H:%M:%S", &tm);
// Print alert to stdout
printf("[!] %s.%06ldZ %s\n", tstamp, tv->tv_usec, sbuf);
fflush(stdout);
// Write syslog entry if required
if(opt_SYSLOG)
{
syslog(LOG_LOCAL7 | LOG_ALERT, "%s", sbuf);
}
case NIDS_CLOSE:
case NIDS_RESET:
case NIDS_TIMED_OUT:
// Connection terminated
break;
default:
break;
}
}
void udp_callback (struct tuple4 *addr, char *buf, int len, struct ip *iph)
{
char src_ip[46], dst_ip[46];
int sport, dport;
static char sbuf[1000];
// Check for signs of a winnti connection
if(check_for_winnti(buf, len) != 0)
return;
// Get IP addresses and ports
fill_adr_strings(*addr, src_ip, dst_ip, &sport, &dport);
// Build alert message
snprintf(sbuf, sizeof(sbuf), "Found WINNTI session setup: (UDP) %s:%i -> %s:%i",
src_ip, sport, dst_ip, dport);
// Make UTC timestamp of last PCAP packet
struct tm tm;
struct timeval *tv = &nids_last_pcap_header->ts;
gmtime_r(&tv->tv_sec, &tm);
// Get timestamp
char tstamp[64];
strftime(tstamp, 64, "%Y-%m-%d %H:%M:%S", &tm);
// Print alert to stdout
printf("[!] %s.%06ldZ %s\n", tstamp, tv->tv_usec, sbuf);
fflush(stdout);
// Write syslog entry if required
if(opt_SYSLOG)
{
syslog(LOG_LOCAL7 | LOG_ALERT, "%s", sbuf);
}
}
void nids_syslog(int type, int err, struct ip *iph, void *data)
{
return;
}
void usage()
{
printf(
"Usage: %s <-i device|-f pcapfile> [-l]\n"
" -l Log to syslog (local7.alert 'nsm')\n",
PRGNAME);
exit(1);
}
int main(int argc, char * const *argv)
{
PRGNAME = argv[0];
fprintf(stderr, "wntidect version %s using libnids %i.%i -- Stefan Ruester\n", WINTIDECT_VERISON, NIDS_MAJOR, NIDS_MINOR);
// Enable TCP workarounds
nids_params.tcp_workarounds = 1;
// Disable portscan detection
nids_params.scan_num_hosts = 0;
// Disable logging by libnids
nids_params.syslog = &nids_syslog;
// Disable checksum checks
struct nids_chksum_ctl cksum_ctl = {
.netaddr = 0,
.mask = 0,
.action = NIDS_DONT_CHKSUM
};
nids_register_chksum_ctl(&cksum_ctl, 1);
// Read command line arguments
int opt;
while ((opt = getopt(argc, argv, "i:f:l")) != -1)
{
switch(opt)
{
case 'f':
if(mode != MODE_UNDEFINED)
usage();
mode = MODE_READ_PCAP;
nids_params.filename = optarg;
fprintf(stderr, "[i] Reading PCAP file %s\n", argv[2]);
break;
case 'i':
if(mode != MODE_UNDEFINED)
usage();
mode = MODE_LIVE_INTERFACE;
nids_params.device = optarg;
fprintf(stderr, "[i] Reading from device %s\n", argv[2]);
break;
case 'l':
opt_SYSLOG = 1;
openlog("nsm", 0, LOG_LOCAL7);
break;
default:
usage();
}
}
if(mode == MODE_UNDEFINED)
{
usage();
}
// Initialize NDIS library
if(!nids_init())
{
fprintf(stderr,"%s\n", nids_errbuf);
exit(1);
}
fflush(stdout);
// Register callback functions
nids_register_tcp (tcp_callback);
nids_register_udp (udp_callback);
// Start capture
nids_run ();
return 0;
}