Codebase list hydra / debian/9.3-2 hydra-smb.c
debian/9.3-2

Tree @debian/9.3-2 (Download .tar.gz)

hydra-smb.c @debian/9.3-2raw · history · blame

   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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
#include "hydra-mod.h"
#ifndef LIBOPENSSL
void dummy_smb() { printf("\n"); }
#else
#include "hmacmd5.h"
#include "sasl.h"
#include <openssl/des.h>
#include <openssl/md4.h>

// FIXME XXX BUG: several malloc()s without return code checking

/*

http://technet.microsoft.com/en-us/library/cc960646.aspx

   Most of the new code comes from Medusa smbnt module

   ------------------------------------------------------------------------
    Copyright (C) 2009 Joe Mondloch
    JoMo-Kun / jmk@foofus.net

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License version 2,
    as published by the Free Software Foundation

    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.

    http://www.gnu.org/licenses/gpl.txt

    This program is released under the GPL with the additional exemption
    that compiling, linking, and/or using OpenSSL is allowed.

   ------------------------------------------------------------------------

   Based on code from: SMB Auditing Tool
   [Copyright (C) Patrik Karlsson 2001]
   This code allows Hydra to directly test NTLM hashes against
   a Windows. This may be useful for an auditor who has aquired
   a sam._ or pwdump file and would like to quickly determine
   which are valid entries. This module can also be used to test
   SMB passwords against devices that do not allow clear text
   LanMan passwords.

   The "-m 'METHOD'" option is required for this module. The
   following are valid methods: Local, Domain, Hash, Machine,
   NTLMV2, NTLM, LMV2, LM (in quotes).

     Local == Check local account.
     Domain == Check credentials against this hosts primary
          domain controller via this host.
     Hash == Use a NTLM hash rather than a password.
     Machine == Use the Machine's NetBIOS name as the password.
     NTLMV2, NTLM, LMV2, LM == set the dialect

   Be careful of mass domain account lockout with this. For
   example, assume you are checking several accounts against
   many domain workstations. If you are not using the 'L'
   options and these accounts do not exist locally on the
   workstations, each workstation will in turn check their
   respective domain controller. This could cause a bunch of
   lockouts. Of course, it'd look like the workstations, not
   you, were doing it. ;)

   **FYI, this code is unable to test accounts on default XP
   hosts which are not part of a domain and do not have normal
   file sharing enabled. Default XP does not allow shares and
   returns STATUS_LOGON_FAILED for both valid and invalid
   credentials. XP with simple sharing enabled returns SUCCESS
   for both valid and invalid credentials. If anyone knows a
   way to test in these configurations...

*/

#define WIN2000_NATIVEMODE 1
#define WIN_NETBIOSMODE 2

#define PLAINTEXT 10
#define ENCRYPTED 11

#ifndef CHAR_BIT
#define CHAR_BIT 8
#endif

#ifndef TIME_T_MIN
#define TIME_T_MIN ((time_t)0 < (time_t)-1 ? (time_t)0 : ~(time_t)0 << (sizeof(time_t) * CHAR_BIT - 1))
#endif
#ifndef TIME_T_MAX
#define TIME_T_MAX (~(time_t)0 - TIME_T_MIN)
#endif

#define IVAL_NC(buf, pos) (*(uint32_t *)((char *)(buf) + (pos))) /* Non const version of above. */
#define SIVAL(buf, pos, val) IVAL_NC(buf, pos) = ((uint32_t)(val))

#define TIME_FIXUP_CONSTANT_INT 11644473600LL

extern char *HYDRA_EXIT;
static unsigned char challenge[8];
static unsigned char workgroup[16];
static unsigned char domain[16];
static unsigned char machine_name[16];
int32_t hashFlag, accntFlag, protoFlag;

int32_t smb_auth_mechanism = AUTH_NTLM;
int32_t security_mode = ENCRYPTED;

static size_t UTF8_UTF16LE(unsigned char *in, int32_t insize, unsigned char *out, int32_t outsize) {
  int32_t i = 0, j = 0;
  uint64_t ch;
  if (debug) {
    hydra_report(stderr, "[DEBUG] UTF8_UTF16LE in:\n");
    hydra_dump_asciihex(in, insize);
  }
  for (i = 0; i < insize; i++) {
    if (in[i] < 128) { // one byte
      out[j] = in[i];
      out[j + 1] = 0;
      j = j + 2;
    } else if ((in[i] >= 0xc0) && (in[i] <= 0xdf)) { // Two bytes
      out[j + 1] = 0x07 & (in[i] >> 2);
      out[j] = (0xc0 & (in[i] << 6)) | (0x3f & in[i + 1]);
      j = j + 2;
      i = i + 1;
    } else if ((in[i] >= 0xe0) && (in[i] <= 0xef)) { // Three bytes
      out[j] = (0xc0 & (in[i + 1] << 6)) | (0x3f & in[i + 2]);
      out[j + 1] = (0xf0 & (in[i] << 4)) | (0x0f & (in[i + 1] >> 2));
      j = j + 2;
      i = i + 2;
    } else if ((in[i] >= 0xf0) && (in[i] <= 0xf7)) { // Four bytes
      ch = ((in[i] & 0x07) << 18) + ((0x3f & in[i + 1]) << 12) + ((0x3f & in[i + 2]) << 6) + (0x3f & in[i + 3]) - 0x10000;
      out[j] = (ch >> 10) & 0xff;
      out[j + 1] = 0xd8 | ((ch >> 18) & 0xff);
      out[j + 2] = ch & 0xff;
      out[j + 3] = 0xdc | ((ch >> 8) & 0x3);
      j = j + 4;
      i = i + 3;
    }
    if (j - 2 > outsize)
      break;
  }
  if (debug) {
    hydra_report(stderr, "[DEBUG] UTF8_UTF16LE out:\n");
    hydra_dump_asciihex(out, j);
  }
  return j;
}

static unsigned char Get7Bits(unsigned char *input, int32_t startBit) {
  register uint32_t word;

  word = (unsigned)input[startBit / 8] << 8;
  word |= (unsigned)input[startBit / 8 + 1];

  word >>= 15 - (startBit % 8 + 7);

  return word & 0xFE;
}

/* Make the key */
static void MakeKey(unsigned char *key, unsigned char *DES_key) {
  DES_key[0] = Get7Bits(key, 0);
  DES_key[1] = Get7Bits(key, 7);
  DES_key[2] = Get7Bits(key, 14);
  DES_key[3] = Get7Bits(key, 21);
  DES_key[4] = Get7Bits(key, 28);
  DES_key[5] = Get7Bits(key, 35);
  DES_key[6] = Get7Bits(key, 42);
  DES_key[7] = Get7Bits(key, 49);

  DES_set_odd_parity((DES_cblock *)DES_key);
}

/* Do the DesEncryption */
void DesEncrypt(unsigned char *clear, unsigned char *key, unsigned char *cipher) {
  DES_cblock DES_key;
  DES_key_schedule key_schedule;

  MakeKey(key, DES_key);
  DES_set_key(&DES_key, &key_schedule);
  DES_ecb_encrypt((DES_cblock *)clear, (DES_cblock *)cipher, &key_schedule, 1);
}

