Class: Rex::Proto::NTLM::Utils

Inherits:
Object
  • Object
show all
Defined in:
lib/rex/proto/ntlm/utils.rb

Class Method Summary collapse

Class Method Details

.asn1encode(str = '') ⇒ Object

Prepends an ASN1 formatted length field to a piece of data



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/rex/proto/ntlm/utils.rb', line 23

def self.asn1encode(str = '')
  res = ''

  # If the high bit of the first byte is 1, it contains the number of
  # length bytes that follow

  case str.length
    when 0 .. 0x7F
      res = [str.length].pack('C') + str
    when 0x80 .. 0xFF
      res = [0x81, str.length].pack('CC') + str
    when 0x100 .. 0xFFFF
      res = [0x82, str.length].pack('Cn') + str
    when  0x10000 .. 0xffffff
      res = [0x83, str.length >> 16, str.length & 0xFFFF].pack('CCn') + str
    when  0x1000000 .. 0xffffffff
      res = [0x84, str.length].pack('CN') + str
    else
      raise "ASN1 str too long"
  end
  return res
end

.create_lm_ntlm_responses(user, pass, challenge_key, domain = '', default_name = '', default_domain = '', dns_host_name = '', dns_domain_name = '', chall_MsvAvTimestamp = nil, spnopt = {}, opt = {}) ⇒ Object

create lm/ntlm responses



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
# File 'lib/rex/proto/ntlm/utils.rb', line 478

