forked from RoliSoft/Obfuscation-Tunnel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathicmp_client.cpp
201 lines (169 loc) · 5.55 KB
/
icmp_client.cpp
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
#pragma once
#include "shared.cpp"
#include "icmp_base.cpp"
class icmp_client : public icmp_base
{
public:
bool pcap = false;
char *cap_dev;
private:
int fd;
struct sockaddr_in remote_addr;
#if HAVE_PCAP
pcap_t *cap_ptr;
#endif
public:
icmp_client(struct sockaddr_in remote_addr, struct session* session)
: transport_base(session->verbose), icmp_base(session->random_id), pcap(session->pcap), cap_dev(session->cap_dev), remote_addr(remote_addr)
{
}
icmp_client(struct sockaddr_in remote_addr, bool pcap = false, char *cap_dev = NULL, bool random_id = false, bool verbose = false)
: transport_base(verbose), icmp_base(random_id), pcap(pcap), cap_dev(cap_dev), remote_addr(remote_addr)
{
}
int start()
{
if ((this->fd = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP)) < 0)
{
perror("Client socket creation failed");
return EXIT_FAILURE;
}
#if HAVE_PCAP
if (this->pcap)
{
pcap_if_t *cap_devs;
char cap_err[PCAP_ERRBUF_SIZE];
if (this->cap_dev == NULL)
{
pcap_findalldevs(&cap_devs, cap_err);
if (cap_devs == NULL)
{
printf("Error finding devices: %s\n", cap_err);
return EXIT_FAILURE;
}
this->cap_dev = cap_devs->name;
}
this->cap_ptr = pcap_open_live(this->cap_dev, MTU_SIZE, 1, 1, cap_err);
if (this->cap_ptr == NULL)
{
fprintf(stderr, "Can't open pcap device %s: %s\n", this->cap_dev, cap_err);
return EXIT_FAILURE;
}
pcaps.push_back(this->cap_ptr);
printf("Device selected for packet capture: %s\n", this->cap_dev);
struct bpf_program fp;
char *bpf_filter;
if (this->random_id)
{
bpf_filter = (char*)"icmp[icmptype] == icmp-echoreply";
}
else
{
bpf_filter = (char*)"icmp[icmptype] == icmp-echoreply and icmp[4] == 0x13 and icmp[5] = 0x37";
}
if (pcap_compile(this->cap_ptr, &fp, bpf_filter, 0, 0) == -1)
{
fprintf(stderr, "Can't parse filter %s: %s\n", bpf_filter, pcap_geterr(this->cap_ptr));
return EXIT_FAILURE;
}
if (pcap_setfilter(this->cap_ptr, &fp) == -1)
{
fprintf(stderr, "Can't install filter %s: %s\n", bpf_filter, pcap_geterr(this->cap_ptr));
return EXIT_FAILURE;
}
}
else
{
printf("You should consider adding -p for PCAP when remote is ICMP.\n");
#ifdef AF_PACKET
// ip[20] == 0x00 && ip[24] == 0x13 && ip[25] == 0x37
// in order to offset the presence of an ethernet header assumed by pcap_compile,
// we'll change the ip[] to ether[]
struct bpf_program bpf;
this->cap_ptr = pcap_open_dead(DLT_EN10MB, MTU_SIZE);
char *bpf_filter;
if (this->random_id)
{
bpf_filter = (char*)"ether[20] == 0x00";
}
else
{
bpf_filter = (char*)"ether[20] == 0x00 && ether[24] == 0x13 && ether[25] == 0x37";
}
if (pcap_compile(this->cap_ptr, &bpf, bpf_filter, 0, PCAP_NETMASK_UNKNOWN) == -1)
{
fprintf(stderr, "Can't parse filter %s: %s\n", bpf_filter, pcap_geterr(this->cap_ptr));
return EXIT_FAILURE;
}
struct sock_fprog linux_bpf = {
.len = (u_short)bpf.bf_len,
.filter = (struct sock_filter *)bpf.bf_insns,
};
if(setsockopt(this->fd, SOL_SOCKET, SO_ATTACH_FILTER, &linux_bpf, sizeof(linux_bpf)) != 0)
{
perror("Failed to set BPF filter on raw socket, connection may be unstable.");
}
pcap_close(this->cap_ptr);
#else
// BPF program cannot be attached to a raw socket in BSD,
// reimplementing /dev/bpf* would essentially just be reimplementing libpcap
printf("Packet filtering for BSD not implemented, use -p if connection is unstable.\n");
#endif
}
#else
printf("ICMP tunnels are much faster and stable when used with PCAP.\n");
#endif
printf("Started ICMP client for ");
print_ip(&this->remote_addr);
printf("\n");
sockets.push_back(this->fd);
started = true;
return EXIT_SUCCESS;
}
int stop()
{
close(this->fd);
#if HAVE_PCAP
if (this->pcap)
{
pcap_close(this->cap_ptr);
}
#endif
started = false;
return EXIT_SUCCESS;
}
int send(char *buffer, ssize_t msglen)
{
return _send(this->fd, false, (const struct sockaddr*)&this->remote_addr, buffer, msglen);
}
int receive(char *buffer, int* offset)
{
#if HAVE_PCAP
if (this->pcap)
{
return _receive_pcap(this->cap_ptr, false, &this->remote_addr, buffer, offset);
}
else
{
#endif
return _receive(this->fd, false, (struct sockaddr*)&this->remote_addr, buffer, offset);
#if HAVE_PCAP
}
#endif
}
int get_selectable()
{
#if HAVE_PCAP
if (this->pcap)
{
return pcap_get_selectable_fd(this->cap_ptr);
}
else
{
#endif
return this->fd;
#if HAVE_PCAP
}
#endif
}
};