/*
  HashLM
  Function: Create a LM hash from the challenge
  Variables:
        lmhash    = the hash created from this function
        pass      = users password
        challenge = the challenge recieved from the server
*/
int32_t HashLM(unsigned char **lmhash, unsigned char *pass, unsigned char *challenge) {
  static unsigned char magic[] = {0x4b, 0x47, 0x53, 0x21, 0x40, 0x23, 0x24, 0x25};
  unsigned char password[14 + 1];
  unsigned char lm_hash[21];
  unsigned char lm_response[24];
  int32_t i = 0, j = 0;
  unsigned char *p = NULL;
  char HexChar;
  int32_t HexValue;

  memset(password, 0, 14 + 1);
  memset(lm_hash, 0, 21);
  memset(lm_response, 0, 24);

  /* Use LM Hash instead of password */
  /* D42E35E1A1E4C22BD32E2170E4857C20:5E20780DD45857A68402938C7629D3B2::: */
  if (hashFlag == 1) {
    p = pass;
    while ((*p != '\0') && (i < 1)) {
      if (*p == ':')
        i++;
      p++;
    }

    if (*p == '\0') {
      hydra_report(stderr, "[ERROR] Reading PwDump file.\n");
      return -1;
    } else if (*p == 'N') {
      if (verbose)
        hydra_report(stderr, "[VERBOSE] Found \"NO PASSWORD\" for LM Hash.\n");

      /* Generate 16-byte LM hash */
      DesEncrypt(magic, &password[0], &lm_hash[0]);
      DesEncrypt(magic, &password[7], &lm_hash[8]);
    } else {
      if (verbose)
        hydra_report(stderr, "[VERBOSE] Convert ASCII PwDump LM Hash (%s).\n", p);
      for (i = 0; i < 16; i++) {
        HexValue = 0x0;
        for (j = 0; j < 2; j++) {
          HexChar = (char)p[2 * i + j];

          if (HexChar > 0x39)
            HexChar = HexChar | 0x20; /* convert upper case to lower */

          if (!(((HexChar >= 0x30) && (HexChar <= 0x39)) ||  /* 0 - 9 */
                ((HexChar >= 0x61) && (HexChar <= 0x66)))) { /* a - f */

            hydra_report(stderr, "[ERROR] Invalid char (%c) for hash.\n", HexChar);
            HexChar = 0x30;
          }

          HexChar -= 0x30;
          if (HexChar > 0x09) /* HexChar is "a" - "f" */
            HexChar -= 0x27;

          HexValue = (HexValue << 4) | (char)HexChar;
        }
        lm_hash[i] = (unsigned char)HexValue;
      }
    }
  } else {
    /* Password == Machine Name */
    if (hashFlag == 2) {
      for (i = 0; i < 16; i++) {
        if (machine_name[i] > 0x39)
          machine_name[i] = machine_name[i] | 0x20; /* convert upper case to lower */
        pass = machine_name;
      }
    }

    /* convert lower case characters to upper case */
    strncpy((char *)password, (char *)pass, 14);
    for (i = 0; i < 14; i++) {
      if ((password[i] >= 0x61) && (password[i] <= 0x7a)) /* a - z */
        password[i] -= 0x20;
    }

    /* Generate 16-byte LM hash */
    DesEncrypt(magic, &password[0], &lm_hash[0]);
    DesEncrypt(magic, &password[7], &lm_hash[8]);
  }

  /*
     NULL-pad 16-byte LM hash to 21-bytes
     Split resultant value into three 7-byte thirds
     DES-encrypt challenge using each third as a key
     Concatenate three 8-byte resulting values to form 24-byte LM response
   */
  DesEncrypt(challenge, &lm_hash[0], &lm_response[0]);
  DesEncrypt(challenge, &lm_hash[7], &lm_response[8]);
  DesEncrypt(challenge, &lm_hash[14], &lm_response[16]);

  memcpy(*lmhash, lm_response, 24);

  return 0;
}

/*
  MakeNTLM
  Function: Create a NTLM hash from the password
*/
int32_t MakeNTLM(unsigned char *ntlmhash, unsigned char *pass) {
  MD4_CTX md4Context;
  unsigned char hash[16];                 /* MD4_SIGNATURE_SIZE = 16 */
  unsigned char unicodePassword[256 * 2]; /* MAX_NT_PASSWORD = 256 */
  int32_t i = 0, j = 0;
  int32_t mdlen;
  unsigned char *p = NULL;
  char HexChar;
  int32_t HexValue;

  /* Use NTLM Hash instead of password */
  if (hashFlag == 1) {
    /* 1000:D42E35E1A1E4C22BD32E2170E4857C20:5E20780DD45857A68402938C7629D3B2:::
     */
    p = pass;
    while ((*p != '\0') && (i < 1)) {
      if (*p == ':')
        i++;
      p++;
    }

    if (*p == '\0') {
      hydra_report(stderr, "[ERROR] reading PWDUMP file.\n");
      return -1;
    }

    for (i = 0; i < 16; i++) {
      HexValue = 0x0;
      for (j = 0; j < 2; j++) {
        HexChar = (char)p[2 * i + j];

        if (HexChar > 0x39)
          HexChar = HexChar | 0x20; /* convert upper case to lower */

        if (!(((HexChar >= 0x30) && (HexChar <= 0x39)) ||  /* 0 - 9 */
              ((HexChar >= 0x61) && (HexChar <= 0x66)))) { /* a - f */
          /*
           *  fprintf(stderr, "Error invalid char (%c) for hash.\n", HexChar);
           *  hydra_child_exit(0);
           */
          HexChar = 0x30;
        }

        HexChar -= 0x30;
        if (HexChar > 0x09) /* HexChar is "a" - "f" */
          HexChar -= 0x27;

        HexValue = (HexValue << 4) | (char)HexChar;
      }
      hash[i] = (unsigned char)HexValue;
    }
  } else {
    /* Password == Machine Name */
    if (hashFlag == 2) {
      for (i = 0; i < 16; i++) {
        if (machine_name[i] > 0x39)
          machine_name[i] = machine_name[i] | 0x20; /* convert upper case to lower */
        pass = machine_name;
      }
    }

    /* Initialize the Unicode version of the secret (== password). */
    /* This implicitly supports most UTF8 characters. */

    j = UTF8_UTF16LE(pass, strlen((char *)pass), unicodePassword, sizeof(unicodePassword));

    mdlen = j; /* length in bytes */

    MD4_Init(&md4Context);
    MD4_Update(&md4Context, unicodePassword, mdlen);
    MD4_Final(hash, &md4Context); /* Tell MD4 we're done */
  }

  memcpy(ntlmhash, hash, 16);
  return 0;
}

/*
  HashLMv2

  This function implements the LMv2 response algorithm. The LMv2 response is
  used to provide pass-through authentication compatibility with older servers.
  The response is based on the NTLM password hash and is exactly 24 bytes.

  The below code is based heavily on the following resources:

    http://davenport.sourceforge.net/ntlm.html#theLmv2Response
    samba-3.0.28a - libsmb/smbencrypt.c
    jcifs - packet capture of LMv2-only connection
*/
int32_t HashLMv2(unsigned char **LMv2hash, unsigned char *szLogin, unsigned char *szPassword) {
  unsigned char ntlm_hash[16];
  unsigned char lmv2_response[24];
  unsigned char unicodeUsername[20 * 2];
  unsigned char unicodeTarget[256 * 2];
  HMACMD5Context ctx;
  unsigned char kr_buf[16];
  int32_t ret, i;
  unsigned char client_challenge[8] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88};

  memset(ntlm_hash, 0, 16);
  memset(lmv2_response, 0, 24);
  memset(kr_buf, 0, 16);

  /* --- HMAC #1 Caculations --- */

  /* Calculate and set NTLM password hash */
  ret = MakeNTLM((unsigned char *)&ntlm_hash, (unsigned char *)szPassword);
  if (ret == -1)
    return -1;

  /*
     The Unicode uppercase username is concatenated with the Unicode
     authentication target (the domain or server name specified in the Target
     Name field of the Type 3 message). Note that this calculation always uses
     the Unicode representation, even if OEM encoding has been negotiated; also
     note that the username is converted to uppercase, while the authentication
     target is case-sensitive and must match the case presented in the Target
     Name field.

     The HMAC-MD5 message authentication code algorithm (described in RFC 2104)
     is applied to this value using the 16-byte NTLM hash as the key. This
     results in a 16-byte value - the NTLMv2 hash.
   */

  /* Initialize the Unicode version of the username and target. */
  /* This implicitly supports 8-bit ISO8859/1 characters. */
  /* convert lower case characters to upper case */
  bzero(unicodeUsername, sizeof(unicodeUsername));
  for (i = 0; i < strlen((char *)szLogin); i++) {
    if ((szLogin[i] >= 0x61) && (szLogin[i] <= 0x7a)) /* a - z */
      unicodeUsername[i * 2] = (unsigned char)szLogin[i] - 0x20;
    else
      unicodeUsername[i * 2] = (unsigned char)szLogin[i];
  }

  bzero(unicodeTarget, sizeof(unicodeTarget));
  for (i = 0; i < strlen((char *)workgroup); i++)
    unicodeTarget[i * 2] = (unsigned char)workgroup[i];

  hmac_md5_init_limK_to_64(ntlm_hash, 16, &ctx);
  hmac_md5_update((const unsigned char *)unicodeUsername, 2 * strlen((char *)szLogin), &ctx);
  hmac_md5_update((const unsigned char *)unicodeTarget, 2 * strlen((char *)workgroup), &ctx);
  hmac_md5_final(kr_buf, &ctx);

  /* --- HMAC #2 Calculations --- */
  /*
     The challenge from the Type 2 message is concatenated with our fixed client
     nonce. The HMAC-MD5 message authentication code algorithm is applied to
     this value using the 16-byte NTLMv2 hash (calculated above) as the key.
     This results in a 16-byte output value.
   */

  hmac_md5_init_limK_to_64(kr_buf, 16, &ctx);
  hmac_md5_update((const unsigned char *)challenge, 8, &ctx);
  hmac_md5_update(client_challenge, 8, &ctx);
  hmac_md5_final(lmv2_response, &ctx);

  /* --- 24-byte LMv2 Response Complete --- */
  if ((*LMv2hash = malloc(24)) == NULL)
    return -1;
  memset(*LMv2hash, 0, 24);
  memcpy(*LMv2hash, lmv2_response, 16);
  memcpy(*LMv2hash + 16, client_challenge, 8);

  return 0;
}