def self.create_lm_ntlm_responses(user, pass, challenge_key, domain = '', default_name = '', default_domain = '',
        dns_host_name = '', dns_domain_name = '', chall_MsvAvTimestamp = nil, spnopt = {}, opt = {} )

  usentlm2_session 	= opt[:usentlm2_session]	!= nil ? opt[:usentlm2_session] : true
  use_ntlmv2 		= opt[:use_ntlmv2] 		!= nil ? opt[:use_ntlmv2] : false
  send_lm 		= opt[:send_lm] 		!= nil ? opt[:send_lm] : true
  send_ntlm 		= opt[:send_ntlm] 		!= nil ? opt[:send_ntlm] : true

  #calculate the lm/ntlm response
  resp_lm = "\x00" * 24
  resp_ntlm = "\x00" * 24

  client_challenge = Rex::Text.rand_text(8)
  ntlm_cli_challenge = ''
  if send_ntlm  #should be default
    if usentlm2_session
      if use_ntlmv2
        ntlm_cli_challenge = self.make_ntlmv2_clientchallenge(
          default_domain, default_name, dns_domain_name,
          dns_host_name,client_challenge,
          chall_MsvAvTimestamp, spnopt)

        if self.is_pass_ntlm_hash?(pass)
          argntlm = {
            :ntlmv2_hash => Rex::Proto::NTLM::Crypt::ntlmv2_hash(
                      user,
                      [ pass.upcase()[33,65] ].pack('H32'),
                      domain,{:pass_is_hash => true}
                    ),
            :challenge   => challenge_key
          }
        else
          argntlm = {
            :ntlmv2_hash =>  Rex::Proto::NTLM::Crypt::ntlmv2_hash(user, pass, domain),
            :challenge   =>  challenge_key
          }
        end

        optntlm = { :nt_client_challenge => ntlm_cli_challenge}
        ntlmv2_response = Rex::Proto::NTLM::Crypt::ntlmv2_response(argntlm,optntlm)
        resp_ntlm = ntlmv2_response

        if send_lm
          if self.is_pass_ntlm_hash?(pass)
            arglm = {
              :ntlmv2_hash =>  Rex::Proto::NTLM::Crypt::ntlmv2_hash(
                        user,
                        [ pass.upcase()[33,65] ].pack('H32'),
                        domain,{:pass_is_hash => true}
                      ),
              :challenge   => challenge_key
            }
          else
            arglm = {
              :ntlmv2_hash =>  Rex::Proto::NTLM::Crypt::ntlmv2_hash(user,pass, domain),
              :challenge   => challenge_key
            }
          end

          optlm = { :client_challenge => client_challenge }
          resp_lm = Rex::Proto::NTLM::Crypt::lmv2_response(arglm, optlm)
        else
          resp_lm = "\x00" * 24
        end

      else # ntlm2_session
        if self.is_pass_ntlm_hash?(pass)
          argntlm = {
            :ntlm_hash =>  [ pass.upcase()[33,65] ].pack('H32'),
            :challenge => challenge_key
          }
        else
          argntlm = {
            :ntlm_hash =>  Rex::Proto::NTLM::Crypt::ntlm_hash(pass),
            :challenge => challenge_key
          }
        end

        optntlm = {	:client_challenge => client_challenge}
        resp_ntlm = Rex::Proto::NTLM::Crypt::ntlm2_session(argntlm,optntlm).join[24,24]

        # Generate the fake LANMAN hash
        resp_lm = client_challenge + ("\x00" * 16)
      end

    else # we use lmv1/ntlmv1
      if self.is_pass_ntlm_hash?(pass)
        argntlm = {
          :ntlm_hash =>  [ pass.upcase()[33,65] ].pack('H32'),
          :challenge =>  challenge_key
        }
      else
        argntlm = {
          :ntlm_hash =>  Rex::Proto::NTLM::Crypt::ntlm_hash(pass),
          :challenge =>  challenge_key
        }
      end

      resp_ntlm = Rex::Proto::NTLM::Crypt::ntlm_response(argntlm)
      if send_lm
        if self.is_pass_ntlm_hash?(pass)
          arglm = {
            :lm_hash => [ pass.upcase()[0,32] ].pack('H32'),
            :challenge =>  challenge_key
          }
        else
          arglm = {
            :lm_hash => Rex::Proto::NTLM::Crypt::lm_hash(pass),
            :challenge =>  challenge_key
          }
        end
        resp_lm = Rex::Proto::NTLM::Crypt::lm_response(arglm)
      else
        #when windows does not send lm in ntlmv1 type response,
        # it gives lm response the same value as ntlm response
        resp_lm  = resp_ntlm
      end
    end
  else #send_ntlm = false
    #lmv2
    if usentlm2_session && use_ntlmv2
      if self.is_pass_ntlm_hash?(pass)
        arglm = {
          :ntlmv2_hash =>  Rex::Proto::NTLM::Crypt::ntlmv2_hash(
                    user,
                    [ pass.upcase()[33,65] ].pack('H32'),
                    domain,{:pass_is_hash => true}
                  ),
          :challenge => challenge_key
        }
      else
        arglm = {
          :ntlmv2_hash =>  Rex::Proto::NTLM::Crypt::ntlmv2_hash(user,pass, domain),
          :challenge => challenge_key
        }
      end
      optlm = { :client_challenge => client_challenge }
      resp_lm = Rex::Proto::NTLM::Crypt::lmv2_response(arglm, optlm)
    else
      if self.is_pass_ntlm_hash?(pass)
        arglm = {
          :lm_hash => [ pass.upcase()[0,32] ].pack('H32'),
          :challenge =>  challenge_key
        }
      else
        arglm = {
          :lm_hash => Rex::Proto::NTLM::Crypt::lm_hash(pass),
          :challenge =>  challenge_key
        }
      end
      resp_lm = Rex::Proto::NTLM::Crypt::lm_response(arglm)
    end
    resp_ntlm = ""
  end
  return resp_lm, resp_ntlm, client_challenge, ntlm_cli_challenge
end

.create_session_key(ntlmssp_flags, server_ntlmssp_flags, user, pass, domain, challenge_key, client_challenge = '', ntlm_cli_challenge = '', opt = {}) ⇒ Object

create the session key



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
# File 'lib/rex/proto/ntlm/utils.rb', line 636

