-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdarkshell.cpp
350 lines (304 loc) · 9.15 KB
/
darkshell.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
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
/*****************************************************************
* Created on 2012/01/31
****************************************************************/
#include <iostream>
#include <iomanip>
#include <ctime>
#include <pcap.h>
#include <assert.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <pwd.h>
#include <time.h>
#include <malloc.h>
#include <pthread.h>
#include <fcntl.h>
#include <syslog.h>
#include <arpa/inet.h>
#include <zlib.h>
#include "common.h"
#include "darkshell.h"
using namespace std;
//
// for cb1st botnet decompress/compress
#if 0
// not thread-safe
int dshell_handle_pcap(
struct timeval *ts,
unsigned int sip,
u_short sport,
unsigned int dip,
u_short dport,
u_char data[],
int len // not including tcp header
)
{
struct tm *tm;
//unsigned short sport = ntohs(tcphdr->th_sport);
//unsigned short dport = ntohs(tcphdr->th_dport);
#define CB1ST_CMD_BUF_SIZE 2048
static u_char cmd_data[CB1ST_CMD_BUF_SIZE];
static int write_pos = 0;
u_char uncompr[CB1ST_CMD_BUF_SIZE];
int uncomprLen = CB1ST_CMD_BUF_SIZE;
cb1st_hdr *hdr = (cb1st_hdr *)cmd_data;
int ret = -1;
int i;
//printf("sizeof()=%d, offset=%d\n",
// sizeof(cb1st_hdr), (char *)&(hdr->total_len) - (char *)hdr);
if (write_pos >= CB1ST_CMD_BUF_SIZE)
{
printf("buf overflow1, %u.%u.%u.%u:%d -> %u.%u.%u.%u:%d, "
"write_pos=%d, cb1st.total_len=%d!\n",
sip&0xFF, (sip>>8)&0xFF,(sip>>16)&0xFF,(sip>>24)&0xFF, sport,
dip&0xFF, (dip>>8)&0xFF,(dip>>16)&0xFF,(dip>>24)&0xFF, dport,
write_pos, (hdr->total_len)
);
write_pos = 0; // clear
return -1;
}
memcpy(cmd_data + write_pos, data, min(len, CB1ST_CMD_BUF_SIZE - write_pos));
write_pos += min(len, CB1ST_CMD_BUF_SIZE - write_pos);
if (write_pos != (hdr->total_len))
{
if (write_pos > (hdr->total_len))
{
printf("buf overflow2, %u.%u.%u.%u:%d -> %u.%u.%u.%u:%d, "
"write_pos(=%d) > cb1st.total_len(=%d)!\n",
sip&0xFF, (sip>>8)&0xFF,(sip>>16)&0xFF,(sip>>24)&0xFF, sport,
dip&0xFF, (dip>>8)&0xFF,(dip>>16)&0xFF,(dip>>24)&0xFF, dport,
write_pos, (hdr->total_len)
);
write_pos = 0; // clear
return -1;
}
printf("data not enough, %u.%u.%u.%u:%d -> %u.%u.%u.%u:%d, "
"write_pos(=%d) > cb1st.total_len(=%d)!\n",
sip&0xFF, (sip>>8)&0xFF,(sip>>16)&0xFF,(sip>>24)&0xFF, sport,
dip&0xFF, (dip>>8)&0xFF,(dip>>16)&0xFF,(dip>>24)&0xFF, dport,
write_pos, (hdr->total_len)
);
return 0;
}
ret = cb1st_inflate(hdr->data, (hdr->total_len) - sizeof(*hdr), uncompr, &uncomprLen);
if (ret < 0)
{
printf("failed to inflate, %u.%u.%u.%u:%d -> %u.%u.%u.%u:%d, "
"cb1st.total_len=%d\n",
sip&0xFF, (sip>>8)&0xFF,(sip>>16)&0xFF,(sip>>24)&0xFF, sport,
dip&0xFF, (dip>>8)&0xFF,(dip>>16)&0xFF,(dip>>24)&0xFF, dport,
(hdr->total_len)
);
return -1;
}
// display uncompressed data
tm = localtime(&ts->tv_sec);
printf("%.4i-%.2i-%.2i %.2i:%.2i:%.2i, "
"%u.%u.%u.%u:%d -> %u.%u.%u.%u:%d, ",
1900 + tm->tm_year,1+ tm->tm_mon,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec,
sip&0xFF, (sip>>8)&0xFF,(sip>>16)&0xFF,(sip>>24)&0xFF, sport,
dip&0xFF, (dip>>8)&0xFF,(dip>>16)&0xFF,(dip>>24)&0xFF, dport);
printf("total_len=%d, old_len=%d\n ", hdr->total_len, hdr->olen);
for (i = 0; i < uncomprLen; )
{
// by "-hex ", default in ascii mode
if (in_hex)
{
printf("%.2X ", *(uncompr + i));
}
else
{
if (*(uncompr + i) < 0x7F && *(uncompr + i) >= 0x20)
{
printf("%c", *(uncompr + i));
}
else
{
printf(".");
}
}
i++;
if ((i % 16) == 0)
{
printf("\n ");
}
}
printf("\n\n");
write_pos = 0; // clear
return 0;
}
void pcap_handler_dshell(
char *usr,
const struct pcap_pkthdr *pcapheader,
const u_char *pkt
)
{
struct ip_h *ip_hdr;
struct tcphdr *tcp_hdr;
int len;
int ret;
static int number = 0;
//printf("pcap_handler_cb1st(): pkt # %d\n", number++);
//----- ignore packets that are just too short
if (pcapheader->caplen < sizeof(struct eth_h) + sizeof(*ip_hdr) + sizeof(*tcp_hdr))
{
printf("invalid packet!\n");
return;
}
ip_hdr = (struct ip_h*)(pkt + sizeof(struct eth_h));
if (ip_hdr->proto != 0x06) // TCP
{
printf("not tcp!\n");
return;
}
tcp_hdr = (struct tcphdr *)((char *)ip_hdr + ((ip_hdr->vhl &0x0F) << 2));
if (!(tcp_hdr->th_flags & TH_PSH))
{
// no payload, skip!
//printf("no payload\n");
return;
}
len = ntohs(ip_hdr->len) - ((ip_hdr->vhl &0x0F) << 2) - (tcp_hdr->th_off
<< 2);
if (pcapheader->caplen - len != sizeof(struct eth_h) + ((ip_hdr->vhl &
0x0F) << 2) +
(tcp_hdr->th_off << 2))
{
printf("invalid packet len, tcphdr_len=%d, len=%d, pkt_len=%d!\n",
(tcp_hdr->th_off << 2), len, pcapheader->caplen);
return;
}
if (0 == len)
{
printf("PUSH set, but no payload!\n");
return;
}
ret = cb1st_handle_pcap((struct timeval *)&pcapheader->ts, ip_hdr->src.s_addr, ntohs(tcp_hdr->th_sport),
ip_hdr->dst.s_addr, ntohs(tcp_hdr->th_dport),
(u_char *)tcp_hdr + (tcp_hdr->th_off << 2), len);
return;
}
#endif
#if 0
char *msg_descr[CB1ST_MSG_CODE_MAX] =
{
[CB1ST_MSG_CODE_PING] = "Ping",
[CB1ST_MSG_CODE_PONG] = "Pong",
[CB1ST_MSG_CODE_REGISTER] = "Register",
[CB1ST_MSG_CODE_REGISTER_RESP] = "Register response",
[CB1ST_MSG_CODE_VER_TOO_LOW ] = "Server version too low",
};
#endif
#if 0
int dshell_handle_msg(
cb1st_msg *msg_recv,
int recv_len, // in bytes
cb1st_msg *msg_resp,
int *resp_len // in bytes
)
{
u_char *p = msg_resp->data;
int left = *resp_len;
int i;
//printf("cb1st_handle_msg(): code=0x%.2X\n", msg_recv->msg_code);
//save_message(char file [ ], msg_recv, recv_len)
switch (msg_recv->msg_code)
{
case CB1ST_MSG_CODE_REGISTER:
{
printf("cb1st_handle_msg(): on CB1ST_MSG_CODE_REGISTER\n");
printf("version=");
#if 0
//int i;
//cb1st_register_msg *register_msg = (cb1st_register_msg *)msg_recv->data;
for (i = 0; i < 20; i++)
{
printf("%.2X", register_msg->VersionInfoEx[i]);
}
printf("\n");
#endif
CB1ST_HANDLE_MSG_CHECK_BUF(left, register_resp_len);
msg_resp->msg_code = CB1ST_MSG_CODE_REGISTER_RESP;
memcpy(msg_resp->data, register_resp, register_resp_len - 1);
*resp_len = register_resp_len;
printf("*resp_len=%d\n", *resp_len);
break;
}
case CB1ST_MSG_CODE_PING:
printf("cb1st_handle_msg(): on CB1ST_MSG_CODE_PING\n");
msg_resp->msg_code = CB1ST_MSG_CODE_PONG;
*resp_len = 1;
break;
case CB1ST_MSG_CODE_VER_TOO_LOW:
printf("cb1st_handle_msg(): on CB1ST_MSG_CODE_VER_TOO_LOW\n");
*resp_len = 0;
break;
default:
printf("cb1st_handle_msg(): unknown code %d\n", msg_recv->msg_code);
*resp_len = 0;
#if 0
printf("data: ");
for (i = 0; i < recv_len; i++)
{
printf("%.2X ", msg_recv->data[i]);
}
printf("\n");
#endif
break;
}
printf("cb1st_handle_msg(): resp_code=0x%.2X, len=%d\n\n",
msg_resp->msg_code, *resp_len);
return *resp_len;
}
#endif
#if 0
int cb1st_handle_cmd(
u_char command,
cb1st_msg *msg_resp,
int *resp_len // in bytes
)
{
//u_char *p = msg_resp->data;
//int left = *resp_len;
int i;
msg_resp->msg_code = command;
switch (command)
{
case CB1ST_MSG_CODE_DDOS:
printf("cb1st_handle_cmd(): on CB1ST_MSG_CODE_DDOS\n");
*resp_len = 1;
break;
default:
*resp_len = 1;
break;
}
return *resp_len;
}
#endif
void dshell_decrypt(u_char *data, int len, u_char key)
{
while ( len > 0)
{
u_char key2 = (key % 254) + 1;
*data = (*data - key2) ^ key2;
++data;
--len;
}
}
void dshell_encrypt(u_char *data, int len, u_char key)
{
if ( len != 0)
{
do
{
u_char key2 = (key % 254) + 1;
*data = ((*data) ^ key2) + key2;
++data;
--len;
}
while ( len > 0);
}
}