/*
  HashNTLMv2

  This function implements the NTLMv2 response algorithm. Support for this
  algorithm was added with Microsoft Windows with NT 4.0 SP4. It should be noted
  that code doesn't currently work with Microsoft Vista. While NTLMv2
  authentication with Samba and Windows 2003 functions as expected, Vista
  systems respond with the oh-so-helpful "INVALID_PARAMETER" error code.
  LMv2-only authentication appears to work against Vista in cases where LM and
  NTLM are refused.

  The below code is based heavily on the following two resources:

    http://davenport.sourceforge.net/ntlm.html#theNtlmv2Response
    samba-3.0.28 - libsmb/smbencrypt.c

  NTLMv2 network authentication is required when attempting to authenticated to
  a system which has the following policy enforced:

  GPO:     "Network Security: LAN Manager authentication level"
  Setting: "Send NTLMv2 response only\refuse LM & NTLM"
*/
int32_t HashNTLMv2(unsigned char **NTLMv2hash, int32_t *iByteCount, unsigned char *szLogin, unsigned char *szPassword) {
  unsigned char ntlm_hash[16];
  unsigned char ntlmv2_response[56 + 20 * 2 + 256 * 2];
  unsigned char unicodeUsername[20 * 2];
  unsigned char unicodeTarget[256 * 2];
  HMACMD5Context ctx;
  unsigned char kr_buf[16];
  int32_t ret, i, iTargetLen;
  unsigned char client_challenge[8] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88};

  /*
     -- Example NTLMv2 Response Data --

     [0]       HMAC: (16 bytes)

     [16]      Header: Blob Signature [01 01 00 00] (4 bytes)
     [20]      Reserved: [00 00 00 00] (4 bytes)
     [24]      Time: Little-endian, 64-bit signed value representing the number
     of tenths of a microsecond since January 1, 1601. (8 bytes) [32] Client
     Nonce: (8 bytes) [40]      Unknown: 00 00 00 00 (4 bytes) [44]      Target
     Information (from the Type 2 message) NetBIOS domain/workgroup: Type:
     domain 02 00 (2 bytes) Length: 12 00 (2 bytes) Name: WORKGROUP [NULL
     spacing -> 57 00 4f 00 ...] (18 bytes) End-of-list: 00 00 00 00 (4 bytes)
     Termination: 00 00 00 00 (4 bytes)
   */

  iTargetLen = 2 * strlen((char *)workgroup);

  memset(ntlm_hash, 0, 16);
  memset(ntlmv2_response, 0, 56 + 20 * 2 + 256 * 2);
  memset(kr_buf, 0, 16);

  /* --- HMAC #1 Caculations --- */

  /* Calculate and set NTLM password hash */
  ret = MakeNTLM((unsigned char *)&ntlm_hash, (unsigned char *)szPassword);
  if (ret == -1)
    return -1;

  /*
     The Unicode uppercase username is concatenated with the Unicode
     authentication target (the domain or server name specified in the Target
     Name field of the Type 3 message). Note that this calculation always uses
     the Unicode representation, even if OEM encoding has been negotiated; also
     note that the username is converted to uppercase, while the authentication
     target is case-sensitive and must match the case presented in the Target
     Name field.

     The HMAC-MD5 message authentication code algorithm (described in RFC 2104)
     is applied to this value using the 16-byte NTLM hash as the key. This
     results in a 16-byte value - the NTLMv2 hash.
   */

  /* Initialize the Unicode version of the username and target. */
  /* This implicitly supports 8-bit ISO8859/1 characters. */
  /* convert lower case characters to upper case */
  bzero(unicodeUsername, sizeof(unicodeUsername));
  for (i = 0; i < strlen((char *)szLogin); i++) {
    if ((szLogin[i] >= 0x61) && (szLogin[i] <= 0x7a)) /* a - z */
      unicodeUsername[i * 2] = (unsigned char)szLogin[i] - 0x20;
    else
      unicodeUsername[i * 2] = (unsigned char)szLogin[i];
  }

  bzero(unicodeTarget, sizeof(unicodeTarget));
  for (i = 0; i < strlen((char *)workgroup); i++)
    unicodeTarget[i * 2] = (unsigned char)workgroup[i];

  hmac_md5_init_limK_to_64(ntlm_hash, 16, &ctx);
  hmac_md5_update((const unsigned char *)unicodeUsername, 2 * strlen((char *)szLogin), &ctx);
  hmac_md5_update((const unsigned char *)unicodeTarget, 2 * strlen((char *)workgroup), &ctx);
  hmac_md5_final(kr_buf, &ctx);

  /* --- Blob Construction --- */

  memset(ntlmv2_response + 16, 1, 2); /* Blob Signature 0x01010000 */
  memset(ntlmv2_response + 18, 0, 2);
  memset(ntlmv2_response + 20, 0, 4); /* Reserved */

  /* Time -- Take a Unix time and convert to an NT TIME structure:
     Little-endian, 64-bit signed value representing the number of tenths of a
     microsecond since January 1, 1601.
   */
  struct timespec ts;
  unsigned long long nt;

  ts.tv_sec = (time_t)time(NULL);
  ts.tv_nsec = 0;

  if (ts.tv_sec == 0)
    nt = 0;
  else if (ts.tv_sec == TIME_T_MAX)
    nt = 0x7fffffffffffffffLL;
  else if (ts.tv_sec == (time_t)-1)
    nt = (unsigned long)-1;
  else {
    nt = ts.tv_sec;
    nt += TIME_FIXUP_CONSTANT_INT;
    nt *= 1000 * 1000 * 10; /* nt is now in the 100ns units */
  }

  SIVAL(ntlmv2_response + 24, 0, nt & 0xFFFFFFFF);
  SIVAL(ntlmv2_response + 24, 4, nt >> 32);
  /* End time calculation */

  /* Set client challenge - using a non-random value in this case. */
  memcpy(ntlmv2_response + 32, client_challenge, 8); /* Client Nonce */
  memset(ntlmv2_response + 40, 0, 4);                /* Unknown */

  /* Target Information Block */
  /*
     0x0100 Server name
     0x0200 Domain name
     0x0300 Fully-qualified DNS host name
     0x0400 DNS domain name

     TODO: Need to rework negotiation code to correctly extract target
     information
   */

  memset(ntlmv2_response + 44, 0x02, 1); /* Type: Domain */
  memset(ntlmv2_response + 45, 0x00, 1);
  memset(ntlmv2_response + 46, iTargetLen, 1); /* Length */
  memset(ntlmv2_response + 47, 0x00, 1);

  /* Name of domain or workgroup */
  for (i = 0; i < strlen((char *)workgroup); i++)
    ntlmv2_response[48 + i * 2] = (unsigned char)workgroup[i];

  memset(ntlmv2_response + 48 + iTargetLen, 0, 4); /* End-of-list */

  /* --- HMAC #2 Caculations --- */

  /*
     The challenge from the Type 2 message is concatenated with the blob. The
     HMAC-MD5 message authentication code algorithm is applied to this value
     using the 16-byte NTLMv2 hash (calculated above) as the key. This results
     in a 16-byte output value.
   */

  hmac_md5_init_limK_to_64(kr_buf, 16, &ctx);
  hmac_md5_update(challenge, 8, &ctx);
  hmac_md5_update(ntlmv2_response + 16, 48 - 16 + iTargetLen + 4, &ctx);
  hmac_md5_final(ntlmv2_response, &ctx);

  *iByteCount = 48 + iTargetLen + 4;
  if ((*NTLMv2hash = malloc(*iByteCount)) == NULL)
    return -1;
  memset(*NTLMv2hash, 0, *iByteCount);
  memcpy(*NTLMv2hash, ntlmv2_response, *iByteCount);

  return 0;
}