def self.create_session_key(ntlmssp_flags, server_ntlmssp_flags, user, pass, domain, challenge_key,
        client_challenge = '', ntlm_cli_challenge = '' , opt = {} )

  usentlm2_session 	= opt[:usentlm2_session]	!= nil ? opt[:usentlm2_session] : true
  use_ntlmv2 		= opt[:use_ntlmv2] 		!= nil ? opt[:use_ntlmv2] : false
  send_lm 		= opt[:send_lm] 		!= nil ? opt[:send_lm] : true
  send_ntlm 		= opt[:send_ntlm] 		!= nil ? opt[:send_ntlm] : true
  use_lanman_key 		= opt[:use_lanman_key] 		!= nil ? opt[:use_lanman_key] : false

  # Create the sessionkey (aka signing key, aka mackey) and encrypted session key
  # Server will decide for key_size and key_exchange
  enc_session_key = ''
  signing_key = ''

  # Set default key size and key exchange values
  key_size = 40
  key_exchange = false
  # Remove ntlmssp.negotiate56
  ntlmssp_flags &= 0x7fffffff
  # Remove ntlmssp.negotiatekeyexch
  ntlmssp_flags &= 0xbfffffff
  # Remove ntlmssp.negotiate128
  ntlmssp_flags &= 0xdfffffff
  # Check the keyexchange
  if server_ntlmssp_flags & Rex::Proto::NTLM::Constants::NEGOTIATE_KEY_EXCH != 0 then
    key_exchange = true
    ntlmssp_flags |= Rex::Proto::NTLM::Constants::NEGOTIATE_KEY_EXCH
  end
  # Check 128bits
  if server_ntlmssp_flags & Rex::Proto::NTLM::Constants::NEGOTIATE_128 != 0 then
    key_size = 128
    ntlmssp_flags |= Rex::Proto::NTLM::Constants::NEGOTIATE_128
    ntlmssp_flags |= Rex::Proto::NTLM::Constants::NEGOTIATE_56
  # Check 56bits
  else
    if server_ntlmssp_flags & Rex::Proto::NTLM::Constants::NEGOTIATE_56 != 0 then
      key_size = 56
      ntlmssp_flags |= Rex::Proto::NTLM::Constants::NEGOTIATE_56
    end
  end
  # Generate the user session key
  lanman_weak = false
  if send_ntlm  # Should be default
    if usentlm2_session
      if use_ntlmv2
        if self.is_pass_ntlm_hash?(pass)
          user_session_key = Rex::Proto::NTLM::Crypt::ntlmv2_user_session_key(user,
                      [ pass.upcase()[33,65] ].pack('H32'),
                      domain,
                      challenge_key, ntlm_cli_challenge,
                      {:pass_is_hash => true})
        else
          user_session_key = Rex::Proto::NTLM::Crypt::ntlmv2_user_session_key(user, pass, domain,
                    challenge_key, ntlm_cli_challenge)
        end
      else
        if self.is_pass_ntlm_hash?(pass)
          user_session_key = Rex::Proto::NTLM::Crypt::ntlm2_session_user_session_key([ pass.upcase()[33,65] ].pack('H32'),
                          challenge_key,
                          client_challenge,
                          {:pass_is_hash => true})
        else
          user_session_key = Rex::Proto::NTLM::Crypt::ntlm2_session_user_session_key(pass, challenge_key,
                          client_challenge)
        end
      end
    else # lmv1/ntlmv1
      # lanman_key may also be used without ntlm response but it is not so much used
      # so we don't care about this feature
      if send_lm && use_lanman_key
        if self.is_pass_ntlm_hash?(pass)
          user_session_key = Rex::Proto::NTLM::Crypt::lanman_session_key([ pass.upcase()[0,32] ].pack('H32'),
                      challenge_key,
                      {:pass_is_hash => true})
        else
          user_session_key = Rex::Proto::NTLM::Crypt::lanman_session_key(pass, challenge_key)
        end
        lanman_weak = true


      else
        if self.is_pass_ntlm_hash?(pass)
          user_session_key = Rex::Proto::NTLM::Crypt::ntlmv1_user_session_key([ pass.upcase()[33,65] ].pack('H32'),
                        {:pass_is_hash => true})
        else
          user_session_key = Rex::Proto::NTLM::Crypt::ntlmv1_user_session_key(pass)
        end
      end
    end
  else
      if usentlm2_session && use_ntlmv2
        if self.is_pass_ntlm_hash?(pass)
          user_session_key = Rex::Proto::NTLM::Crypt::lmv2_user_session_key(user, [ pass.upcase()[33,65] ].pack('H32'),
                      domain,
                      challenge_key, client_challenge,
                      {:pass_is_hash => true})
        else
          user_session_key = Rex::Proto::NTLM::Crypt::lmv2_user_session_key(user, pass, domain,
                      challenge_key, client_challenge)
        end
      else
        if self.is_pass_ntlm_hash?(pass)
          user_session_key = Rex::Proto::NTLM::Crypt::lmv1_user_session_key([ pass.upcase()[0,32] ].pack('H32'),
                        {:pass_is_hash => true})
        else
          user_session_key = Rex::Proto::NTLM::Crypt::lmv1_user_session_key(pass)
        end
      end
  end

  user_session_key = Rex::Proto::NTLM::Crypt::make_weak_sessionkey(user_session_key,key_size, lanman_weak)

  # Sessionkey and encrypted session key
  if key_exchange
    signing_key = Rex::Text.rand_text(16)
    enc_session_key = Rex::Proto::NTLM::Crypt::encrypt_sessionkey(signing_key, user_session_key)
  else
    signing_key = user_session_key
  end

  return signing_key, enc_session_key, ntlmssp_flags
