forked from vanhauser-thc/thc-hydra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hydra-rdp.c
3255 lines (2666 loc) · 86.6 KB
/
hydra-rdp.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
/*
david: this module is heavily based on rdesktop v 1.7.0
rdesktop: A Remote Desktop Protocol client.
Protocol services - RDP layer
Copyright (C) Matthew Chapman <matthewc.unsw.edu.au> 1999-2008
Copyright 2003-2011 Peter Astrand <astrand@cendio.se> for Cendio AB
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
note:
this module was tested on w2k, xp, w2k3, w2k8
in terminal services configuration, in rdp-tcp properties
in Logon Settings tab, if 'Always prompt for password' is checked,
the password can't be passed interactively so there is no way
to test the credential (unless manually).
it's advised to lower the number of parallel tasks as RDP server
can't handle multiple connections at the same time.
It's particularly true on windows XP
*/
#include "hydra-mod.h"
#ifndef LIBOPENSSL
#include <stdio.h>
void dummy_rdp() {
printf("\n");
}
#else
#include "rdp.h"
extern char *HYDRA_EXIT;
BOOL g_encryption = True;
BOOL g_use_rdp5 = True;
BOOL g_console_session = False;
BOOL g_bitmap_cache = True;
BOOL g_bitmap_cache_persist_enable = False;
BOOL g_bitmap_compression = True;
BOOL g_desktop_save = True;
int32_t g_server_depth = -1;
int32_t os_version = 0; //2000
uint32 g_rdp5_performanceflags = RDP5_NO_WALLPAPER | RDP5_NO_FULLWINDOWDRAG | RDP5_NO_MENUANIMATIONS;
/* Session Directory redirection */
BOOL g_redirect = False;
uint32 g_redirect_flags = 0;
uint32 g_reconnect_logonid = 0;
char g_reconnect_random[16];
BOOL g_has_reconnect_random = False;
uint8 g_client_random[SEC_RANDOM_SIZE];
/*
0 unknown
1 success
2 failed
*/
#define LOGIN_UNKN 0
#define LOGIN_SUCC 1
#define LOGIN_FAIL 2
int32_t login_result = LOGIN_UNKN;
uint8 *g_next_packet;
uint32 g_rdp_shareid;
/* Called during redirection to reset the state to support redirection */
void rdp_reset_state(void) {
g_next_packet = NULL; /* reset the packet information */
g_rdp_shareid = 0;
sec_reset_state();
}
static void rdesktop_reset_state(void) {
rdp_reset_state();
}
static RDP_ORDER_STATE g_order_state;
#define TCP_STRERROR strerror(errno)
#define TCP_BLOCKS (errno == EWOULDBLOCK)
#ifndef INADDR_NONE
#define INADDR_NONE ((unsigned long) -1)
#endif
#define STREAM_COUNT 1
int32_t g_sock;
static struct stream g_in;
static struct stream g_out[STREAM_COUNT];
/* wait till socket is ready to write or timeout */
static BOOL tcp_can_send(int32_t sck, int32_t millis) {
fd_set wfds;
struct timeval time;
int32_t sel_count;
time.tv_sec = millis / 1000;
time.tv_usec = (millis * 1000) % 1000000;
FD_ZERO(&wfds);
FD_SET(sck, &wfds);
sel_count = select(sck + 1, 0, &wfds, 0, &time);
if (sel_count > 0) {
return True;
}
return False;
}
/* Initialise TCP transport data packet */
STREAM tcp_init(uint32 maxlen) {
static int32_t cur_stream_id = 0;
STREAM result = NULL;
result = &g_out[cur_stream_id];
cur_stream_id = (cur_stream_id + 1) % STREAM_COUNT;
if (maxlen > result->size) {
result->data = (uint8 *) xrealloc(result->data, maxlen);
result->size = maxlen;
}
result->p = result->data;
result->end = result->data; // + result->size;
return result;
}
/* Send TCP transport data packet */
void tcp_send(STREAM s) {
int32_t length = s->end - s->data;
int32_t sent, total = 0;
while (total < length) {
sent = hydra_send(g_sock, (char *) (s->data + total), length - total, 0);
if (sent <= 0) {
if (sent == -1 && TCP_BLOCKS) {
tcp_can_send(g_sock, 100);
sent = 0;
} else {
if (g_sock && !login_result)
error("send: %s\n", TCP_STRERROR);
return;
}
}
total += sent;
}
}
/* Receive a message on the TCP layer */
STREAM tcp_recv(STREAM s, uint32 length) {
uint32 new_length, end_offset, p_offset;
int32_t rcvd = 0;
if (s == NULL) {
/* read into "new" stream */
g_in.data = (uint8 *) xmalloc(length);
g_in.size = length;
g_in.end = g_in.p = g_in.data;
s = &g_in;
} else {
/* append to existing stream */
new_length = (s->end - s->data) + length;
if (new_length > s->size) {
p_offset = s->p - s->data;
end_offset = s->end - s->data;
//printf("length: %d, %p s->data, %p +%d s->p, %p +%d s->end, end-data %d, size %d\n", length, s->data, s->p, s->p - s->data, s->end, s->end - s->p, s->end - s->data, s->size);
s->data = (uint8 *) xrealloc(s->data, new_length);
s->size = new_length;
s->p = s->data + p_offset;
s->end = s->data + end_offset;
}
}
while (length > 0) {
rcvd = hydra_recv(g_sock, (char *) s->end, length);
if (rcvd < 0) {
if (rcvd == -1 && TCP_BLOCKS) {
rcvd = 0;
} else {
//error("recv: %s\n", TCP_STRERROR);
return NULL;
}
} else if (rcvd == 0) {
error("Connection closed\n");
return NULL;
}
s->end += rcvd;
length -= rcvd;
}
return s;
}
char *tcp_get_address() {
static char ipaddr[32];
struct sockaddr_in sockaddr;
socklen_t len = sizeof(sockaddr);
if (getsockname(g_sock, (struct sockaddr *) &sockaddr, &len) == 0) {
uint8 *ip = (uint8 *) & sockaddr.sin_addr;
sprintf(ipaddr, "%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]);
} else
strcpy(ipaddr, "127.0.0.1");
return ipaddr;
}
/* reset the state of the tcp layer */
void tcp_reset_state(void) {
int32_t i;
g_sock = -1; /* reset socket */
/* Clear the incoming stream */
if (g_in.data != NULL)
free(g_in.data);
g_in.p = NULL;
g_in.end = NULL;
g_in.data = NULL;
g_in.size = 0;
g_in.iso_hdr = NULL;
g_in.mcs_hdr = NULL;
g_in.sec_hdr = NULL;
g_in.rdp_hdr = NULL;
g_in.channel_hdr = NULL;
/* Clear the outgoing stream(s) */
for (i = 0; i < STREAM_COUNT; i++) {
if (g_out[i].data != NULL)
free(g_out[i].data);
g_out[i].p = NULL;
g_out[i].end = NULL;
g_out[i].data = NULL;
g_out[i].size = 0;
g_out[i].iso_hdr = NULL;
g_out[i].mcs_hdr = NULL;
g_out[i].sec_hdr = NULL;
g_out[i].rdp_hdr = NULL;
g_out[i].channel_hdr = NULL;
}
}
uint16 g_mcs_userid;
/* Parse an ASN.1 BER header */
static BOOL ber_parse_header(STREAM s, int32_t tagval, int32_t *length) {
int32_t tag, len;
if (tagval > 0xff) {
in_uint16_be(s, tag);
} else {
in_uint8(s, tag);
}
if (tag != tagval) {
error("expected tag %d, got %d\n", tagval, tag);
return False;
}
in_uint8(s, len);
if (len & 0x80) {
len &= ~0x80;
*length = 0;
while (len--)
next_be(s, *length);
} else
*length = len;
return s_check(s);
}
/* Output an ASN.1 BER header */
static void ber_out_header(STREAM s, int32_t tagval, int32_t length) {
if (tagval > 0xff) {
out_uint16_be(s, tagval);
} else {
out_uint8(s, tagval);
}
if (length >= 0x80) {
out_uint8(s, 0x82);
out_uint16_be(s, length);
} else
out_uint8(s, length);
}
/* Output an ASN.1 BER integer */
static void ber_out_integer(STREAM s, int32_t value) {
ber_out_header(s, BER_TAG_INTEGER, 2);
out_uint16_be(s, value);
}
/* Output a DOMAIN_PARAMS structure (ASN.1 BER) */
static void mcs_out_domain_params(STREAM s, int32_t max_channels, int32_t max_users, int32_t max_tokens, int32_t max_pdusize) {
ber_out_header(s, MCS_TAG_DOMAIN_PARAMS, 32);
ber_out_integer(s, max_channels);
ber_out_integer(s, max_users);
ber_out_integer(s, max_tokens);
ber_out_integer(s, 1); /* num_priorities */
ber_out_integer(s, 0); /* min_throughput */
ber_out_integer(s, 1); /* max_height */
ber_out_integer(s, max_pdusize);
ber_out_integer(s, 2); /* ver_protocol */
}
/* Parse a DOMAIN_PARAMS structure (ASN.1 BER) */
static BOOL mcs_parse_domain_params(STREAM s) {
int32_t length = 0;
ber_parse_header(s, MCS_TAG_DOMAIN_PARAMS, &length);
in_uint8s(s, length);
return s_check(s);
}
/* Send an MCS_CONNECT_INITIAL message (ASN.1 BER) */
static void mcs_send_connect_initial(STREAM mcs_data) {
int32_t datalen = mcs_data->end - mcs_data->data;
int32_t length = 9 + 3 * 34 + 4 + datalen;
STREAM s;
s = iso_init(length + 5);
ber_out_header(s, MCS_CONNECT_INITIAL, length);
ber_out_header(s, BER_TAG_OCTET_STRING, 1); /* calling domain */
out_uint8(s, 1);
ber_out_header(s, BER_TAG_OCTET_STRING, 1); /* called domain */
out_uint8(s, 1);
ber_out_header(s, BER_TAG_BOOLEAN, 1);
out_uint8(s, 0xff); /* upward flag */
mcs_out_domain_params(s, 34, 2, 0, 0xffff); /* target params */
mcs_out_domain_params(s, 1, 1, 1, 0x420); /* min params */
mcs_out_domain_params(s, 0xffff, 0xfc17, 0xffff, 0xffff); /* max params */
ber_out_header(s, BER_TAG_OCTET_STRING, datalen);
out_uint8p(s, mcs_data->data, datalen);
s_mark_end(s);
iso_send(s);
}
/* Expect a MCS_CONNECT_RESPONSE message (ASN.1 BER) */
static BOOL mcs_recv_connect_response(STREAM mcs_data) {
uint8 result;
int32_t length = 0;
STREAM s;
s = iso_recv(NULL);
if (s == NULL)
return False;
ber_parse_header(s, MCS_CONNECT_RESPONSE, &length);
ber_parse_header(s, BER_TAG_RESULT, &length);
in_uint8(s, result);
if (result != 0) {
error("MCS connect: %d\n", result);
return False;
}
ber_parse_header(s, BER_TAG_INTEGER, &length);
in_uint8s(s, length); /* connect id */
mcs_parse_domain_params(s);
ber_parse_header(s, BER_TAG_OCTET_STRING, &length);
sec_process_mcs_data(s);
/*
if (length > mcs_data->size)
{
error("MCS data length %d, expected %d\n", length,
mcs_data->size);
length = mcs_data->size;
}
in_uint8a(s, mcs_data->data, length);
mcs_data->p = mcs_data->data;
mcs_data->end = mcs_data->data + length;
*/
return s_check_end(s);
}
/* Send an EDrq message (ASN.1 PER) */
static void mcs_send_edrq(void) {
STREAM s;
s = iso_init(5);
out_uint8(s, (MCS_EDRQ << 2));
out_uint16_be(s, 1); /* height */
out_uint16_be(s, 1); /* interval */
s_mark_end(s);
iso_send(s);
}
/* Send an AUrq message (ASN.1 PER) */
static void mcs_send_aurq(void) {
STREAM s;
s = iso_init(1);
out_uint8(s, (MCS_AURQ << 2));
s_mark_end(s);
iso_send(s);
}
/* Expect a AUcf message (ASN.1 PER) */
static BOOL mcs_recv_aucf(uint16 * mcs_userid) {
uint8 opcode, result;
STREAM s;
s = iso_recv(NULL);
if (s == NULL)
return False;
in_uint8(s, opcode);
if ((opcode >> 2) != MCS_AUCF) {
error("expected AUcf, got %d\n", opcode);
return False;
}
in_uint8(s, result);
if (result != 0) {
error("AUrq: %d\n", result);
return False;
}
if (opcode & 2)
in_uint16_be(s, *mcs_userid);
return s_check_end(s);
}
/* Send a CJrq message (ASN.1 PER) */
static void mcs_send_cjrq(uint16 chanid) {
STREAM s;
DEBUG_RDP5(("Sending CJRQ for channel #%d\n", chanid));
s = iso_init(5);
out_uint8(s, (MCS_CJRQ << 2));
out_uint16_be(s, g_mcs_userid);
out_uint16_be(s, chanid);
s_mark_end(s);
iso_send(s);
}
/* Expect a CJcf message (ASN.1 PER) */
static BOOL mcs_recv_cjcf(void) {
uint8 opcode, result;
STREAM s;
s = iso_recv(NULL);
if (s == NULL)
return False;
in_uint8(s, opcode);
if ((opcode >> 2) != MCS_CJCF) {
error("expected CJcf, got %d\n", opcode);
return False;
}
in_uint8(s, result);
if (result != 0) {
error("CJrq: %d\n", result);
return False;
}
in_uint8s(s, 4); /* mcs_userid, req_chanid */
if (opcode & 2)
in_uint8s(s, 2); /* join_chanid */
return s_check_end(s);
}
/* Initialise an MCS transport data packet */
STREAM mcs_init(int32_t length) {
STREAM s;
s = iso_init(length + 8);
s_push_layer(s, mcs_hdr, 8);
return s;
}
/* Send an MCS transport data packet to a specific channel */
void mcs_send_to_channel(STREAM s, uint16 channel) {
uint16 length;
s_pop_layer(s, mcs_hdr);
length = s->end - s->p - 8;
length |= 0x8000;
out_uint8(s, (MCS_SDRQ << 2));
out_uint16_be(s, g_mcs_userid);
out_uint16_be(s, channel);
out_uint8(s, 0x70); /* flags */
out_uint16_be(s, length);
iso_send(s);
}
/* Send an MCS transport data packet to the global channel */
void mcs_send(STREAM s) {
mcs_send_to_channel(s, MCS_GLOBAL_CHANNEL);
}
/* Receive an MCS transport data packet */
STREAM mcs_recv(uint16 * channel, uint8 * rdpver) {
uint8 opcode, appid, length;
STREAM s;
s = iso_recv(rdpver);
if (s == NULL)
return NULL;
if (rdpver != NULL)
if (*rdpver != 3)
return s;
in_uint8(s, opcode);
appid = opcode >> 2;
if (appid != MCS_SDIN) {
if (appid != MCS_DPUM) {
error("expected data, got %d\n", opcode);
}
return NULL;
}
in_uint8s(s, 2); /* userid */
in_uint16_be(s, *channel);
in_uint8s(s, 1); /* flags */
in_uint8(s, length);
if (length & 0x80)
in_uint8s(s, 1); /* second byte of length */
return s;
}
BOOL mcs_connect(char *server, STREAM mcs_data, char *username, BOOL reconnect) {
if (!iso_connect(server, username, reconnect))
return False;
mcs_send_connect_initial(mcs_data);
if (!mcs_recv_connect_response(mcs_data))
goto error;
mcs_send_edrq();
mcs_send_aurq();
if (!mcs_recv_aucf(&g_mcs_userid))
goto error;
mcs_send_cjrq(g_mcs_userid + MCS_USERCHANNEL_BASE);
if (!mcs_recv_cjcf())
goto error;
mcs_send_cjrq(MCS_GLOBAL_CHANNEL);
if (!mcs_recv_cjcf())
goto error;
return True;
error:
iso_disconnect();
return False;
}
/* Disconnect from the MCS layer */
void mcs_disconnect(void) {
iso_disconnect();
}
/* reset the state of the mcs layer */
void mcs_reset_state(void) {
g_mcs_userid = 0;
iso_reset_state();
}
/* Send a self-contained ISO PDU */
static void iso_send_msg(uint8 code) {
STREAM s;
s = tcp_init(11);
out_uint8(s, 3); /* version */
out_uint8(s, 0); /* reserved */
out_uint16_be(s, 11); /* length */
out_uint8(s, 6); /* hdrlen */
out_uint8(s, code);
out_uint16(s, 0); /* dst_ref */
out_uint16(s, 0); /* src_ref */
out_uint8(s, 0); /* class */
s_mark_end(s);
tcp_send(s);
}
static void iso_send_connection_request(char *username) {
STREAM s;
int32_t length = 30 + strlen(username);
s = tcp_init(length);
out_uint8(s, 3); /* version */
out_uint8(s, 0); /* reserved */
out_uint16_be(s, length); /* length */
out_uint8(s, length - 5); /* hdrlen */
out_uint8(s, ISO_PDU_CR);
out_uint16(s, 0); /* dst_ref */
out_uint16(s, 0); /* src_ref */
out_uint8(s, 0); /* class */
out_uint8p(s, "Cookie: mstshash=", strlen("Cookie: mstshash="));
out_uint8p(s, username, strlen(username));
out_uint8(s, 0x0d); /* Unknown */
out_uint8(s, 0x0a); /* Unknown */
s_mark_end(s);
tcp_send(s);
}
/* Send a single input event fast JL, this is required for win8 */
void rdp_send_fast_input_kbd(uint32 time, uint16 flags, uint16 param1) {
STREAM s;
uint8 fast_flags = 0;
uint8 len = 4;
fast_flags |= (flags & RDP_KEYRELEASE) ? FASTPATH_INPUT_KBDFLAGS_RELEASE : 0;
s = tcp_init(len);
out_uint8(s, (1 << 2)); //one event
out_uint8(s, len);
out_uint8(s, fast_flags | (FASTPATH_INPUT_EVENT_SCANCODE << 5));
out_uint8(s, param1);
s_mark_end(s);
tcp_send(s);
}
/* Send a single input event fast JL, this is required for win8 */
void rdp_send_fast_input_mouse(uint32 time, uint16 flags, uint16 param1, uint16 param2) {
STREAM s;
uint8 len = 9;
s = tcp_init(len);
out_uint8(s, (1 << 2)); //one event
out_uint8(s, len);
out_uint8(s, (FASTPATH_INPUT_EVENT_MOUSE << 5));
out_uint16(s, flags);
out_uint16(s, param1);
out_uint16(s, param2);
s_mark_end(s);
tcp_send(s);
}
/* Receive a message on the ISO layer, return code */
static STREAM iso_recv_msg(uint8 * code, uint8 * rdpver) {
STREAM s;
uint16 length;
uint8 version;
s = tcp_recv(NULL, 4);
if (s == NULL)
return NULL;
in_uint8(s, version);
if (rdpver != NULL)
*rdpver = version;
if (version == 3) {
in_uint8s(s, 1); /* pad */
in_uint16_be(s, length);
} else {
in_uint8(s, length);
if (length & 0x80) {
length &= ~0x80;
next_be(s, length);
}
}
if (length < 5) {
error("Bad packet header\n");
return NULL;
}
s = tcp_recv(s, length - 4);
if (s == NULL)
return NULL;
if (version != 3)
return s;
in_uint8s(s, 1); /* hdrlen */
in_uint8(s, *code);
if (*code == ISO_PDU_DT) {
in_uint8s(s, 1); /* eot */
return s;
}
in_uint8s(s, 5); /* dst_ref, src_ref, class */
return s;
}
/* Initialise ISO transport data packet */
STREAM iso_init(int32_t length) {
STREAM s;
s = tcp_init(length + 7);
s_push_layer(s, iso_hdr, 7);
return s;
}
/* Send an ISO data PDU */
void iso_send(STREAM s) {
uint16 length;
s_pop_layer(s, iso_hdr);
length = s->end - s->p;
out_uint8(s, 3); /* version */
out_uint8(s, 0); /* reserved */
out_uint16_be(s, length);
out_uint8(s, 2); /* hdrlen */
out_uint8(s, ISO_PDU_DT); /* code */
out_uint8(s, 0x80); /* eot */
tcp_send(s);
}
/* Receive ISO transport data packet */
STREAM iso_recv(uint8 * rdpver) {
STREAM s;
uint8 code = 0;
s = iso_recv_msg(&code, rdpver);
if (s == NULL)
return NULL;
if (rdpver != NULL)
if (*rdpver != 3)
return s;
if (code != ISO_PDU_DT) {
error("expected DT, got 0x%x\n", code);
return NULL;
}
return s;
}
/* Establish a connection up to the ISO layer */
BOOL iso_connect(char *server, char *username, BOOL reconnect) {
uint8 code = 0;
if (reconnect) {
iso_send_msg(ISO_PDU_CR);
} else {
iso_send_connection_request(username);
}
if (iso_recv_msg(&code, NULL) == NULL) {
return False;
}
if (code != ISO_PDU_CC) {
error("expected CC, got 0x%x\n", code);
hydra_disconnect(g_sock);
return False;
}
return True;
}
/* Disconnect from the ISO layer */
void iso_disconnect(void) {
iso_send_msg(ISO_PDU_DR);
g_sock = hydra_disconnect(g_sock);
}
/* reset the state to support reconnecting */
void iso_reset_state(void) {
tcp_reset_state();
}
static int32_t g_rc4_key_len;
static SSL_RC4 g_rc4_decrypt_key;
static SSL_RC4 g_rc4_encrypt_key;
static uint32 g_server_public_key_len;
static uint8 g_sec_sign_key[16];
static uint8 g_sec_decrypt_key[16];
static uint8 g_sec_encrypt_key[16];
static uint8 g_sec_decrypt_update_key[16];
static uint8 g_sec_encrypt_update_key[16];
static uint8 g_sec_crypted_random[SEC_MAX_MODULUS_SIZE];
uint16 g_server_rdp_version = 0;
/* These values must be available to reset state - Session Directory */
static int32_t g_sec_encrypt_use_count = 0;
static int32_t g_sec_decrypt_use_count = 0;
void ssl_sha1_init(SSL_SHA1 * sha1) {
SHA1_Init(sha1);
}
void ssl_sha1_update(SSL_SHA1 * sha1, uint8 * data, uint32 len) {
SHA1_Update(sha1, data, len);
}
void ssl_sha1_final(SSL_SHA1 * sha1, uint8 * out_data) {
SHA1_Final(out_data, sha1);
}
void ssl_md5_init(SSL_MD5 * md5) {
MD5_Init(md5);
}
void ssl_md5_update(SSL_MD5 * md5, uint8 * data, uint32 len) {
MD5_Update(md5, data, len);
}
void ssl_md5_final(SSL_MD5 * md5, uint8 * out_data) {
MD5_Final(out_data, md5);
}
void ssl_rc4_set_key(SSL_RC4 * rc4, uint8 * key, uint32 len) {
RC4_set_key(rc4, len, key);
}
void ssl_rc4_crypt(SSL_RC4 * rc4, uint8 * in_data, uint8 * out_data, uint32 len) {
RC4(rc4, len, in_data, out_data);
}
static void reverse(uint8 * p, int32_t len) {
int32_t i, j;
uint8 temp;
for (i = 0, j = len - 1; i < j; i++, j--) {
temp = p[i];
p[i] = p[j];
p[j] = temp;
}
}
void ssl_rsa_encrypt(uint8 * out, uint8 * in, int32_t len, uint32 modulus_size, uint8 * modulus, uint8 * exponent) {
BN_CTX *ctx;
BIGNUM *mod, *exp, *x, *y;
uint8 inr[SEC_MAX_MODULUS_SIZE];
int32_t outlen;
reverse(modulus, modulus_size);
reverse(exponent, SEC_EXPONENT_SIZE);
memcpy(inr, in, len);
reverse(inr, len);
ctx = BN_CTX_new();
mod = BN_new();
exp = BN_new();
x = BN_new();
y = BN_new();
BN_bin2bn(modulus, modulus_size, mod);
BN_bin2bn(exponent, SEC_EXPONENT_SIZE, exp);
BN_bin2bn(inr, len, x);
BN_mod_exp(y, x, exp, mod, ctx);
outlen = BN_bn2bin(y, out);
reverse(out, outlen);
if (outlen < (int32_t) modulus_size)
memset(out + outlen, 0, modulus_size - outlen);
BN_free(y);
BN_free(x);
BN_free(exp);
BN_free(mod);
BN_CTX_free(ctx);
}
/* returns newly allocated X509 or NULL */
X509 *ssl_cert_read(uint8 * data, uint32 len) {
/* this will move the data pointer but we don't care, we don't use it again */
return d2i_X509(NULL, (D2I_X509_CONST unsigned char **) &data, len);
}
static void ssl_cert_free(X509 * cert) {
X509_free(cert);
}
/* returns newly allocated SSL_RKEY or NULL */
SSL_RKEY *ssl_cert_to_rkey(X509 * cert, uint32 * key_len) {
EVP_PKEY *epk = NULL;
SSL_RKEY *lkey;
int32_t nid;
/* By some reason, Microsoft sets the OID of the Public RSA key to
the oid for "MD5 with RSA Encryption" instead of "RSA Encryption"
Kudos to Richard Levitte for the following (. intuitive .)
lines of code that resets the OID and let's us extract the key. */
#if OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(LIBRESSL_VERSION_NUMBER)
nid = X509_get_signature_nid(cert);
#else
nid = OBJ_obj2nid(cert->cert_info->key->algor->algorithm);
#endif
if ((nid == NID_md5WithRSAEncryption) || (nid == NID_shaWithRSAEncryption)) {
#if OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(LIBRESSL_VERSION_NUMBER)
// fprintf(stderr, "[ERROR] the current experimental openssl-1.1 support in hydra does not support RDP :( \n");
// hydra_child_exit(2);
X509_ALGOR *algor = X509_get0_tbs_sigalg(cert);
DEBUG_RDP5(("Re-setting algorithm type to RSA in server certificate\n"));
ASN1_OBJECT_free(algor->algorithm);
algor->algorithm = OBJ_nid2obj(NID_rsaEncryption);
//X509_ALGOR_set0(algor, OBJ_nid2obj(NID_rsaEncryption), V_ASN1_SEQUENCE, NULL /*pbe_str*/);
#else
DEBUG_RDP5(("Re-setting algorithm type to RSA in server certificate\n"));
ASN1_OBJECT_free(cert->cert_info->key->algor->algorithm);
cert->cert_info->key->algor->algorithm = OBJ_nid2obj(NID_rsaEncryption);
#endif
}
epk = X509_get_pubkey(cert);
if (NULL == epk) {
error("Failed to extract public key from certificate\n");
return NULL;
}
lkey = RSAPublicKey_dup(EVP_PKEY_get1_RSA(epk));
EVP_PKEY_free(epk);
*key_len = RSA_size(lkey);
return lkey;
}
int32_t ssl_cert_print_fp(FILE * fp, X509 * cert) {
return X509_print_fp(fp, cert);
}
void ssl_rkey_free(SSL_RKEY * rkey) {
RSA_free(rkey);
}
/* returns error */
int32_t ssl_rkey_get_exp_mod(SSL_RKEY * rkey, uint8 * exponent, uint32 max_exp_len, uint8 * modulus, uint32 max_mod_len) {
int32_t len;
#if OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(LIBRESSL_VERSION_NUMBER)
BIGNUM *n, *e, *d;
n = BN_new();
e = BN_new();
RSA_get0_key(rkey, &n, &e, NULL);
if ((BN_num_bytes(e) > (int32_t) max_exp_len) || (BN_num_bytes(n) > (int32_t) max_mod_len)) {
return 1;
}
len = BN_bn2bin(e, exponent);
reverse(exponent, len);
len = BN_bn2bin(n, modulus);
reverse(modulus, len);
BN_free(n);
BN_free(e);
#else
if ((BN_num_bytes(rkey->e) > (int32_t) max_exp_len) || (BN_num_bytes(rkey->n) > (int32_t) max_mod_len))
return 1;
len = BN_bn2bin(rkey->e, exponent);
reverse(exponent, len);
len = BN_bn2bin(rkey->n, modulus);
reverse(modulus, len);
#endif
return 0;
}
/* returns boolean */
BOOL ssl_sig_ok(uint8 * exponent, uint32 exp_len, uint8 * modulus, uint32 mod_len, uint8 * signature, uint32 sig_len) {
return True;
}
void ssl_hmac_md5(const void *key, int32_t key_len, const unsigned char *msg, int32_t msg_len, unsigned char *md) {
#if OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(LIBRESSL_VERSION_NUMBER)
HMAC_CTX *ctx;
ctx = HMAC_CTX_new();
HMAC(EVP_md5(), key, key_len, msg, msg_len, md, NULL);
HMAC_CTX_free(ctx);
#else
HMAC_CTX ctx;
HMAC_CTX_init(&ctx);
HMAC(EVP_md5(), key, key_len, msg, msg_len, md, NULL);