/*
  HashNTLM
  Function: Create a NTLM hash from the challenge
  Variables:
        ntlmhash  = the hash created from this function
        pass      = users password
        challenge = the challenge recieved from the server
*/
int32_t HashNTLM(unsigned char **ntlmhash, unsigned char *pass, unsigned char *challenge, char *miscptr) {
  int32_t ret;
  unsigned char hash[16]; /* MD4_SIGNATURE_SIZE = 16 */
  unsigned char p21[21];
  unsigned char ntlm_response[24];

  ret = MakeNTLM((unsigned char *)&hash, (unsigned char *)pass);
  if (ret == -1)
    hydra_child_exit(0);

  memset(p21, '\0', 21);
  memcpy(p21, hash, 16);

  DesEncrypt(challenge, p21 + 0, ntlm_response + 0);
  DesEncrypt(challenge, p21 + 7, ntlm_response + 8);
  DesEncrypt(challenge, p21 + 14, ntlm_response + 16);

  memcpy(*ntlmhash, ntlm_response, 24);

  return 0;
}

/*
   NBS Session Request
   Function: Request a new session from the server
   Returns: TRUE on success else FALSE.
*/
int32_t NBSSessionRequest(int32_t s) {
  char nb_name[32];  /* netbiosname */
  char nb_local[32]; /* netbios localredirector */
  unsigned char rqbuf[7] = {0x81, 0x00, 0x00, 0x44, 0x20, 0x00, 0x20};
  char *buf;
  unsigned char rbuf[400];
  int32_t k;

  /* if we are running in native mode (aka port 445) don't do netbios */
  if (protoFlag == WIN2000_NATIVEMODE)
    return 0;

  /* convert computer name to netbios name */
  memset(nb_name, 0, 32);
  memset(nb_local, 0, 32);
  memcpy(nb_name, "CKFDENECFDEFFCFGEFFCCACACACACACA", 32);  /* *SMBSERVER */
  memcpy(nb_local, "EIFJEEFCEBCACACACACACACACACACACA", 32); /* HYDRA */

  if ((buf = (char *)malloc(100)) == NULL)
    return -1;
  memset(buf, 0, 100);
  memcpy(buf, (char *)rqbuf, 5);
  memcpy(buf + 5, nb_name, 32);
  memcpy(buf + 37, (char *)rqbuf + 5, 2);
  memcpy(buf + 39, nb_local, 32);
  memcpy(buf + 71, (char *)rqbuf + 5, 1);

  hydra_send(s, buf, 72, 0);
  free(buf);

  memset(rbuf, 0, 400);
  k = hydra_recv(s, (char *)rbuf, sizeof(rbuf));

  if (k > 0 && (rbuf[0] == 0x82))
    return 0; /* success */
  else
    return -1; /* failed */
}

/*
   SMBNegProt
   Function: Negotiate protocol with server ...
       Actually a pseudo negotiation since the whole
       program counts on NTLM support :)

    The challenge is retrieved from the answer
    No error checking is performed i.e cross your fingers....
*/
int32_t SMBNegProt(int32_t s) {
  unsigned char buf[] = {
      0x00, 0x00, 0x00, 0xbe, 0xff, 0x53, 0x4d, 0x42, 0x72, 0x00, 0x00, 0x00, 0x00, 0x08, 0x01, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3c, 0x7d, 0x00, 0x00, 0x01, 0x00, 0x00, 0x9b, 0x00, 0x02, 0x50, 0x43, 0x20, 0x4e, 0x45, 0x54, 0x57, 0x4f, 0x52, 0x4b, 0x20, 0x50, 0x52, 0x4f, 0x47, 0x52, 0x41, 0x4d, 0x20, 0x31, 0x2e, 0x30, 0x00, 0x02, 0x4d,
      0x49, 0x43, 0x52, 0x4f, 0x53, 0x4f, 0x46, 0x54, 0x20, 0x4e, 0x45, 0x54, 0x57, 0x4f, 0x52, 0x4b, 0x53, 0x20, 0x31, 0x2e, 0x30, 0x33, 0x00, 0x02, 0x4d, 0x49, 0x43, 0x52, 0x4f, 0x53, 0x4f, 0x46, 0x54, 0x20, 0x4e, 0x45, 0x54, 0x57, 0x4f, 0x52, 0x4b, 0x53, 0x20, 0x33, 0x2e, 0x30, 0x00, 0x02, 0x4c, 0x41, 0x4e, 0x4d, 0x41, 0x4e, 0x31, 0x2e, 0x30, 0x00, 0x02, 0x4c, 0x4d, 0x31, 0x2e, 0x32, 0x58,
      0x30, 0x30, 0x32, 0x00, 0x02, 0x44, 0x4f, 0x53, 0x20, 0x4c, 0x41, 0x4e, 0x4d, 0x41, 0x4e, 0x32, 0x2e, 0x31, 0x00, 0x02, 0x4c, 0x41, 0x4e, 0x4d, 0x41, 0x4e, 0x32, 0x2e, 0x31, 0x00, 0x02, 0x53, 0x61, 0x6d, 0x62, 0x61, 0x00, 0x02, 0x4e, 0x54, 0x20, 0x4c, 0x41, 0x4e, 0x4d, 0x41, 0x4e, 0x20, 0x31, 0x2e, 0x30, 0x00, 0x02, 0x4e, 0x54, 0x20, 0x4c, 0x4d, 0x20, 0x30, 0x2e, 0x31, 0x32, 0x00

      /*
      0x02,
          0x50, 0x43, 0x20, 0x4e, 0x45, 0x54, 0x57, 0x4f,
          0x52, 0x4b, 0x20, 0x50, 0x52, 0x4f, 0x47, 0x52,
          0x41, 0x4d, 0x20, 0x31, 0x2e, 0x30, 0x00, 0x02,
          0x4d, 0x49, 0x43, 0x52, 0x4f, 0x53, 0x4f, 0x46,
          0x54, 0x20, 0x4e, 0x45, 0x54, 0x57, 0x4f, 0x52,
          0x4b, 0x53, 0x20, 0x31, 0x2e, 0x30, 0x33, 0x00,
          0x02, 0x4d, 0x49, 0x43, 0x52, 0x4f, 0x53, 0x4f,
          0x46, 0x54, 0x20, 0x4e, 0x45, 0x54, 0x57, 0x4f,
          0x52, 0x4b, 0x53, 0x20, 0x33, 0x2e, 0x30, 0x00,
          0x02, 0x4c, 0x41, 0x4e, 0x4d, 0x41, 0x4e, 0x31,
          0x2e, 0x30, 0x00, 0x02, 0x4c, 0x4d, 0x31, 0x2e,
          0x32, 0x58, 0x30, 0x30, 0x32, 0x00, 0x02, 0x53,
          0x61, 0x6d, 0x62, 0x61, 0x00, 0x02, 0x4e, 0x54,
          0x20, 0x4c, 0x41, 0x4e, 0x4d, 0x41, 0x4e, 0x20,
          0x31, 0x2e, 0x30, 0x00, 0x02, 0x4e, 0x54, 0x20,
          0x4c, 0x4d, 0x20, 0x30, 0x2e, 0x31, 0x32, 0x00
      */
  };

  unsigned char rbuf[400];
  unsigned char sess_key[2];
  unsigned char userid[2] = {0xCD, 0xEF};
  int32_t i = 0, j = 0, k;
  int32_t iLength = 194;
  int32_t iResponseOffset = 73;

  memset((char *)rbuf, 0, 400);

  /* set session key */
  sess_key[1] = getpid() / 100;
  sess_key[0] = getpid() - (100 * sess_key[1]);
  memcpy(buf + 30, sess_key, 2);
  memcpy(buf + 32, userid, 2);

  if (smb_auth_mechanism == AUTH_LM) {
    if (verbose)
      hydra_report(stderr, "[VERBOSE] Setting Negotiate Protocol Response for LM.\n");
    buf[3] = 0xA3;  // Set message length
    buf[37] = 0x80; // Set byte count for dialects
    iLength = 167;
    iResponseOffset = 65;
  }

  hydra_send(s, (char *)buf, iLength, 0);
  k = hydra_recv(s, (char *)rbuf, sizeof(rbuf));
  if (k == 0)
    return 3;

  /* retrieve the security mode */
  /*
     [0] Mode:       (0) ?                                 (1) USER security
     mode [1] Password:   (0) PLAINTEXT password                (1) ENCRYPTED
     password. Use challenge/response [2] Signatures: (0) Security signatures
     NOT enabled   (1) ENABLED [3] Sig Req:    (0) Security signatures NOT
     required  (1) REQUIRED

     SAMBA: 0x01 (default)
     WinXP: 0x0F (default)
     WinXP: 0x07 (Windows 2003 / DC)
   */
  switch (rbuf[39]) {
  case 0x01:
    // real plaintext should be used with LM auth
    if (verbose)
      hydra_report(stderr, "[VERBOSE] Server requested PLAINTEXT password.\n");
    security_mode = PLAINTEXT;

    if (hashFlag == 1) {
      if (verbose)
        hydra_report(stderr, "[VERBOSE] Server requested PLAINTEXT password. HASH "
                             "password mode not supported for this configuration.\n");
      return 3;
    }
    if (hashFlag == 2) {
      if (verbose)
        hydra_report(stderr, "[VERBOSE] Server requested PLAINTEXT password. MACHINE "
                             "password mode not supported for this configuration.\n");
      return 3;
    }
    break;
  case 0x03:
    if (verbose)
      hydra_report(stderr, "[VERBOSE] Server requested ENCRYPTED password "
                           "without security signatures.\n");
    security_mode = ENCRYPTED;
    break;
  case 0x07:
  case 0x0F:
    if (verbose)
      hydra_report(stderr, "[VERBOSE] Server requested ENCRYPTED password.\n");
    security_mode = ENCRYPTED;
    break;
  default:
    if (verbose)
      hydra_report(stderr,
                   "[VERBOSE] Unknown security mode request: %2.2X. Proceeding "
                   "using ENCRYPTED password mode.\n",
                   rbuf[39]);
    security_mode = ENCRYPTED;
    break;
  }

  /* Retrieve the challenge */
  memcpy(challenge, (char *)rbuf + iResponseOffset, sizeof(challenge));

  /* Find the primary domain/workgroup name */
  memset(workgroup, 0, 16);
  memset(machine_name, 0, 16);

  // seems using LM only the domain is returned not the server
  // and the domain is not padded with null chars
  if (smb_auth_mechanism == AUTH_LM) {
    while ((rbuf[iResponseOffset + 8 + i] != 0) && (i < 16)) {
      workgroup[i] = rbuf[iResponseOffset + 8 + i];
      i++;
    }
  } else {
    while ((rbuf[iResponseOffset + 8 + i * 2] != 0) && (i < 16)) {
      workgroup[i] = rbuf[iResponseOffset + 8 + i * 2];
      i++;
    }

    while ((rbuf[iResponseOffset + 8 + (i + j + 1) * 2] != 0) && (j < 16)) {
      machine_name[j] = rbuf[iResponseOffset + 8 + (i + j + 1) * 2];
      j++;
    }
  }

  if (verbose) {
    hydra_report(stderr, "[VERBOSE] Server machine name: %s\n", machine_name);
    hydra_report(stderr, "[VERBOSE] Server primary domain: %s\n", workgroup);
  }
  // success
  return 2;
}