end

.is_pass_ntlm_hash?(str) ⇒ Boolean

Determine whether the password is a known hash format

Returns:

  • (Boolean)


16
17
18
# File 'lib/rex/proto/ntlm/utils.rb', line 16

def self.is_pass_ntlm_hash?(str)
  str.downcase =~ /^[0-9a-f]{32}:[0-9a-f]{32}$/
end

.make_negotiate_secblob_resp(account, domain) ⇒ Object

GSS BLOB useful for SMB_NEGOCIATE_RESPONSE message mechTypes: 4 items : MechType: 1.2.840.48018.1.2.2 (MS KRB5 - Microsoft Kerberos 5) MechType: 1.2.840.113554.1.2.2 (KRB5 - Kerberos 5) MechType: 1.2.840.113554.1.2.2.3 (KRB5 - Kerberos 5 - User to User) MechType: 1.3.6.1.4.1.311.2.2.10 (NTLMSSP - Microsoft NTLM Security Support Provider) mechListMIC: principal: account@domain



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
# File 'lib/rex/proto/ntlm/utils.rb', line 84

def self.make_negotiate_secblob_resp(, domain)
  blob =
  "\x60" + self.asn1encode(
    "\x06" + self.asn1encode(
      "\x2b\x06\x01\x05\x05\x02"
    ) +
    "\xa0" + self.asn1encode(
      "\x30" + self.asn1encode(
        "\xa0" + self.asn1encode(
          "\x30" + self.asn1encode(
            "\x06" + self.asn1encode(
              "\x2a\x86\x48\x82\xf7\x12\x01\x02\x02"
            ) +
            "\x06" + self.asn1encode(
              "\x2a\x86\x48\x86\xf7\x12\x01\x02\x02"
            ) +
            "\x06" + self.asn1encode(
              "\x2a\x86\x48\x86\xf7\x12\x01\x02\x02\x03"
            ) +
            "\x06" + self.asn1encode(
              "\x2b\x06\x01\x04\x01\x82\x37\x02\x02\x0a"
            )
          )
        ) +
        "\xa3" + self.asn1encode(
          "\x30" + self.asn1encode(
            "\xa0" + self.asn1encode(
              "\x1b" + self.asn1encode(
                 + '@' + domain
              )
            )
          )
        )
      )
    )
  )

  return blob
end

.make_ntlm_flags(opt = {}) ⇒ Object

