-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathfiddle.cpp
More file actions
114 lines (95 loc) · 2.6 KB
/
Copy pathfiddle.cpp
File metadata and controls
114 lines (95 loc) · 2.6 KB
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
#include "stdafx.h"
unsigned __stdcall fiddle(void *Argclist)
{
char *filter_string;
BOOL *end_command ;
agrclist *pagrclist;
pagrclist = (struct agrclist *)Argclist;
filter_string = pagrclist->filter;
end_command = &pagrclist->command;
HANDLE handle;
INT16 priority = 0;
unsigned char packet[MAXBUF];
UINT packet_len;
WINDIVERT_ADDRESS addr;
PWINDIVERT_IPHDR ip_header;
PWINDIVERT_IPV6HDR ipv6_header;
PWINDIVERT_ICMPHDR icmp_header;
PWINDIVERT_ICMPV6HDR icmpv6_header;
PWINDIVERT_TCPHDR tcp_header;
PWINDIVERT_UDPHDR udp_header;
const char *err_str;
LARGE_INTEGER base, freq;
handle = WinDivertOpen(filter_string, WINDIVERT_LAYER_NETWORK, priority,
WINDIVERT_FLAG_SNIFF);
if (handle == INVALID_HANDLE_VALUE)
{
if (GetLastError() == ERROR_INVALID_PARAMETER &&
!WinDivertHelperCheckFilter(filter_string, WINDIVERT_LAYER_NETWORK,
&err_str, NULL))
{
fprintf(stderr, "error: invalid filter \"%s\"\n", err_str);
exit(EXIT_FAILURE);
}
fprintf(stderr, "error: failed to open the WinDivert device (%d)\n",
GetLastError());
exit(EXIT_FAILURE);
}
if (!WinDivertSetParam(handle, WINDIVERT_PARAM_QUEUE_LEN, 8192))
{
fprintf(stderr, "error: failed to set packet queue length (%d)\n",
GetLastError());
exit(EXIT_FAILURE);
}
if (!WinDivertSetParam(handle, WINDIVERT_PARAM_QUEUE_TIME, 2048))
{
fprintf(stderr, "error: failed to set packet queue time (%d)\n",
GetLastError());
exit(EXIT_FAILURE);
}
QueryPerformanceFrequency(&freq);
QueryPerformanceCounter(&base);
/*FILE *f;
errno_t err;
if ((err = fopen_s(&f, "tmp.pcap", "w+b")) != 0) {
printf("file open error!\n");
WinDivertClose(handle);
_endthreadex(0);
}*/
FILE *f = fopen("tmp.pcap", "w+b");
if (f == NULL) {
printf("file open error!\n");
return NULL;
}
int packet_id = 0;
while (*end_command)
{
if (!WinDivertRecv(handle, packet, sizeof(packet), &addr, &packet_len))
{
fprintf(stderr, "warning: failed to read packet (%d)\n",
GetLastError());
continue;
}
WinDivertHelperParsePacket(packet, packet_len, &ip_header,
&ipv6_header, &icmp_header, &icmpv6_header, &tcp_header,
&udp_header, NULL, NULL);
if (ip_header == NULL && ipv6_header == NULL)
{
fprintf(stderr, "warning: junk packet\n");
}
PAC packets;
if (ip_header != NULL) {
if (tcp_header != NULL || tcp_header != NULL) {
memcpy(packets.packet, packet, MAXBUF);
packets.ID = packet_id;
packets.packet_len = packet_len;
fwrite(&packets, sizeof(PAC), 1, f);
packet_id++;
}
}
}
fclose(f);
WinDivertClose(handle);
_endthreadex(0);
return 0;
}