/*
  SMBSessionSetup
  Function: Send username + response to the challenge from
            the server.
  Returns: TRUE on success else FALSE.
*/
unsigned long SMBSessionSetup(int32_t s, char *szLogin, char *szPassword, char *miscptr) {
  unsigned char buf[512];
  unsigned char *LMv2hash = NULL;
  unsigned char *NTLMv2hash = NULL;
  unsigned char *NTLMhash = NULL;
  unsigned char *LMhash = NULL;
  //  unsigned char unicodeLogin[32 * 2];
  int32_t j;
  char bufReceive[512];
  int32_t nReceiveBufferSize = 0;
  int32_t ret;
  int32_t iByteCount = 0, iOffset = 0;

  if (accntFlag == 0) {
    strcpy((char *)workgroup, "localhost");

  } else if (accntFlag == 2) {
    memset(workgroup, 0, 16);
  }
  // domain flag is not needed here, it will be auto set,
  // below it's domain specified on cmd line
  else if (accntFlag == 4) {
    strncpy((char *)workgroup, (char *)domain, 16);
  }

  /* NetBIOS Session Service */
  unsigned char szNBSS[4] = {
      0x00,            /* Message Type: Session Message */
      0x00, 0x00, 0x85 /* Length -- MUST SET */
  };

  /* SMB Header */
  unsigned char szSMB[32] = {
      0xff,
      0x53,
      0x4d,
      0x42, /* Server Component */
      0x73, /* SMB Command: Session Setup AndX */
      0x00,
      0x00,
      0x00,
      0x00, /* NT Status: STATUS_SUCCESS */
      0x08, /* Flags */
      0x01,
      0xc0,
      /* Flags2 */ /* add Unicode */
      0x00,
      0x00, /* Process ID High */
      0x00,
      0x00,
      0x00,
      0x00,
      0x00,
      0x00,
      0x00,
      0x00, /* Signature */
      0x00,
      0x00, /* Reserved */
      0x00,
      0x00, /* Tree ID */
      0x13,
      0x37, /* Process ID */
      0x00,
      0x00, /* User ID */
      0x01,
      0x00 /* Multiplx ID */
  };

  memset(buf, 0, 512);
  memcpy(buf, szNBSS, 4);
  memcpy(buf + 4, szSMB, 32);

  if (security_mode == ENCRYPTED) {
    /* Session Setup AndX Request */
    if (smb_auth_mechanism == AUTH_LM) {
      if (verbose)
        hydra_report(stderr, "[VERBOSE] Attempting LM password authentication.\n");

      unsigned char szSessionRequest[23] = {
          0x0a,                   /* Word Count */
          0xff,                   /* AndXCommand: No further commands */
          0x00,                   /* Reserved */
          0x00, 0x00,             /* AndXOffset */
          0xff, 0xff,             /* Max Buffer */
          0x02, 0x00,             /* Max Mpx Count */
          0x3c, 0x7d,             /* VC Number */
          0x00, 0x00, 0x00, 0x00, /* Session Key */
          0x18, 0x00,             /* LAN Manager Password Hash Length */
          0x00, 0x00, 0x00, 0x00, /* Reserved */
          0x49, 0x00              /* Byte Count -- MUST SET */
      };

      iOffset = 59;    /* szNBSS + szSMB + szSessionRequest */
      iByteCount = 24; /* Start with length of LM hash */

      /* Set Session Setup AndX Request header information */
      memcpy(buf + 36, szSessionRequest, 23);

      /* Calculate and set LAN Manager password hash */
      if ((LMhash = (unsigned char *)malloc(24)) == NULL)
        return -1;
      memset(LMhash, 0, 24);

      ret = HashLM(&LMhash, (unsigned char *)szPassword, (unsigned char *)challenge);
      if (ret == -1) {
        free(LMhash);
        return -1;
      }

      memcpy(buf + iOffset, LMhash, 24);
      free(LMhash);

    } else if (smb_auth_mechanism == AUTH_NTLM) {
      if (verbose)
        hydra_report(stderr, "[VERBOSE] Attempting NTLM password authentication.\n");

      unsigned char szSessionRequest[29] = {
          0x0d, /* Word Count */
          0xff, /* AndXCommand: No further commands */
          0x00, /* Reserved */
          0x00,
          0x00, /* AndXOffset */
          0xff,
          0xff, /* Max Buffer */
          0x02,
          0x00, /* Max Mpx Count */
          0x3c,
          0x7d, /* VC Number */
          0x00,
          0x00,
          0x00,
          0x00, /* Session Key */
          0x18,
          0x00, /* LAN Manager Password Hash Length */
          0x18,
          0x00, /* NT LAN Manager Password Hash Length */
          0x00,
          0x00,
          0x00,
          0x00, /* Reserved */
          0x5c,
          0x00,
          0x00,
          0x00,
          /* Capabilities */ /* Add Unicode */
          0x49,
          0x00 /* Byte Count -- MUST SET */
      };

      iOffset = 65;    /* szNBSS + szSMB + szSessionRequest */
      iByteCount = 48; /* Start with length of NTLM and LM hashes */

      /* Set Session Setup AndX Request header information */
      memcpy(buf + 36, szSessionRequest, 29);

      /* Calculate and set NTLM password hash */
      if ((NTLMhash = (unsigned char *)malloc(24)) == NULL)
        return -1;
      memset(NTLMhash, 0, 24);

      /* We don't need to actually calculated a LM hash for this mode, only NTLM
       */
      ret = HashNTLM(&NTLMhash, (unsigned char *)szPassword, (unsigned char *)challenge, miscptr);
      if (ret == -1)
        return -1;

      memcpy(buf + iOffset + 24, NTLMhash, 24); /* Skip space for LM hash */
      free(NTLMhash);
    } else if (smb_auth_mechanism == AUTH_LMv2) {
      if (verbose)
        hydra_report(stderr, "[VERBOSE] Attempting LMv2 password authentication.\n");

      unsigned char szSessionRequest[29] = {
          0x0d,                   /* Word Count */
          0xff,                   /* AndXCommand: No further commands */
          0x00,                   /* Reserved */
          0x00, 0x00,             /* AndXOffset */
          0xff, 0xff,             /* Max Buffer */
          0x02, 0x00,             /* Max Mpx Count */
          0x3c, 0x7d,             /* VC Number */
          0x00, 0x00, 0x00, 0x00, /* Session Key */
          0x18, 0x00,             /* LAN Manager Password Hash Length */
          0x00, 0x00,             /* NT LAN Manager Password Hash Length */
          0x00, 0x00, 0x00, 0x00, /* Reserved */
          0x50, 0x00, 0x00, 0x00, /* Capabilities */
          0x49, 0x00              /* Byte Count -- MUST SET */
      };

      iOffset = 65;    /* szNBSS + szSMB + szSessionRequest */
      iByteCount = 24; /* Start with length of LMv2 response */

      /* Set Session Setup AndX Request header information */
      memcpy(buf + 36, szSessionRequest, 29);

      /* Calculate and set LMv2 response hash */
      if ((LMv2hash = (unsigned char *)malloc(24)) == NULL)
        return -1;
      memset(LMv2hash, 0, 24);

      ret = HashLMv2(&LMv2hash, (unsigned char *)szLogin, (unsigned char *)szPassword);
      if (ret == -1) {
        free(LMv2hash);
        return -1;
      }

      memcpy(buf + iOffset, LMv2hash, 24);
      free(LMv2hash);
    } else if (smb_auth_mechanism == AUTH_NTLMv2) {
      if (verbose)
        hydra_report(stderr, "[VERBOSE] Attempting LMv2/NTLMv2 password authentication.\n");

      unsigned char szSessionRequest[29] = {
          0x0d,                   /* Word Count */
          0xff,                   /* AndXCommand: No further commands */
          0x00,                   /* Reserved */
          0x00, 0x00,             /* AndXOffset */
          0xff, 0xff,             /* Max Buffer */
          0x02, 0x00,             /* Max Mpx Count */
          0x3c, 0x7d,             /* VC Number */
          0x00, 0x00, 0x00, 0x00, /* Session Key */
          0x18, 0x00,             /* LMv2 Response Hash Length */
          0x4b, 0x00,             /* NTLMv2 Response Hash Length -- MUST SET */
          0x00, 0x00, 0x00, 0x00, /* Reserved */
          0x50, 0x00, 0x00, 0x00, /* Capabilities */
          0x49, 0x00              /* Byte Count -- MUST SET */
      };

      iOffset = 65; /* szNBSS + szSMB + szSessionRequest */

      /* Set Session Setup AndX Request header information */
      memcpy(buf + 36, szSessionRequest, 29);

      /* Calculate and set LMv2 response hash */
      ret = HashLMv2(&LMv2hash, (unsigned char *)szLogin, (unsigned char *)szPassword);
      if (ret == -1)
        return -1;

      memcpy(buf + iOffset, LMv2hash, 24);
      free(LMv2hash);

      /* Calculate and set NTLMv2 response hash */
      ret = HashNTLMv2(&NTLMv2hash, &iByteCount, (unsigned char *)szLogin, (unsigned char *)szPassword);
      if (ret == -1)
        return -1;

      /* Set NTLMv2 Response Length */
      memset(buf + iOffset - 12, iByteCount, 1);
      if (verbose)
        hydra_report(stderr, "[VERBOSE] HashNTLMv2 response length: %d\n", iByteCount);

      memcpy(buf + iOffset + 24, NTLMv2hash, iByteCount);
      free(NTLMv2hash);

      iByteCount += 24; /* Reflects length of both LMv2 and NTLMv2 responses */
    }
  } else if (security_mode == PLAINTEXT) {
    if (verbose)
      hydra_report(stderr, "[VERBOSE] Attempting PLAINTEXT password authentication.\n");

    unsigned char szSessionRequest[23] = {
        0x0a,                   /* Word Count */
        0xff,                   /* AndXCommand: No further commands */
        0x00,                   /* Reserved */
        0x00, 0x00,             /* AndXOffset */
        0xff, 0xff,             /* Max Buffer */
        0x02, 0x00,             /* Max Mpx Count */
        0x3c, 0x7d,             /* VC Number */
        0x00, 0x00, 0x00, 0x00, /* Session Key */
        0x00, 0x00,             /* Password Length -- MUST SET */
        0x00, 0x00, 0x00, 0x00, /* Reserved */
        0x49, 0x00              /* Byte Count -- MUST SET */
    };

    iOffset = 59; /* szNBSS + szSMB + szSessionRequest */

    /* Set Session Setup AndX Request header information */
    memcpy(buf + 36, szSessionRequest, 23);

    /* Calculate and set password length */
    /* Samba appears to append NULL characters equal to the password length plus
     * 2 */
    // iByteCount = 2 * strlen(szPassword) + 2;
    iByteCount = strlen(szPassword) + 1;
    buf[iOffset - 8] = (iByteCount) % 256;
    buf[iOffset - 7] = (iByteCount) / 256;

    /* set ANSI password */
    /*
       Depending on the SAMBA server configuration, multiple passwords may be
       successful when dealing with mixed-case values. The SAMBA parameter
       "password level" appears to determine how many characters within a
       password are tested by the server both upper and lower case. For example,
       assume a SAMBA account has a password of "Fred" and the server is
       configured with "password level = 2". Medusa sends the password "FRED".
       The SAMBA server will brute-force test this value for us with values
       like: "FRed", "FrEd", "FreD", "fREd", "fReD", "frED", ... The default
       setting is "password level = 0". This results in only two attempts to
       being made by the remote server; the password as is and the password in
       all-lower case.
     */
    strncpy((char *)(buf + iOffset), szPassword, 256);
  } else {
    hydra_report(stderr, "[ERROR] Security_mode was not properly set. This "
                         "should not happen.\n");
    return -1;
  }

  /* Set account and workgroup values */

  j = UTF8_UTF16LE((unsigned char *)szLogin, strlen(szLogin), buf + iOffset + iByteCount + 1, 2 * strlen(szLogin));
  iByteCount += j + 3; /* NULL pad account name */
  j = UTF8_UTF16LE(workgroup, strlen((char *)workgroup), buf + iOffset + iByteCount, 2 * strlen((char *)workgroup));
  iByteCount += j + 2; // NULL pad workgroup name

  /* Set native OS and LAN Manager values */

  char *szOSName = "Unix";
  j = UTF8_UTF16LE((unsigned char *)szOSName, strlen(szOSName), buf + iOffset + iByteCount, 2 * sizeof(szOSName));
  iByteCount += j + 2; // NULL terminated
  char *szLANMANName = "Samba";
  j = UTF8_UTF16LE((unsigned char *)szLANMANName, strlen(szLANMANName), buf + iOffset + iByteCount, 2 * sizeof(szLANMANName));
  iByteCount += j + 2; // NULL terminated

  /* Set the header length */
  buf[2] = (iOffset - 4 + iByteCount) / 256;
  buf[3] = (iOffset - 4 + iByteCount) % 256;

  if (verbose)
    hydra_report(stderr, "[VERBOSE] Set NBSS header length: %2.2X\n", buf[3]);

  /* Set data byte count */
  buf[iOffset - 2] = iByteCount;
  if (verbose)
    hydra_report(stderr, "[VERBOSE] Set byte count: %2.2X\n", buf[57]);

  hydra_send(s, (char *)buf, iOffset + iByteCount, 0);

  nReceiveBufferSize = hydra_recv(s, bufReceive, sizeof(bufReceive));
  if (/*(bufReceive == NULL) ||*/ (nReceiveBufferSize == 0))
    return -1;

  /* 41 - Action (Guest/Non-Guest Account) */
  /*  9 - NT Status (Error code) */
  return (((bufReceive[41] & 0x01) << 24) | ((bufReceive[11] & 0xFF) << 16) | ((bufReceive[10] & 0xFF) << 8) | (bufReceive[9] & 0xFF));
}