Return the correct ntlmflags upon the configuration



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
# File 'lib/rex/proto/ntlm/utils.rb', line 328

def self.make_ntlm_flags(opt = {})

  signing 		= opt[:signing] 		!= nil ? opt[:signing] : false
  usentlm2_session 	= opt[:usentlm2_session]	!= nil ? opt[:usentlm2_session] : true
  use_ntlmv2 		= opt[:use_ntlmv2] 		!= nil ? opt[:use_ntlmv2] : false
  send_lm 		= opt[:send_lm] 		!= nil ? opt[:send_lm] : true
  send_ntlm 		= opt[:send_ntlm] 		!= nil ? opt[:send_ntlm] : true
  use_lanman_key 		= opt[:use_lanman_key] 		!= nil ? opt[:use_lanman_key] : false

  if signing
    ntlmssp_flags = 0xe2088215
  else

    ntlmssp_flags = 0xa2080205
  end

  if usentlm2_session
    if use_ntlmv2
      #set Negotiate Target Info
      ntlmssp_flags |= Rex::Proto::NTLM::Constants::NEGOTIATE_TARGET_INFO
    end

  else
    #remove the ntlm2_session flag
    ntlmssp_flags &= 0xfff7ffff
    #set lanmanflag only when lm and ntlm are sent
    if send_lm
      ntlmssp_flags |= Rex::Proto::NTLM::Constants::NEGOTIATE_LMKEY if use_lanman_key
    end
  end

  #we can also downgrade ntlm2_session when we send only lmv1
  ntlmssp_flags &= 0xfff7ffff if usentlm2_session && (not use_ntlmv2) && (not send_ntlm)

  return ntlmssp_flags
end

.make_ntlmssp_blob_auth(domain, name, user, lm, ntlm, enc_session_key, flags = 0x080201) ⇒ Object

BLOB without GSS Useful for ntlmssp type 3 message



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
# File 'lib/rex/proto/ntlm/utils.rb', line 233

def self.make_ntlmssp_blob_auth(domain, name, user, lm, ntlm, enc_session_key, flags = 0x080201)
  lm ||= "\x00" * 24
  ntlm ||= "\x00" * 24

  domain_uni = Rex::Text.to_unicode(domain)
  user_uni   = Rex::Text.to_unicode(user)
  name_uni   = Rex::Text.to_unicode(name)
  session    = enc_session_key

  ptr  = 64

  blob = "NTLMSSP\x00" +
    [ 3 ].pack('V') +

    [	# Lan Manager Response
      lm.length,
      lm.length,
      (ptr)
    ].pack('vvV') +

    [	# NTLM Manager Response
      ntlm.length,
      ntlm.length,
      (ptr += lm.length)
    ].pack('vvV') +

    [	# Domain Name
      domain_uni.length,
      domain_uni.length,
      (ptr += ntlm.length)
    ].pack('vvV') +

    [	# Username
      user_uni.length,
      user_uni.length,
      (ptr += domain_uni.length)
    ].pack('vvV') +

    [	# Hostname
      name_uni.length,
      name_uni.length,
      (ptr += user_uni.length)
    ].pack('vvV') +

    [	# Session Key (none)
      session.length,
      session.length,
      (ptr += name_uni.length)
    ].pack('vvV') +

    [ flags ].pack('V') +

    lm +
    ntlm +
    domain_uni +
    user_uni +
    name_uni +
    session + "\x00"
  return blob

end

.make_ntlmssp_blob_chall(win_domain, win_name, dns_domain, dns_name, chall, flags) ⇒ Object

BLOB without GSS useful for ntlm type 2 message



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
# File 'lib/rex/proto/ntlm/utils.rb', line 175

