forked from iagox86/dnscat2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdriver_command.c
169 lines (135 loc) · 3.88 KB
/
driver_command.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
/* driver_command.c
* By Ron Bowes
* Created May, 2014
*
* See LICENSE.md
*/
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#ifndef WIN32
#include <unistd.h>
#endif
#include "command_packet.h"
#include "controller/session.h"
#include "controller/controller.h"
#include "drivers/driver_exec.h"
#include "libs/log.h"
#include "libs/memory.h"
#include "libs/select_group.h"
#include "libs/tcp.h"
#include "libs/types.h"
#include "driver_command.h"
static uint16_t request_id()
{
static uint16_t request_id = 0;
return request_id++;
}
/* I moved some functions into other files for better organization;
* this includes them. */
#include "commands_standard.h"
#include "commands_tunnel.h"
void driver_command_data_received(driver_command_t *driver, uint8_t *data, size_t length)
{
command_packet_t *in = NULL;
command_packet_t *out = NULL;
buffer_add_bytes(driver->stream, data, length);
while((in = command_packet_read(driver->stream)))
{
/* TUNNEL_DATA commands are too noisy to print. */
if(in->command_id != TUNNEL_DATA)
{
printf("Got a command: ");
command_packet_print(in);
}
switch(in->command_id)
{
case COMMAND_PING:
out = handle_ping(driver, in);
break;
case COMMAND_SHELL:
out = handle_shell(driver, in);
break;
case COMMAND_EXEC:
out = handle_exec(driver, in);
break;
case COMMAND_DOWNLOAD:
out = handle_download(driver, in);
break;
case COMMAND_UPLOAD:
out = handle_upload(driver, in);
break;
case COMMAND_SHUTDOWN:
out = handle_shutdown(driver, in);
break;
case COMMAND_DELAY:
out = handle_delay(driver, in);
break;
case TUNNEL_CONNECT:
out = handle_tunnel_connect(driver, in);
break;
case TUNNEL_DATA:
out = handle_tunnel_data(driver, in);
break;
case TUNNEL_CLOSE:
out = handle_tunnel_close(driver, in);
break;
case COMMAND_ERROR:
out = handle_error(driver, in);
break;
default:
LOG_ERROR("Got a command packet that we don't know how to handle!\n");
out = command_packet_create_error_response(in->request_id, 0xFFFF, "Not implemented yet!");
}
/* Respond if and only if an outgoing packet was created. */
if(out)
{
uint8_t *data;
size_t length;
if(out->command_id != TUNNEL_DATA)
{
printf("Response: ");
command_packet_print(out);
}
data = command_packet_to_bytes(out, &length);
buffer_add_bytes(driver->outgoing_data, data, length);
safe_free(data);
command_packet_destroy(out);
}
command_packet_destroy(in);
}
}
uint8_t *driver_command_get_outgoing(driver_command_t *driver, size_t *length, size_t max_length)
{
/* If the driver has been killed and we have no bytes left, return NULL to close the session. */
if(driver->is_shutdown && buffer_get_remaining_bytes(driver->outgoing_data) == 0)
return NULL;
return buffer_read_remaining_bytes(driver->outgoing_data, length, max_length, TRUE);
}
driver_command_t *driver_command_create(select_group_t *group)
{
driver_command_t *driver = (driver_command_t*) safe_malloc(sizeof(driver_command_t));
driver->stream = buffer_create(BO_BIG_ENDIAN);
driver->group = group;
driver->is_shutdown = FALSE;
driver->outgoing_data = buffer_create(BO_LITTLE_ENDIAN);
driver->tunnels = ll_create(NULL);
return driver;
}
void driver_command_destroy(driver_command_t *driver)
{
if(!driver->is_shutdown)
driver_command_close(driver);
if(driver->name)
safe_free(driver->name);
if(driver->stream)
buffer_destroy(driver->stream);
safe_free(driver);
}
void driver_command_close(driver_command_t *driver)
{
driver->is_shutdown = TRUE;
}