int32_t start_smb(int32_t s, char *ip, int32_t port, unsigned char options, char *miscptr, FILE *fp) {
  char *empty = "";
  char *login, *pass;
  int32_t SMBerr, SMBaction;
  unsigned long SMBSessionRet;
  char ipaddr_str[64];
  char ErrorCode[10];

  memset(&ErrorCode, 0, 10);

  if (strlen(login = hydra_get_next_login()) == 0)
    login = empty;
  if (strlen(pass = hydra_get_next_password()) == 0)
    pass = empty;

  strncpy(ipaddr_str, hydra_address2string(ip), sizeof(ipaddr_str) - 1);

  SMBSessionRet = SMBSessionSetup(s, login, pass, miscptr);
  if (SMBSessionRet == -1)
    return 3;
  SMBerr = (unsigned long)SMBSessionRet & 0x00FFFFFF;
  SMBaction = ((unsigned long)SMBSessionRet & 0xFF000000) >> 24;

  if (verbose)
    hydra_report(stderr, "[VERBOSE] SMBSessionRet: %8.8X SMBerr: %4.4X SMBaction: %2.2X\n", (uint32_t)SMBSessionRet, SMBerr, SMBaction);

  /*
     some error code are available here:
     http://msdn.microsoft.com/en-us/library/ee441884(v=prot.13).aspx
   */

  if (SMBerr == 0x000000) {  /* success */
    if (SMBaction == 0x01) { /* invalid account - anonymous connection */
      fprintf(stderr,
              "[%d][smb] Host: %s Account: %s Error: Invalid account "
              "(Anonymous success)\n",
              port, ipaddr_str, login);
      hydra_completed_pair_skip();
    } else { /* valid account */
      hydra_report_found_host(port, ip, "smb", fp);
      hydra_completed_pair_found();
    }
  } else if ((SMBerr == 0x00000D) && (SMBaction == 0x00)) {
    hydra_report(stderr, "[ERROR] Invalid parameter status received, either "
                         "the account or the method used are not valid\n");
    hydra_completed_pair_skip();
  } else if (SMBerr == 0x00006E) { /* Valid password, GPO Disabling Remote
                                      Connections Using NULL Passwords */
    hydra_report(stdout,
                 "[%d][smb] Host: %s Account: %s Valid password, GPO Disabling "
                 "Remote Connections Using NULL Passwords\n",
                 port, ipaddr_str, login);
    hydra_report_found_host(port, ip, "smb", fp);
    hydra_completed_pair_found();
  } else if (SMBerr == 0x00015B) { /* Valid password, GPO "Deny access to this
                                      computer from the network" */
    hydra_report(stdout,
                 "[%d][smb] Host: %s Account: %s Valid password, GPO Deny "
                 "access to this computer from the network\n",
                 port, ipaddr_str, login);
    hydra_report_found_host(port, ip, "smb", fp);
    hydra_completed_pair_found();
  } else if (SMBerr == 0x000193) { /* Valid password, account expired  */
    hydra_report(stdout, "[%d][smb] Host: %s Account: %s Valid password, account expired\n", port, ipaddr_str, login);
    hydra_report_found_host(port, ip, "smb", fp);
    hydra_completed_pair_found();
  } else if ((SMBerr == 0x000224) || (SMBerr == 0xC20002)) { /* Valid password, account expired  */
    hydra_report(stdout,
                 "[%d][smb] Host: %s Account: %s Valid password, password "
                 "expired and must be changed on next logon\n",
                 port, ipaddr_str, login);
    hydra_report_found_host(port, ip, "smb", fp);
    hydra_completed_pair_found();
  } else if ((SMBerr == 0x00006F) || (SMBerr == 0xC10002)) { /* Invalid logon hours  */
    hydra_report(stdout,
                 "[%d][smb] Host: %s Account: %s Valid password, but logon "
                 "hours invalid\n",
                 port, ipaddr_str, login);
    hydra_report_found_host(port, ip, "smb", fp);
    hydra_completed_pair_found();
  } else if (SMBerr == 0x050001) { /* AS/400 -- Incorrect password */
    hydra_report(stdout,
                 "[%d][smb] Host: %s Account: %s Error: Incorrect password or "
                 "account disabled\n",
                 port, ipaddr_str, login);
    if ((miscptr) && (strstr(miscptr, "LM")))
      hydra_report(stderr, "[INFO] LM dialect may be disabled, try LMV2 instead\n");
    hydra_completed_pair_skip();
  } else if (SMBerr == 0x000024) { /* change password on next login [success] */
    hydra_report(stdout, "[%d][smb] Host: %s Account: %s Error: ACCOUNT_CHANGE_PASSWORD\n", port, ipaddr_str, login);
    hydra_completed_pair_found();
  } else if (SMBerr == 0x00006D) { /* STATUS_LOGON_FAILURE */
    hydra_completed_pair();
  } else if (SMBerr == 0x000071) { /* password expired */
    if (verbose)
      fprintf(stderr, "[%d][smb] Host: %s Account: %s Error: PASSWORD EXPIRED\n", port, ipaddr_str, login);
    hydra_completed_pair_skip();
  } else if ((SMBerr == 0x000072) || (SMBerr == 0xBF0002)) { /* account disabled */ /* BF0002 on w2k */
    if (verbose)
      fprintf(stderr, "[%d][smb] Host: %s Account: %s Error: ACCOUNT_DISABLED\n", port, ipaddr_str, login);
    hydra_completed_pair_skip();
  } else if (SMBerr == 0x000034 || SMBerr == 0x000234) { /* account locked out */
    if (verbose)
      fprintf(stderr, "[%d][smb] Host: %s Account: %s Error: ACCOUNT_LOCKED\n", port, ipaddr_str, login);
    hydra_completed_pair_skip();
  } else if (SMBerr == 0x00008D) { /* ummm... broken client-domain membership  */
    if (verbose)
      fprintf(stderr,
              "[%d][smb] Host: %s Account: %s Error: "
              "NT_STATUS_TRUSTED_RELATIONSHIP_FAILURE\n",
              port, ipaddr_str, login);
    hydra_completed_pair();
  } else { /* failed */
    if (verbose)
      fprintf(stderr, "[%d][smb] Host: %s Account: %s Unknown Error: %6.6X\n", port, ipaddr_str, login, SMBerr);
    hydra_completed_pair();
  }

  hydra_disconnect(s);
  if (memcmp(hydra_get_next_pair(), &HYDRA_EXIT, sizeof(HYDRA_EXIT)) == 0)
    return 3;
  return 1;
}