def self.make_ntlmssp_blob_chall(win_domain, win_name, dns_domain, dns_name, chall, flags)

  addr_list  = ''
  addr_list  << [2, win_domain.length].pack('vv') + win_domain
  addr_list  << [1, win_name.length].pack('vv') + win_name
  addr_list  << [4, dns_domain.length].pack('vv') + dns_domain
  addr_list  << [3, dns_name.length].pack('vv') + dns_name
  addr_list  << [0, 0].pack('vv')

  ptr  = 0
  blob =	"NTLMSSP\x00" +
      [2].pack('V') +
      [
        win_domain.length,  # length
        win_domain.length,  # max length
        (ptr += 48) # offset
      ].pack('vvV') +
      [ flags ].pack('V') +
      chall +
      "\x00\x00\x00\x00\x00\x00\x00\x00" +
      [
        addr_list.length,  # length
        addr_list.length,  # max length
        (ptr += win_domain.length)
      ].pack('vvV') +
      win_domain +
      addr_list
  return blob
end

.make_ntlmssp_blob_init(domain = 'WORKGROUP', name = 'WORKSTATION', flags = 0x80201) ⇒ Object

BLOB without GSS useful for ntlmssp type 1 message



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/rex/proto/ntlm/utils.rb', line 125

def self.make_ntlmssp_blob_init(domain = 'WORKGROUP', name = 'WORKSTATION', flags=0x80201)
  blob =	"NTLMSSP\x00" +
    [1, flags].pack('VV') +

    [
      domain.length,  #length
      domain.length,  #max length
      32
    ].pack('vvV') +

    [
      name.length,	#length
      name.length, 	#max length
      domain.length + 32
    ].pack('vvV') +

    domain + name
  return blob
end

.make_ntlmssp_secblob_auth(domain, name, user, lm, ntlm, enc_session_key, flags = 0x080201) ⇒ Object

GSS BLOB Useful for ntlmssp type 3 message



296
297
298
299
300
301
302
303
304
305
306
307
308
309
# File 'lib/rex/proto/ntlm/utils.rb', line 296

def self.make_ntlmssp_secblob_auth(domain, name, user, lm, ntlm, enc_session_key, flags = 0x080201)

  blob =
    "\xa1" + self.asn1encode(
      "\x30" + self.asn1encode(
        "\xa2" + self.asn1encode(
          "\x04" + self.asn1encode(
          make_ntlmssp_blob_auth(domain, name, user, lm, ntlm, enc_session_key, flags )
        )
      )
    )
  )
  return blob
end

.make_ntlmssp_secblob_chall(win_domain, win_name, dns_domain, dns_name, chall, flags) ⇒ Object

GSS BLOB useful for ntlmssp type 2 message



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
# File 'lib/rex/proto/ntlm/utils.rb', line 206

def self.make_ntlmssp_secblob_chall(win_domain, win_name, dns_domain, dns_name, chall, flags)

  blob =
    "\xa1" + self.asn1encode(
      "\x30" + self.asn1encode(
        "\xa0" + self.asn1encode(
          "\x0a" + self.asn1encode(
            "\x01"
          )
        ) +
        "\xa1" + self.asn1encode(
          "\x06" + self.asn1encode(
            "\x2b\x06\x01\x04\x01\x82\x37\x02\x02\x0a"
          )
        ) +
        "\xa2" + self.asn1encode(
          "\x04" + self.asn1encode(
            make_ntlmssp_blob_chall(win_domain, win_name, dns_domain, dns_name, chall, flags)
          )
        )
      )
    )

  return blob
end

.make_ntlmssp_secblob_init(domain = 'WORKGROUP', name = 'WORKSTATION', flags = 0x80201) ⇒ Object

GSS BLOB useful for ntlmssp type 1 message



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
# File 'lib/rex/proto/ntlm/utils.rb', line 146

def self.make_ntlmssp_secblob_init(domain = 'WORKGROUP', name = 'WORKSTATION', flags=0x80201)
  blob =
  "\x60" + self.asn1encode(
    "\x06" + self.asn1encode(
      "\x2b\x06\x01\x05\x05\x02"
    ) +
    "\xa0" + self.asn1encode(
      "\x30" + self.asn1encode(
        "\xa0" + self.asn1encode(
          "\x30" + self.asn1encode(
            "\x06" + self.asn1encode(
              "\x2b\x06\x01\x04\x01\x82\x37\x02\x02\x0a"
            )
          )
        ) +
        "\xa2" + self.asn1encode(
          "\x04" + self.asn1encode(
            make_ntlmssp_blob_init(domain, name, flags)
          )
        )
      )
    )
  )

  return blob
