-
Notifications
You must be signed in to change notification settings - Fork 4
/
hydra-mod.c
1133 lines (1028 loc) · 31 KB
/
hydra-mod.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
#include "hydra-mod.h"
#include <arpa/inet.h>
#ifdef LIBOPENSSL
#include <openssl/ssl.h>
#include <openssl/err.h>
#endif
#ifdef HAVE_PCRE
#include <pcre.h>
#endif
#define MAX_CONNECT_RETRY 1
#define WAIT_BETWEEN_CONNECT_RETRY 3
#define HYDRA_DUMP_ROWS 16
/* rfc 1928 SOCKS proxy */
#define SOCKS_V5 5
#define SOCKS_V4 4
#define SOCKS_NOAUTH 0
/* http://tools.ietf.org/html/rfc1929 */
#define SOCKS_PASSAUTH 2
#define SOCKS_NOMETHOD 0xff
#define SOCKS_CONNECT 1
#define SOCKS_IPV4 1
#define SOCKS_DOMAIN 3
#define SOCKS_IPV6 4
extern int conwait;
int module_auth_type = -1;
int intern_socket, extern_socket;
char pair[260];
char HYDRA_EXIT[5] = "\x00\xff\x00\xff\x00";
char *HYDRA_EMPTY = "\x00\x00\x00\x00";
char *fe80 = "\xfe\x80\x00";
int fail = 0;
int alarm_went_off = 0;
int use_ssl = 0;
char ipaddr_str[64];
int src_port = 0;
int __fck = 0;
int ssl_first = 1;
int __first_connect = 1;
char ipstring[64];
#ifdef LIBOPENSSL
SSL *ssl = NULL;
SSL_CTX *sslContext = NULL;
RSA *rsa = NULL;
#endif
/* prototype */
int my_select(int fd, fd_set * fdread, fd_set * fdwrite, fd_set * fdex, long sec, long usec);
/* ----------------- alarming functions ---------------- */
void alarming() {
fail++;
alarm_went_off++;
/* uh, I think it's not good for performance if we try to reconnect to a timeout system!
* if (fail > MAX_CONNECT_RETRY) {
*/
fprintf(stderr, "Process %d: Can not connect [timeout], process exiting\n", (int) getpid());
if (debug)
printf("DEBUG_CONNECT_TIMEOUT\n");
hydra_child_exit(1);
/*
* } else {
* if (verbose) fprintf(stderr, "Process %d: Can not connect [timeout], retrying (%d of %d retries)\n", (int)getpid(), fail, MAX_CONNECT_RETRY);
* }
*/
}
void interrupt() {
if (debug)
printf("DEBUG_INTERRUPTED\n");
}
/* ----------------- internal functions ----------------- */
int internal__hydra_connect(char *host, int port, int protocol, int type) {
int s, ret = -1, ipv6 = 0;
#ifdef AF_INET6
struct sockaddr_in6 target6;
struct sockaddr_in6 sin6;
#endif
struct sockaddr_in target;
struct sockaddr_in sin;
char *buf, *tmpptr = NULL;
int err = 0;
#ifdef AF_INET6
memset(&target6, 0, sizeof(target6));
memset(&sin6, 0, sizeof(sin6));
if ((host[0] == 16 && proxy_string_ip[0] != 4) || proxy_string_ip[0] == 16)
ipv6 = 1;
#endif
#ifdef AF_INET6
if (ipv6)
s = socket(AF_INET6, protocol, type);
else
#endif
s = socket(PF_INET, protocol, type);
if (s >= 0) {
if (src_port != 0) {
int bind_ok = 0;
#ifdef AF_INET6
if (ipv6) {
sin6.sin6_family = AF_INET6;
sin6.sin6_port = htons(src_port);
} else
#endif
{
sin.sin_family = PF_INET;
sin.sin_port = htons(src_port);
sin.sin_addr.s_addr = INADDR_ANY;
}
//we will try to find a free port down to 512
while (!bind_ok && src_port >= 512) {
#ifdef AF_INET6
if (ipv6)
ret = bind(s, (struct sockaddr *) &sin6, sizeof(sin6));
else
#endif
ret = bind(s, (struct sockaddr *) &sin, sizeof(sin));
if (ret == -1) {
if (verbose)
perror("internal_hydra_connect error");
if (errno == EADDRINUSE) {
src_port--;
#ifdef AF_INET6
if (ipv6)
sin6.sin6_port = htons(src_port);
else
#endif
sin.sin_port = htons(src_port);
} else {
if (errno == EACCES && (getuid() > 0)) {
printf("You need to be root to test this service\n");
return -1;
}
}
} else
bind_ok = 1;
}
}
if (use_proxy > 0) {
if (proxy_string_ip[0] == 4) {
memcpy(&target.sin_addr.s_addr, &proxy_string_ip[1], 4);
target.sin_family = AF_INET;
target.sin_port = htons(proxy_string_port);
}
#ifdef AF_INET6
if (proxy_string_ip[0] == 16) {
memcpy(&target6.sin6_addr, &proxy_string_ip[1], 16);
target6.sin6_family = AF_INET6;
target6.sin6_port = htons(proxy_string_port);
}
#endif
} else {
if (host[0] == 4) {
memcpy(&target.sin_addr.s_addr, &host[1], 4);
target.sin_family = AF_INET;
target.sin_port = htons(port);
}
#ifdef AF_INET6
if (host[0] == 16) {
memcpy(&target6.sin6_addr, &host[1], 16);
target6.sin6_family = AF_INET6;
target6.sin6_port = htons(port);
}
#endif
}
signal(SIGALRM, alarming);
do {
if (fail > 0)
sleep(WAIT_BETWEEN_CONNECT_RETRY);
alarm_went_off = 0;
alarm(waittime);
#ifdef AF_INET6
#ifdef SO_BINDTODEVICE
if (host[17] != 0) {
setsockopt(s, SOL_SOCKET, SO_BINDTODEVICE, &host[17], strlen(&host[17]) + 1);
}
#else
#ifdef IP_FORCE_OUT_IFP
if (host[17] != 0) {
setsockopt(s, SOL_SOCKET, IP_FORCE_OUT_IFP, &host[17], strlen(&host[17]) + 1);
}
#endif
#endif
if (ipv6)
ret = connect(s, (struct sockaddr *) &target6, sizeof(target6));
else
#endif
ret = connect(s, (struct sockaddr *) &target, sizeof(target));
alarm(0);
if (ret < 0 && alarm_went_off == 0) {
fail++;
if (verbose && fail <= MAX_CONNECT_RETRY)
fprintf(stderr, "Process %d: Can not connect [unreachable], retrying (%d of %d retries)\n", (int) getpid(), fail, MAX_CONNECT_RETRY);
}
} while (ret < 0 && fail <= MAX_CONNECT_RETRY);
if (ret < 0 && fail > MAX_CONNECT_RETRY) {
if (debug)
printf("DEBUG_CONNECT_UNREACHABLE\n");
/* we wont quit here, thats up to the module to decide what to do
* fprintf(stderr, "Process %d: Can not connect [unreachable], process exiting\n", (int)getpid());
* hydra_child_exit(1);
*/
extern_socket = -1;
ret = -1;
return ret;
}
ret = s;
extern_socket = s;
if (debug)
printf("DEBUG_CONNECT_OK\n");
err = 0;
if (use_proxy == 2) {
if ((buf = malloc(4096)) == NULL) {
fprintf(stderr, "[ERROR] could not malloc()\n");
return -1;
}
memset(&target, 0, sizeof(target));
if (host[0] == 4) {
memcpy(&target.sin_addr.s_addr, &host[1], 4);
target.sin_family = AF_INET;
target.sin_port = htons(port);
}
#ifdef AF_INET6
memset(&target6, 0, sizeof(target6));
if (host[0] == 16) {
memcpy(&target6.sin6_addr, &host[1], 16);
target6.sin6_family = AF_INET6;
target6.sin6_port = htons(port);
}
#endif
if (hydra_strcasestr(proxy_string_type, "connect") || hydra_strcasestr(proxy_string_type, "http")) {
if (proxy_authentication == NULL)
if (host[0] == 16)
snprintf(buf, 4096, "CONNECT [%s]:%d HTTP/1.0\r\n\r\n", hydra_address2string(host), port);
else
snprintf(buf, 4096, "CONNECT %s:%d HTTP/1.0\r\n\r\n", hydra_address2string(host), port);
else
if (host[0] == 16)
snprintf(buf, 4096, "CONNECT [%s]:%d HTTP/1.0\r\nProxy-Authorization: Basic %s\r\n\r\n", hydra_address2string(host), port, proxy_authentication);
else
snprintf(buf, 4096, "CONNECT %s:%d HTTP/1.0\r\nProxy-Authorization: Basic %s\r\n\r\n", hydra_address2string(host), port, proxy_authentication);
send(s, buf, strlen(buf), 0);
recv(s, buf, 4096, 0);
if (strncmp("HTTP/", buf, 5) == 0 && (tmpptr = index(buf, ' ')) != NULL && *++tmpptr == '2') {
if (debug)
printf("DEBUG_CONNECT_PROXY_OK\n");
} else {
if (debug)
printf("DEBUG_CONNECT_PROXY_FAILED (Code: %c%c%c)\n", *tmpptr, *(tmpptr + 1), *(tmpptr + 2));
if (verbose)
fprintf(stderr, "[ERROR] CONNECT call to proxy failed with code %c%c%c\n", *tmpptr, *(tmpptr + 1), *(tmpptr + 2));
err = 1;
}
free(buf);
} else {
if (hydra_strcasestr(proxy_string_type, "socks5")) {
char buf[1024];
size_t cnt, wlen;
/* socks v5 support */
buf[0] = SOCKS_V5;
buf[1] = 1;
if (proxy_authentication == NULL)
buf[2] = SOCKS_NOAUTH;
else
buf[2] = SOCKS_PASSAUTH;
cnt = hydra_send(s, buf, 3, 0);
if (cnt != 3) {
hydra_report(stderr, "[ERROR] SOCKS5 proxy write failed (%zu/3)\n", cnt);
err = 1;
} else {
cnt = hydra_recv(s, buf, 2);
if (cnt != 2) {
hydra_report(stderr, "[ERROR] SOCKS5 proxy read failed (%zu/2)\n", cnt);
err = 1;
}
if (buf[1] == SOCKS_NOMETHOD) {
hydra_report(stderr, "[ERROR] SOCKS5 proxy authentication method negotiation failed\n");
err = 1;
}
/* SOCKS_DOMAIN not supported here, do we need it ? */
if (err != 1) {
/* send user/pass */
if (proxy_authentication != NULL) {
//format was checked previously
char *login=strtok(proxy_authentication, ":");
char *pass=strtok(NULL, ":");
snprintf(buf, sizeof(buf), "\x01%c%s%c%s", (char) strlen(login), login, (char) strlen(pass), pass);
cnt = hydra_send(s, buf, strlen(buf), 0);
if (cnt != strlen(buf)) {
hydra_report(stderr, "[ERROR] SOCKS5 proxy write failed (%zu/3)\n", cnt);
err = 1;
} else {
cnt = hydra_recv(s, buf, 2);
if (cnt != 2) {
hydra_report(stderr, "[ERROR] SOCKS5 proxy read failed (%zu/2)\n", cnt);
err = 1;
}
if (buf[1] != 0) {
hydra_report(stderr, "[ERROR] SOCKS5 proxy authentication failure\n");
err = 1;
} else {
if (debug)
hydra_report(stderr, "[DEBUG] SOCKS5 proxy authentication success\n");
}
}
}
#ifdef AF_INET6
if (ipv6) {
/* Version 5, connect: IPv6 address */
buf[0] = SOCKS_V5;
buf[1] = SOCKS_CONNECT;
buf[2] = 0;
buf[3] = SOCKS_IPV6;
memcpy(buf + 4, &target6.sin6_addr, sizeof target6.sin6_addr);
memcpy(buf + 20, &target6.sin6_port, sizeof target6.sin6_port);
wlen = 22;
} else {
#endif
/* Version 5, connect: IPv4 address */
buf[0] = SOCKS_V5;
buf[1] = SOCKS_CONNECT;
buf[2] = 0;
buf[3] = SOCKS_IPV4;
memcpy(buf + 4, &target.sin_addr, sizeof target.sin_addr);
memcpy(buf + 8, &target.sin_port, sizeof target.sin_port);
wlen = 10;
#ifdef AF_INET6
}
#endif
cnt = hydra_send(s, buf, wlen, 0);
if (cnt != wlen) {
hydra_report(stderr, "[ERROR] SOCKS5 proxy write failed (%zu/%zu)\n", cnt, wlen);
err = 1;
} else {
cnt=hydra_recv(s, buf, 10);
if (cnt != 10) {
hydra_report(stderr, "[ERROR] SOCKS5 proxy read failed (%zu/10)\n", cnt);
err = 1;
}
if (buf[1] != 0) {
/* 0x05 = connection refused by destination host */
if (buf[1] == 5)
hydra_report(stderr, "[ERROR] SOCKS proxy request failed\n");
else
hydra_report(stderr, "[ERROR] SOCKS error %d\n", buf[1]);
err = 1;
}
}
}
}
} else {
if (hydra_strcasestr(proxy_string_type, "socks4")) {
if (ipv6) {
hydra_report(stderr, "[ERROR] SOCKS4 proxy does not support IPv6\n");
err = 1;
} else {
char buf[1024];
size_t cnt, wlen;
/* socks v4 support */
buf[0] = SOCKS_V4;
buf[1] = SOCKS_CONNECT; /* connect */
memcpy(buf + 2, &target.sin_port, sizeof target.sin_port);
memcpy(buf + 4, &target.sin_addr, sizeof target.sin_addr);
buf[8] = 0; /* empty username */
wlen = 9;
cnt = hydra_send(s, buf, wlen, 0);
if (cnt != wlen) {
hydra_report(stderr, "[ERROR] SOCKS4 proxy write failed (%zu/%zu)\n", cnt, wlen);
err = 1;
} else {
cnt=hydra_recv(s, buf, 8);
if (cnt != 8) {
hydra_report(stderr, "[ERROR] SOCKS4 proxy read failed (%zu/8)\n", cnt);
err = 1;
}
if (buf[1] != 90) {
/* 91 = 0x5b = request rejected or failed */
if (buf[1] == 91)
hydra_report(stderr, "[ERROR] SOCKS proxy request failed\n");
else
hydra_report(stderr, "[ERROR] SOCKS error %d\n", buf[1]);
err = 1;
}
}
}
} else {
hydra_report(stderr, "[ERROR] Unknown proxy type: %s, valid type are \"connect\", \"socks4\" or \"socks5\"\n", proxy_string_type);
err = 1;
}
}
}
}
if (err) {
close(s);
extern_socket = -1;
ret = -1;
free(buf);
return ret;
}
fail = 0;
return ret;
}
return ret;
}
#ifdef LIBOPENSSL
RSA *ssl_temp_rsa_cb(SSL * ssl, int export, int keylength) {
if (rsa == NULL)
rsa = RSA_generate_key(512, RSA_F4, NULL, NULL);
return rsa;
}
int internal__hydra_connect_to_ssl(int socket) {
int err;
if (ssl_first) {
SSL_load_error_strings();
SSL_library_init();
ssl_first = 0;
}
if (sslContext == NULL) {
/* context: ssl2 + ssl3 is allowed, whatever the server demands */
if ((sslContext = SSL_CTX_new(SSLv23_client_method())) == NULL) {
if (verbose) {
err = ERR_get_error();
fprintf(stderr, "[ERROR] SSL allocating context: %s\n", ERR_error_string(err, NULL));
}
return -1;
}
/* set the compatbility mode */
SSL_CTX_set_options(sslContext, SSL_OP_ALL);
/* we set the default verifiers and dont care for the results */
(void) SSL_CTX_set_default_verify_paths(sslContext);
SSL_CTX_set_tmp_rsa_callback(sslContext, ssl_temp_rsa_cb);
SSL_CTX_set_verify(sslContext, SSL_VERIFY_NONE, NULL);
}
if ((ssl = SSL_new(sslContext)) == NULL) {
if (verbose) {
err = ERR_get_error();
fprintf(stderr, "[ERROR] preparing an SSL context: %s\n", ERR_error_string(err, NULL));
}
SSL_set_bio(ssl, NULL, NULL);
SSL_clear(ssl);
return -1;
}
SSL_set_fd(ssl, socket);
if (SSL_connect(ssl) <= 0) {
printf("[ERROR] SSL Connect %d\n", SSL_connect(ssl));
if (verbose) {
err = ERR_get_error();
fprintf(stderr, "[VERBOSE] Could not create an SSL session: %s\n", ERR_error_string(err, NULL));
}
close(socket);
return -1;
}
if (debug)
fprintf(stderr, "[VERBOSE] SSL negotiated cipher: %s\n", SSL_get_cipher(ssl));
use_ssl = 1;
return socket;
}
int internal__hydra_connect_ssl(char *host, int port, int protocol, int type) {
int socket;
if ((socket = internal__hydra_connect(host, port, protocol, type)) < 0)
return -1;
return internal__hydra_connect_to_ssl(socket);
}
#endif
int internal__hydra_recv(int socket, char *buf, int length) {
#ifdef LIBOPENSSL
if (use_ssl) {
return SSL_read(ssl, buf, length);
} else
#endif
return recv(socket, buf, length, 0);
}
int internal__hydra_send(int socket, char *buf, int size, int options) {
#ifdef LIBOPENSSL
if (use_ssl) {
return SSL_write(ssl, buf, size);
} else
#endif
return send(socket, buf, size, options);
}
/* ------------------ public functions ------------------ */
void hydra_child_exit(int code) {
char buf[2];
if (debug)
fprintf(stderr, "[DEBUG] pid %d called child_exit with code %d\n", getpid(), code);
if (code == 0) /* normal quitting */
__fck = write(intern_socket, "Q", 1);
else if (code == 1) /* no connect possible */
__fck = write(intern_socket, "C", 1);
else if (code == 2) /* application protocol error or service shutdown */
__fck = write(intern_socket, "E", 1);
// code 3 means exit without telling mommy about it - a bad idea. mommy should know
else if (code == -1 || code > 3) {
fprintf(stderr, "FUCKUP: a module should not use hydra_child_exit(-1) ! Fix it please ...\n");
__fck = write(intern_socket, "E", 1);
}
do {
usleep(10000);
} while (read(intern_socket, buf, 1) <= 0);
// sleep(2); // be sure that mommy receives our message
exit(0); // might be killed before reaching this
}
void hydra_register_socket(int s) {
intern_socket = s;
}
char *hydra_get_next_pair() {
if (pair[0] == 0) {
pair[sizeof(pair) - 1] = 0;
__fck = read(intern_socket, pair, sizeof(pair) - 1);
if (memcmp(&HYDRA_EXIT, &pair, sizeof(HYDRA_EXIT)) == 0)
return HYDRA_EXIT;
if (pair[0] == 0)
return HYDRA_EMPTY;
}
return pair;
}
char *hydra_get_next_login() {
if (pair[0] == 0)
return HYDRA_EMPTY;
return pair;
}
char *hydra_get_next_password() {
char *ptr = pair;
while (*ptr != '\0')
ptr++;
ptr++;
if (*ptr == 0)
return HYDRA_EMPTY;
return ptr;
}
void hydra_completed_pair() {
__fck = write(intern_socket, "N", 1);
pair[0] = 0;
}
void hydra_completed_pair_found() {
char *login;
__fck = write(intern_socket, "F", 1);
login = hydra_get_next_login();
__fck = write(intern_socket, login, strlen(login) + 1);
pair[0] = 0;
}
void hydra_completed_pair_skip() {
char *login;
__fck = write(intern_socket, "f", 1);
login = hydra_get_next_login();
__fck = write(intern_socket, login, strlen(login) + 1);
pair[0] = 0;
}
/*
based on writeError from Medusa project
*/
void hydra_report_debug(FILE * st, char *format, ...) {
va_list ap;
char buf[512];
char bufOut[2048];
char temp[6];
unsigned char cTemp;
int i = 0;
if (format == NULL) {
fprintf(stderr, "[ERROR] no msg passed.\n");
} else {
va_start(ap, format);
memset(bufOut, 0, sizeof(bufOut));
memset(buf, 0, 512);
vsnprintf(buf, sizeof(buf), format, ap);
// Convert any chars less than 32d or greater than 126d to hex
for (i = 0; i < sizeof(buf); i++) {
memset(temp, 0, 6);
cTemp = (unsigned char) buf[i];
if ((cTemp < 32 && cTemp > 0) || cTemp > 126) {
sprintf(temp, "[%02X]", cTemp);
} else
sprintf(temp, "%c", cTemp);
if (strlen(bufOut) + 6 < sizeof(bufOut))
strncat(bufOut, temp, 6);
else
break;
}
fprintf(st, "%s\n", bufOut);
va_end(ap);
}
return;
}
void hydra_report_found(int port, char *svc, FILE * fp) {
if (!strcmp(svc, "rsh"))
fprintf(fp, "[%d][%s] login: %s\n", port, svc, hydra_get_next_login());
else
fprintf(fp, "[%d][%s] login: %s password: %s\n", port, svc, hydra_get_next_login(), hydra_get_next_password());
fflush(fp);
}
/* needed for irc module to display the general server password */
void hydra_report_pass_found(int port, char *ip, char *svc, FILE * fp) {
strcpy(ipaddr_str, hydra_address2string(ip));
printf("[%d][%s] host: %s password: %s\n", port, svc, ipaddr_str, hydra_get_next_password());
fflush(fp);
}
void hydra_report_found_host(int port, char *ip, char *svc, FILE * fp) {
char *keyw = "password";
strcpy(ipaddr_str, hydra_address2string(ip));
if (!strcmp(svc, "smtp-enum"))
keyw = "domain";
if (!strcmp(svc, "rsh") || !strcmp(svc, "oracle-sid"))
fprintf(fp, "[%d][%s] host: %s login: %s\n", port, svc, ipaddr_str, hydra_get_next_login());
else
fprintf(fp, "[%d][%s] host: %s login: %s %s: %s\n", port, svc, ipaddr_str, hydra_get_next_login(), keyw, hydra_get_next_password());
if (stdout != fp) {
if (!strcmp(svc, "rsh") || !strcmp(svc, "oracle-sid"))
printf("[%d][%s] host: %s login: %s\n", port, svc, ipaddr_str, hydra_get_next_login());
else
printf("[%d][%s] host: %s login: %s %s: %s\n", port, svc, ipaddr_str, hydra_get_next_login(), keyw, hydra_get_next_password());
}
fflush(fp);
}
void hydra_report_found_host_msg(int port, char *ip, char *svc, FILE * fp, char *msg) {
strcpy(ipaddr_str, hydra_address2string(ip));
fprintf(fp, "[%d][%s] host: %s login: %s password: %s [%s]\n", port, svc, ipaddr_str, hydra_get_next_login(), hydra_get_next_password(), msg);
if (stdout != fp)
printf("[%d][%s] host: %s login: %s password: %s\n", port, svc, ipaddr_str, hydra_get_next_login(), hydra_get_next_password());
fflush(fp);
}
int hydra_connect_to_ssl(int socket) {
#ifdef LIBOPENSSL
return (internal__hydra_connect_to_ssl(socket));
#else
return -1;
#endif
}
int hydra_connect_ssl(char *host, int port) {
if (__first_connect != 0)
__first_connect = 0;
else if (conwait > 0)
sleep(conwait);
#ifdef LIBOPENSSL
return (internal__hydra_connect_ssl(host, port, SOCK_STREAM, 6));
#else
return (internal__hydra_connect(host, port, SOCK_STREAM, 6));
#endif
}
int hydra_connect_tcp(char *host, int port) {
if (__first_connect != 0)
__first_connect = 0;
else if (conwait > 0)
sleep(conwait);
return (internal__hydra_connect(host, port, SOCK_STREAM, 6));
}
int hydra_connect_udp(char *host, int port) {
if (__first_connect != 0)
__first_connect = 0;
else if (conwait > 0)
sleep(conwait);
return (internal__hydra_connect(host, port, SOCK_DGRAM, 17));
}
int hydra_disconnect(int socket) {
#ifdef LIBOPENSSL
if (use_ssl && SSL_get_fd(ssl) == socket) {
/* SSL_shutdown(ssl); ...skip this--it slows things down */
SSL_set_bio(ssl, NULL, NULL);
SSL_clear(ssl);
use_ssl = 0;
}
#endif
close(socket);
if (debug)
printf("DEBUG_DISCONNECT\n");
return -1;
}
int hydra_data_ready_writing_timed(int socket, long sec, long usec) {
fd_set fds;
FD_ZERO(&fds);
FD_SET(socket, &fds);
return (my_select(socket + 1, &fds, NULL, NULL, sec, usec));
}
int hydra_data_ready_writing(int socket) {
return (hydra_data_ready_writing_timed(socket, 30, 0));
}
int hydra_data_ready_timed(int socket, long sec, long usec) {
fd_set fds;
FD_ZERO(&fds);
FD_SET(socket, &fds);
return (my_select(socket + 1, &fds, NULL, NULL, sec, usec));
}
int hydra_data_ready(int socket) {
return (hydra_data_ready_timed(socket, 0, 100));
}
int hydra_recv(int socket, char *buf, int length) {
int ret;
ret = internal__hydra_recv(socket, buf, length);
if (debug)
hydra_report_debug(stderr, "DEBUG_RECV_BEGIN|%s|END [pid:%d ret:%d]", buf, getpid(), ret);
return ret;
}
int hydra_recv_nb(int socket, char *buf, int length) {
int ret;
if (hydra_data_ready_timed(socket, (long) waittime, 0) > 0) {
if ((ret = internal__hydra_recv(socket, buf, length)) <= 0) {
buf[0] = 0;
return ret;
}
if (debug)
hydra_report_debug(stderr, "DEBUG_RECV_BEGIN|%s|END [pid:%d ret:%d]", buf, getpid(), ret);
}
return ret;
}
char *hydra_receive_line(int socket) {
char buf[1024], *buff;
int i, j = 1, k, got = 0;
if ((buff = malloc(sizeof(buf))) == NULL) {
fprintf(stderr, "[ERROR] could not malloc\n");
return NULL;
}
memset(buff, 0, sizeof(buf));
if (debug)
printf("[DEBUG] hydra_receive_line: waittime: %d, socket: %d\n", waittime, socket);
if ((i = hydra_data_ready_timed(socket, (long) waittime, 0)) > 0) {
if ((got = internal__hydra_recv(socket, buff, sizeof(buf))) < 0) {
free(buff);
return NULL;
}
} else {
if (debug)
printf("[DEBUG] hydra_data_ready_timed: %d, waittime: %d\n", i, waittime);
i = 0;
}
if (got < 0) {
if (debug) {
hydra_report_debug(stderr, "DEBUG_RECV_BEGIN||END [pid:%d %d]", getpid(), i);
perror("recv");
}
free(buff);
return NULL;
} else {
if (got > 0) {
for (k = 0; k < i; k++)
if (buff[k] == 0)
buff[k] = 32;
}
}
while (hydra_data_ready(socket) > 0 && j > 0) {
j = internal__hydra_recv(socket, buf, sizeof(buf));
if (j > 0) {
for (k = 0; k < j; k++)
if (buf[k] == 0)
buf[k] = 32;
buff = realloc(buff, got + j + 1);
memcpy(buff + got, &buf, j);
got += j;
buff[got] = 0;
}
}
if (debug)
hydra_report_debug(stderr, "DEBUG_RECV_BEGIN [pid:%d len:%d]|%s|END", getpid(), got, buff);
if (got == 0) {
free(buff);
return NULL;
}
return buff;
}
int hydra_send(int socket, char *buf, int size, int options) {
if (debug) {
int k;
char *debugbuf = malloc(size + 1);
if (debugbuf != NULL) {
for (k = 0; k < size; k++)
if (buf[k] == 0)
debugbuf[k] = 32;
else
debugbuf[k] = buf[k];
debugbuf[size] = 0;
hydra_report_debug(stderr, "DEBUG_SEND_BEGIN|%s|END [pid:%d]", debugbuf, getpid());
free(debugbuf);
}
}
/* if (hydra_data_ready_writing(socket)) < 1) return -1; XXX maybe needed in the future */
return (internal__hydra_send(socket, buf, size, options));
}
int make_to_lower(char *buf) {
if (buf == NULL)
return 1;
while (buf[0] != 0) {
buf[0] = tolower((int) buf[0]);
buf++;
}
return 1;
}
unsigned char hydra_conv64(unsigned char in) {
if (in < 26)
return (in + 'A');
else if (in >= 26 && in < 52)
return (in + 'a' - 26);
else if (in >= 52 && in < 62)
return (in + '0' - 52);
else if (in == 62)
return '+';
else if (in == 63)
return '/';
else {
fprintf(stderr, "Too high for base64: %d\n", in);
return 0;
}
}
void hydra_tobase64(unsigned char *buf, int buflen, int bufsize) {
unsigned char small[3] = { 0, 0, 0 };
unsigned char big[5];
unsigned char *ptr = buf;
int i = bufsize;
unsigned int len = 0;
unsigned char bof[i];
if (buf == NULL || strlen((char *) buf) == 0)
return;
bof[0] = 0;
memset(big, 0, sizeof(big));
memset(bof, 0, bufsize);
for (i = 0; i < buflen / 3; i++) {
memset(big, 0, sizeof(big));
big[0] = hydra_conv64(*ptr >> 2);
big[1] = hydra_conv64(((*ptr & 3) << 4) + (*(ptr + 1) >> 4));
big[2] = hydra_conv64(((*(ptr + 1) & 15) << 2) + (*(ptr + 2) >> 6));
big[3] = hydra_conv64(*(ptr + 2) & 63);
len += strlen((char *) big);
if (len > bufsize) {
buf[0] = 0;
return;
}
strcat((char *) bof, (char *) big);
ptr += 3;
}
if (*ptr != 0) {
small[0] = *ptr;
if (*(ptr + 1) != 0)
small[1] = *(ptr + 1);
else
small[1] = 0;
ptr = small;
big[0] = hydra_conv64(*ptr >> 2);
big[1] = hydra_conv64(((*ptr & 3) << 4) + (*(ptr + 1) >> 4));
big[2] = hydra_conv64(((*(ptr + 1) & 15) << 2) + (*(ptr + 2) >> 6));
big[3] = hydra_conv64(*(ptr + 2) & 63);
if (small[1] == 0)
big[2] = '=';
big[3] = '=';
strcat((char *) bof, (char *) big);
}
strcpy((char *) buf, (char *) bof); /* can not overflow */
}
void hydra_dump_asciihex(unsigned char *string, int length) {
unsigned char *p = (unsigned char *) string;
unsigned char lastrow_data[16];
int rows = length / HYDRA_DUMP_ROWS;
int lastrow = length % HYDRA_DUMP_ROWS;
int i, j;
for (i = 0; i < rows; i++) {
printf("%04hx: ", i * 16);
for (j = 0; j < HYDRA_DUMP_ROWS; j++) {
printf("%02x", p[(i * 16) + j]);
if (j % 2 == 1)
printf(" ");
}
printf(" [ ");
for (j = 0; j < HYDRA_DUMP_ROWS; j++) {
if (isprint(p[(i * 16) + j]))
printf("%c", p[(i * 16) + j]);
else
printf(".");
}
printf(" ]\n");
}
if (lastrow > 0) {
memset(lastrow_data, 0, sizeof(lastrow_data));
memcpy(lastrow_data, p + length - lastrow, lastrow);
printf("%04hx: ", i * 16);
for (j = 0; j < lastrow; j++) {
printf("%02x", p[(i * 16) + j]);
if (j % 2 == 1)
printf(" ");
}
while (j < HYDRA_DUMP_ROWS) {
printf(" ");
if (j % 2 == 1)
printf(" ");
j++;
}
printf(" [ ");
for (j = 0; j < lastrow; j++) {
if (isprint(p[(i * 16) + j]))
printf("%c", p[(i * 16) + j]);
else
printf(".");
}
while (j < HYDRA_DUMP_ROWS) {
printf(" ");
j++;
}
printf(" ]\n");
}
}
char *hydra_address2string(char *address) {
struct sockaddr_in target;
struct sockaddr_in6 target6;
if (address[0] == 4) {
memcpy(&target.sin_addr.s_addr, &address[1], 4);
return inet_ntoa((struct in_addr) target.sin_addr);
} else
#ifdef AF_INET6
if (address[0] == 16) {
memcpy(&target6.sin6_addr, &address[1], 16);
inet_ntop(AF_INET6, &target6.sin6_addr, ipstring, sizeof(ipstring));
return ipstring;
} else
#endif
{
fprintf(stderr, "[ERROR] unknown address string size!\n");
return NULL;