void service_smb(char *ip, int32_t sp, unsigned char options, char *miscptr, FILE *fp, int32_t port, char *hostname) {
  int32_t run = 1, next_run = 1, sock = -1;

  // default is both (local and domain) checks and normal passwd
  accntFlag = 2; // BOTH
  hashFlag = 0;  // PASS
  smb_auth_mechanism = AUTH_NTLM;

  if (miscptr) {
    // check group
    strupper(miscptr);
    if (strstr(miscptr, "OTHER_DOMAIN:") != NULL) {
      char *tmpdom;
      int32_t err = 0;

      accntFlag = 4; // OTHER DOMAIN
      tmpdom = strstr(miscptr, "OTHER_DOMAIN:");
      tmpdom = tmpdom + strlen("OTHER_DOMAIN:");

      if (tmpdom) {
        // split the string after the domain if there are other values
        strtok(tmpdom, " ");
        if (tmpdom) {
          strncpy((char *)domain, (char *)tmpdom, sizeof(domain) - 1);
          domain[sizeof(domain) - 1] = 0;
        } else {
          err = 1;
        }
      } else {
        err = 1;
      }

      if (err) {
        if (verbose)
          hydra_report(stdout, "[VERBOSE] requested line mode\n");
        accntFlag = 2;
      }
    } else if (strstr(miscptr, "LOCAL") != NULL) {
      accntFlag = 0; // LOCAL
    } else if (strstr(miscptr, "DOMAIN") != NULL) {
      accntFlag = 1; // DOMAIN
    }
    // check pass
    if (strstr(miscptr, "HASH") != NULL) {
      hashFlag = 1;
    } else if (strstr(miscptr, "MACHINE") != NULL) {
      hashFlag = 2;
    }
    // check auth
    if (strstr(miscptr, "NTLMV2") != NULL) {
      smb_auth_mechanism = AUTH_NTLMv2;
    } else if (strstr(miscptr, "NTLM") != NULL) {
      smb_auth_mechanism = AUTH_NTLM;
    } else if (strstr(miscptr, "LMV2") != NULL) {
      smb_auth_mechanism = AUTH_LMv2;
    } else if (strstr(miscptr, "LM") != NULL) {
      smb_auth_mechanism = AUTH_LM;
    }
  }
  if (verbose) {
    hydra_report(stdout, "[VERBOSE] accntFlag is %d\n", accntFlag);
    hydra_report(stdout, "[VERBOSE] hashFlag is %d\n", accntFlag);
  }

  hydra_register_socket(sp);
  if (memcmp(hydra_get_next_pair(), &HYDRA_EXIT, sizeof(HYDRA_EXIT)) == 0)
    return;
  for (;;) {
    switch (run) {
    case 1: /* connect and service init function */
      if (sock >= 0)
        sock = hydra_disconnect(sock);
      //      usleepn(300);

      if (port != 0) {
        sock = hydra_connect_tcp(ip, port);
        if (port == PORT_SMB) {
          protoFlag = WIN_NETBIOSMODE;
          if (verbose)
            hydra_report(stderr, "[VERBOSE] Attempting NETBIOS mode.\n");
        } else {
          protoFlag = WIN2000_NATIVEMODE;
          if (verbose)
            hydra_report(stderr, "[VERBOSE] Attempting WIN2K Native mode.\n");
        }
      } else {
        sock = hydra_connect_tcp(ip, PORT_SMBNT);
        if (sock > 0) {
          port = PORT_SMBNT;
          protoFlag = WIN2000_NATIVEMODE;
        } else {
          hydra_report(stderr, "Failed to establish WIN2000_NATIVE mode. "
                               "Attempting WIN_NETBIOS mode.\n");
          port = PORT_SMB;
          protoFlag = WIN_NETBIOSMODE;
          sock = hydra_connect_tcp(ip, PORT_SMB);
        }
      }
      if (sock < 0) {
        if (quiet != 1)
          fprintf(stderr, "[ERROR] Child with pid %d terminating, can not connect\n", (int32_t)getpid());
        hydra_child_exit(1);
      }
      if (NBSSessionRequest(sock) < 0) {
        fprintf(stderr, "[ERROR] Session Setup Failed (is the server service running?)\n");
        hydra_child_exit(2);
      }
      next_run = SMBNegProt(sock);
      break;
    case 2: /* run the cracking function */
      next_run = start_smb(sock, ip, port, options, miscptr, fp);
      break;
    case 3: /* clean exit */
      if (sock >= 0)
        sock = hydra_disconnect(sock);
      hydra_child_exit(0);
      return;
    default:
      fprintf(stderr, "[ERROR] Caught unknown return code (%d), exiting!\n", run);
      hydra_child_exit(0);
    }
    run = next_run;
  }
}
#endif