end

.make_ntlmv2_clientchallenge(win_domain, win_name, dns_domain, dns_name, client_challenge = nil, chall_MsvAvTimestamp = nil, spnopt = {}) ⇒ Object

This function return an ntlmv2 client challenge This is a partial implementation, full description is in [MS-NLMP].pdf around 3.1.5.2.1 :-/



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
# File 'lib/rex/proto/ntlm/utils.rb', line 429

def self.make_ntlmv2_clientchallenge(win_domain, win_name, dns_domain, dns_name,
          client_challenge = nil, chall_MsvAvTimestamp = nil, spnopt = {})

  client_challenge ||= Rex::Text.rand_text(8)
  # We have to set the timestamps here to the one in the challenge message from server if present
  # If we don't do that, recent server like Seven/2008 will send a STATUS_INVALID_PARAMETER error packet
  timestamp = chall_MsvAvTimestamp != '' ? chall_MsvAvTimestamp : self.time_unix_to_smb(::Time.now.to_i).reverse.pack("VV")
  # Make those values unicode as requested
  win_domain = Rex::Text.to_unicode(win_domain)
  win_name = Rex::Text.to_unicode(win_name)
  dns_domain = Rex::Text.to_unicode(dns_domain)
  dns_name = Rex::Text.to_unicode(dns_name)
  # Make the AV_PAIRs
  addr_list  = ''
  addr_list  << [2, win_domain.length].pack('vv') + win_domain
  addr_list  << [1, win_name.length].pack('vv') + win_name
  addr_list  << [4, dns_domain.length].pack('vv') + dns_domain
  addr_list  << [3, dns_name.length].pack('vv') + dns_name
  addr_list  << [7, 8].pack('vv') + timestamp

  # Windows Seven / 2008r2 Request this type if in local security policies,
  # Microsoft network server : Server SPN target name validation level is set to <Required from client>
  # otherwise it send an STATUS_ACCESS_DENIED packet
  if spnopt[:use_spn]
    spn= Rex::Text.to_unicode("cifs/#{spnopt[:name] || 'unknown'}")
    addr_list  << [9, spn.length].pack('vv') + spn
  end

  # MAY BE USEFUL FOR FUTURE
  # Seven (client) add at least one more av that is of type MsAvRestrictions (8)
  # maybe this will be useful with future windows OSs but has no use at all for the moment afaik
  # restriction_encoding = 	[48,0,0,0].pack("VVV") + # Size, Z4, IntegrityLevel, SubjectIntegrityLevel
  # 			Rex::Text.rand_text(32)	 # MachineId generated on startup on win7 and above
  # addr_list  << [8, restriction_encoding.length].pack('vv') + restriction_encoding

  # Seven (client) and maybe others versions also add an av of type MsvChannelBindings (10) but the hash is "\x00" * 16
  # addr_list  << [10, 16].pack('vv') + "\x00" * 16


  addr_list  << [0, 0].pack('vv')
  ntlm_clientchallenge = 	[1,1,0,0].pack("CCvV") + #RespType, HiRespType, Reserved1, Reserved2
        timestamp + #Timestamp
        client_challenge + 	#clientchallenge
        [0].pack("V")  +	#Reserved3
        addr_list + "\x00" * 4

end

.make_ntlmv2_secblob_successObject

GSS BLOB Useful for SMB Success



313
314
315
316
317
318
319
320
321
322
323
324
325
# File 'lib/rex/proto/ntlm/utils.rb', line 313

