-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLibNIDS.xs
488 lines (395 loc) · 7.71 KB
/
LibNIDS.xs
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"
#include "ppport.h"
#include "nids.h"
#include "const-c.inc"
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include "pcap.h"
SV* our_tcp_callback = 0;
void
tcp_callback_f (struct tcp_stream *tcp_stream, SV ** tcp_stream_sv_ptr) {
if(tcp_stream->nids_state == NIDS_JUST_EST) {
SV* tcp_stream_sv = newRV_noinc(newSViv((IV) tcp_stream));
sv_bless(tcp_stream_sv, gv_stashpv("Net::LibNIDS::tcp_stream",1));
*tcp_stream_sv_ptr = tcp_stream_sv;
}
{
dSP;
ENTER;
SAVETMPS;
PUSHMARK(SP);
XPUSHs(*tcp_stream_sv_ptr);
PUTBACK;
call_sv(our_tcp_callback, G_VOID);
FREETMPS;
LEAVE;
}
if((!tcp_stream->server.collect
&& !tcp_stream->client.collect
&& !tcp_stream->server.collect_urg
&& !tcp_stream->client.collect_urg)
|| tcp_stream->nids_state == NIDS_CLOSE
|| tcp_stream->nids_state == NIDS_RESET
) {
SvREFCNT_dec(*tcp_stream_sv_ptr);
*tcp_stream_sv_ptr = NULL;
if(tcp_stream->nids_state == NIDS_CLOSE
|| tcp_stream->nids_state == NIDS_RESET) {
}
}
}
char* state2string (IV state) {
switch (state) {
case NIDS_JUST_EST:
return "NIDS_JUST_EST";
case NIDS_DATA:
return "NIDS_DATA";
case NIDS_CLOSE:
return "NIDS_CLOSE";
case NIDS_RESET:
return "NIDS_RESET";
case NIDS_TIMED_OUT:
return "NIDS_TIMED_OUT";
case NIDS_EXITING:
return "NIDS_EXITING";
default:
return "UNKNOWN";
}
}
#define obj2tcpstream(obj) ((struct tcp_stream*) SvIV(SvRV(obj)))
#define obj2halfstream(obj) ((struct half_stream*) SvIV(SvRV(obj)))
#define int_ntoa(x) inet_ntoa(*((struct in_addr *)&x))
MODULE = Net::LibNIDS PACKAGE = Net::LibNIDS::tcp_stream
# Export of last_pcap_header was added in libnids-1.19
#if NIDS_MINOR>=19
IV
lastpacket_sec(obj)
SV* obj
CODE:
RETVAL = nids_last_pcap_header->ts.tv_sec;
OUTPUT:
RETVAL
IV
lastpacket_usec(obj)
SV* obj
CODE:
RETVAL = nids_last_pcap_header->ts.tv_usec;
OUTPUT:
RETVAL
#else
void
lastpacket_sec(obj)
SV* obj
CODE:
croak("You need libnids >1.19 in order to use this function");
void
lastpacket_usec(obj)
SV* obj
CODE:
croak("You need libnids >1.19 in order to use this function");
#endif
IV
state(obj)
SV* obj
CODE:
RETVAL = obj2tcpstream(obj)->nids_state;
OUTPUT:
RETVAL
char*
state_string(obj)
SV* obj
CODE:
RETVAL = state2string(obj2tcpstream(obj)->nids_state);
OUTPUT:
RETVAL
SV*
server(obj)
SV* obj
CODE:
RETVAL = newRV_noinc(newSViv((IV) &(obj2tcpstream(obj)->server )));
sv_bless(RETVAL, gv_stashpv("Net::LibNIDS::tcp_stream::half",1));
OUTPUT:
RETVAL
SV*
client(obj)
SV* obj
CODE:
RETVAL = newRV_noinc(newSViv((IV) &(obj2tcpstream(obj)->client )));
sv_bless(RETVAL, gv_stashpv("Net::LibNIDS::tcp_stream::half",1));
OUTPUT:
RETVAL
char*
client_ip(obj)
SV* obj
CODE:
RETVAL = int_ntoa(obj2tcpstream(obj)->addr.saddr);
OUTPUT:
RETVAL
char*
server_ip(obj)
SV* obj
CODE:
RETVAL = int_ntoa(obj2tcpstream(obj)->addr.daddr);
OUTPUT:
RETVAL
IV
client_port(obj)
SV* obj
CODE:
RETVAL = obj2tcpstream(obj)->addr.source;
OUTPUT:
RETVAL
IV
server_port(obj)
SV* obj
CODE:
RETVAL = obj2tcpstream(obj)->addr.dest;
OUTPUT:
RETVAL
void
discard(obj, numbytes);
SV* obj;
int numbytes;
CODE:
nids_discard(obj2tcpstream(obj), numbytes);
MODULE = Net::LibNIDS PACKAGE = Net::LibNIDS::tcp_stream::half
void
collect_on(obj)
SV* obj
CODE:
obj2halfstream(obj)->collect = 1;
void
collect_off(obj)
SV* obj
CODE:
obj2halfstream(obj)->collect = 0;
IV
collect(obj)
SV* obj
CODE:
RETVAL = obj2halfstream(obj)->collect;
OUTPUT:
RETVAL
void
collect_urg_on(obj)
SV* obj
CODE:
obj2halfstream(obj)->collect_urg = 1;
void
collect_urg_off(obj)
SV* obj
CODE:
obj2halfstream(obj)->collect_urg = 0;
IV
collect_urg(obj)
SV* obj
CODE:
RETVAL = obj2halfstream(obj)->collect_urg;
OUTPUT:
RETVAL
IV
count(obj)
SV* obj
CODE:
RETVAL = obj2halfstream(obj)->count;
OUTPUT:
RETVAL
IV
count_new(obj)
SV* obj
CODE:
RETVAL = obj2halfstream(obj)->count_new;
OUTPUT:
RETVAL
IV
offset(obj)
SV* obj
CODE:
RETVAL = obj2halfstream(obj)->offset;
OUTPUT:
RETVAL
SV*
data(obj)
SV* obj
CODE:
RETVAL = newSVpv( obj2halfstream(obj)->data , obj2halfstream(obj)->count_new);
OUTPUT:
RETVAL
IV
curr_ts(obj)
SV* obj
CODE:
RETVAL = obj2halfstream(obj)->curr_ts;
OUTPUT:
RETVAL
MODULE = Net::LibNIDS PACKAGE = Net::LibNIDS
INCLUDE: const-xs.inc
IV
nids_init()
POSTCALL:
if(!RETVAL)
croak("Net::LibNIDS: %s", nids_errbuf);
void
nids_run()
void
tcp_callback(cb);
SV* cb
CODE:
our_tcp_callback = SvRV(cb);
nids_register_tcp(tcp_callback_f);
void
checksum_off()
CODE:
struct nids_chksum_ctl nochksumchk;
nochksumchk.netaddr = 0;
nochksumchk.mask = 0;
nochksumchk.action = NIDS_DONT_CHKSUM;
nids_register_chksum_ctl(&nochksumchk, 1);
MODULE = Net::LibNIDS PACKAGE = Net::LibNIDS::param
char*
get_device()
CODE:
RETVAL = nids_params.device;
OUTPUT:
RETVAL
void
set_device(device)
char* device
CODE:
nids_params.device = device;
char*
get_filename()
CODE:
RETVAL = nids_params.filename;
OUTPUT:
RETVAL
void
set_filename(filename)
char* filename
CODE:
nids_params.filename = filename;
char*
get_pcap_filter()
CODE:
RETVAL = nids_params.pcap_filter;
OUTPUT:
RETVAL
void
set_pcap_filter(pcap_filter)
char* pcap_filter
CODE:
nids_params.pcap_filter = pcap_filter;
IV
get_n_tcp_streams()
CODE:
RETVAL = nids_params.n_tcp_streams;
OUTPUT:
RETVAL
void
set_n_tcp_streams(n_tcp_streams)
IV n_tcp_streams
CODE:
nids_params.n_tcp_streams = n_tcp_streams;
IV
get_n_hosts()
CODE:
RETVAL = nids_params.n_hosts;
OUTPUT:
RETVAL
void
set_n_hosts(n_hosts)
IV n_hosts
CODE:
nids_params.n_hosts = n_hosts;
IV
get_sk_buff_size()
CODE:
RETVAL = nids_params.sk_buff_size;
OUTPUT:
RETVAL
void
set_sk_buff_size(sk_buff_size)
IV sk_buff_size
CODE:
nids_params.sk_buff_size = sk_buff_size;
IV
get_dev_addon()
CODE:
RETVAL = nids_params.dev_addon;
OUTPUT:
RETVAL
void
set_dev_addon(dev_addon)
IV dev_addon
CODE:
nids_params.dev_addon = dev_addon;
IV
get_syslog_level()
CODE:
RETVAL = nids_params.syslog_level;
OUTPUT:
RETVAL
void
set_syslog_level(syslog_level)
IV syslog_level
CODE:
nids_params.syslog_level = syslog_level;
IV
get_scan_num_hosts()
CODE:
RETVAL = nids_params.scan_num_hosts;
OUTPUT:
RETVAL
void
set_scan_num_hosts(scan_num_hosts)
IV scan_num_hosts
CODE:
nids_params.scan_num_hosts = scan_num_hosts;
IV
get_scan_num_ports()
CODE:
RETVAL = nids_params.scan_num_ports;
OUTPUT:
RETVAL
void
set_scan_num_ports(scan_num_ports)
IV scan_num_ports
CODE:
nids_params.scan_num_ports = scan_num_ports;
IV
get_scan_delay()
CODE:
RETVAL = nids_params.scan_delay;
OUTPUT:
RETVAL
void
set_scan_delay(scan_delay)
IV scan_delay
CODE:
nids_params.scan_delay = scan_delay;
IV
get_promisc()
CODE:
RETVAL = nids_params.promisc;
OUTPUT:
RETVAL
void
set_promisc(promisc)
IV promisc
CODE:
nids_params.promisc = promisc;
IV
get_one_loop_less()
CODE:
RETVAL = nids_params.one_loop_less;
OUTPUT:
RETVAL
void
set_one_loop_less(one_loop_less)
IV one_loop_less
CODE:
nids_params.one_loop_less = one_loop_less;