int32_t service_smb_init(char *ip, int32_t sp, unsigned char options, char *miscptr, FILE *fp, int32_t port, char *hostname) {
  // called before the childrens are forked off, so this is the function
  // which should be filled if initial connections and service setup has to be
  // performed once only.
  //
  // fill if needed.
  //
  // return codes:
  //   0 all OK
  //   -1  error, hydra will exit, so print a good error message here
  time_t ctime;
  int ready = 0, sock = hydra_connect_tcp(ip, port);
  unsigned char buf[] = {0x00, 0x00, 0x00, 0xbe, 0xff, 0x53, 0x4d, 0x42, 0x72, 0x00, 0x00, 0x00, 0x00, 0x18, 0x43, 0xc8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfe, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x9b, 0x00, 0x02, 0x50, 0x43, 0x20, 0x4e, 0x45, 0x54, 0x57, 0x4f, 0x52, 0x4b, 0x20, 0x50, 0x52, 0x4f, 0x47, 0x52, 0x41, 0x4d, 0x20, 0x31, 0x2e, 0x30, 0x00, 0x02, 0x4d,
                         0x49, 0x43, 0x52, 0x4f, 0x53, 0x4f, 0x46, 0x54, 0x20, 0x4e, 0x45, 0x54, 0x57, 0x4f, 0x52, 0x4b, 0x53, 0x20, 0x31, 0x2e, 0x30, 0x33, 0x00, 0x02, 0x4d, 0x49, 0x43, 0x52, 0x4f, 0x53, 0x4f, 0x46, 0x54, 0x20, 0x4e, 0x45, 0x54, 0x57, 0x4f, 0x52, 0x4b, 0x53, 0x20, 0x33, 0x2e, 0x30, 0x00, 0x02, 0x4c, 0x41, 0x4e, 0x4d, 0x41, 0x4e, 0x31, 0x2e, 0x30, 0x00, 0x02, 0x4c, 0x4d, 0x31, 0x2e, 0x32, 0x58,
                         0x30, 0x30, 0x32, 0x00, 0x02, 0x44, 0x4f, 0x53, 0x20, 0x4c, 0x41, 0x4e, 0x4d, 0x41, 0x4e, 0x32, 0x2e, 0x31, 0x00, 0x02, 0x4c, 0x41, 0x4e, 0x4d, 0x41, 0x4e, 0x32, 0x2e, 0x31, 0x00, 0x02, 0x53, 0x61, 0x6d, 0x62, 0x61, 0x00, 0x02, 0x4e, 0x54, 0x20, 0x4c, 0x41, 0x4e, 0x4d, 0x41, 0x4e, 0x20, 0x31, 0x2e, 0x30, 0x00, 0x02, 0x4e, 0x54, 0x20, 0x4c, 0x4d, 0x20, 0x30, 0x2e, 0x31, 0x32, 0x00};

  if (sock < 0) {
    fprintf(stderr, "[ERROR] could not connect to target smb://%s:%d/\n", hostname, port);
    return -1;
  }

  if (send(sock, buf, sizeof(buf), 0) < 0) {
    fprintf(stderr, "[ERROR] unable to send to target smb://%s:%d/\n", hostname, port);
    return -1;
  }

  ctime = time(NULL);
  do {
    usleepn(300);
  } while ((ready = hydra_data_ready(sock)) <= 0 && ctime + 5 >= time(NULL));

  if (ready <= 0) {
    fprintf(stderr, "[ERROR] no reply from target smb://%s:%d/\n", hostname, port);
    return -1;
  }

  if ((ready = recv(sock, buf, sizeof(buf), 0)) < 40) {
    fprintf(stderr, "[ERROR] invalid reply from target smb://%s:%d/\n", hostname, port);
    return -1;
  }

  close(sock);

  if (buf[37] == buf[38] && buf[38] == 0xff) {
    fprintf(stderr, "[ERROR] target smb://%s:%d/ does not support SMBv1\n", hostname, port);
    return -1;
  }

  if ((buf[15] & 16) == 16) {
    fprintf(stderr,
            "[ERROR] target smb://%s:%d/ requires signing which we do not "
            "support\n",
            hostname, port);
    return -1;
  }

  return 0;
}

void usage_smb(const char *service) {
  printf("Module smb default value is set to test both local and domain account, "
         "using a simple password with NTLM dialect.\n"
         "Note: you can set the group type using LOCAL or DOMAIN keyword\n"
         "      or other_domain:{value} to specify a trusted domain.\n"
         "      you can set the password type using HASH or MACHINE keyword\n"
         "      (to use the Machine's NetBIOS name as the password).\n"
         "      you can set the dialect using NTLMV2, NTLM, LMV2, LM keyword.\n"
         "Example: \n"
         "      hydra smb://microsoft.com  -l admin -p tooeasy -m \"local lmv2\"\n"
         "      hydra smb://microsoft.com  -l admin -p "
         "D5731CFC6C2A069C21FD0D49CAEBC9EA:2126EE7712D37E265FD63F2C84D2B13D::: -m "
         "\"local hash\"\n"
         "      hydra smb://microsoft.com  -l admin -p tooeasy -m "
         "\"other_domain:SECONDDOMAIN\"\n\n");
}