def self.make_ntlmv2_secblob_success
  blob =
    "\xa1" + self.asn1encode(
      "\x30" + self.asn1encode(
        "\xa0" + self.asn1encode(
          "\x0a" + self.asn1encode(
            "\x00"
          )
        )
      )
    )
  return blob
end

.make_simple_negotiate_secblob_respObject

GSS BLOB useful for SMB_NEGOCIATE_RESPONSE message mechTypes: 2 items : -MechType: 1.3.6.1.4.1.311.2.2.30 (SNMPv2-SMI::enterprises.311.2.2.30) -MechType: 1.3.6.1.4.1.311.2.2.10 (NTLMSSP - Microsoft NTLM Security Support Provider)

this is the default on Win7



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/rex/proto/ntlm/utils.rb', line 54

def self.make_simple_negotiate_secblob_resp
  blob =
  "\x60" + self.asn1encode(
    "\x06" + self.asn1encode(
      "\x2b\x06\x01\x05\x05\x02"
    ) +
    "\xa0" + self.asn1encode(
      "\x30" + self.asn1encode(
        "\xa0" + self.asn1encode(
          "\x30" + self.asn1encode(
            "\x06" + self.asn1encode(
              "\x2b\x06\x01\x04\x01\x82\x37\x02\x02\x0a"
            )
          )
        )
      )
    )
  )

  return blob
end

.parse_ntlm_type_2_blob(blob) ⇒ Object

Parse an ntlm type 2 challenge blob and return useful data



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
# File 'lib/rex/proto/ntlm/utils.rb', line 367

def self.parse_ntlm_type_2_blob(blob)
  data = {}
  # Extract the NTLM challenge key the lazy way
  cidx = blob.index("NTLMSSP\x00\x02\x00\x00\x00")

  if not cidx
    raise Rex::Proto::NTLM::Exceptions::NTLMMissingChallenge
  end

  data[:challenge_key] = blob[cidx + 24, 8]

  data[:server_ntlmssp_flags] = blob[cidx + 20, 4].unpack("V")[0]

  # Extract the address list from the blob
  alist_len,alist_mlen,alist_off = blob[cidx + 40, 8].unpack("vvV")
  alist_buf = blob[cidx + alist_off, alist_len]

  while(alist_buf.length > 0)
    atype, alen = alist_buf.slice!(0,4).unpack('vv')
    break if atype == 0x00
    addr = alist_buf.slice!(0, alen)
    case atype
    when 1
      #netbios name
      temp_name = addr
      temp_name.force_encoding("UTF-16LE")
      data[:default_name] =  temp_name.encode("UTF-8")
    when 2
      #netbios domain
      temp_domain = addr
      temp_domain.force_encoding("UTF-16LE")
      data[:default_domain] =  temp_domain.encode("UTF-8")
    when 3
      #dns name
      temp_dns = addr
      temp_dns.force_encoding("UTF-16LE")
      data[:dns_host_name] =  temp_dns.encode("UTF-8")
    when 4
      #dns domain
      temp_dns_domain = addr
      temp_dns_domain.force_encoding("UTF-16LE")
      data[:dns_domain_name] =  temp_dns_domain.encode("UTF-8")
    when 5
      #The FQDN of the forest.
    when 6
      #A 32-bit value indicating server or client configuration
    when 7
      #Client time
      data[:chall_MsvAvTimestamp] = addr
    when 8
      #A Restriction_Encoding structure
    when 9
      #The SPN of the target server.
    when 10
      #A channel bindings hash.
    end
  end
  return data
end

.time_unix_to_smb(unix_time) ⇒ Object

duplicate from lib/rex/proto/smb/utils cause we only need this function from Rex::Proto::SMB::Utils Convert a unix timestamp to a 64-bit signed server time



8
9
10
11
12
13
# File 'lib/rex/proto/ntlm/utils.rb', line 8

def self.time_unix_to_smb(unix_time)
  t64 = (unix_time + 11644473600) * 10000000
  thi = (t64 & 0xffffffff00000000) >> 32
  tlo = (t64 & 0x00000000ffffffff)
  return [thi, tlo]
end