@@ -132,11 +132,11 @@ def mssql_xpcmdshell(cmd, doprint=false, opts={})
132
132
force_enable = false
133
133
begin
134
134
res = mssql_query ( "EXEC master..xp_cmdshell '#{ cmd } '" , false , opts )
135
- if ( res [ :errors ] and not res [ :errors ] . empty? )
136
- if ( res [ :errors ] . join =~ /xp_cmdshell/ )
137
- if ( force_enable )
135
+ if res [ :errors ] && ! res [ :errors ] . empty?
136
+ if res [ :errors ] . join =~ /xp_cmdshell/
137
+ if force_enable
138
138
print_error ( "The xp_cmdshell procedure is not available and could not be enabled" )
139
- raise RuntimeError , "Failed to execute command"
139
+ raise RuntimeError , "Failed to execute command"
140
140
else
141
141
print_status ( "The server may have xp_cmdshell disabled, trying to enable it..." )
142
142
mssql_query ( mssql_xpcmdshell_enable ( ) )
@@ -150,7 +150,7 @@ def mssql_xpcmdshell(cmd, doprint=false, opts={})
150
150
return res
151
151
152
152
rescue RuntimeError => e
153
- if ( e . to_s =~ /xp_cmdshell disabled/ )
153
+ if e . to_s =~ /xp_cmdshell disabled/
154
154
force_enable = true
155
155
retry
156
156
end
@@ -243,7 +243,7 @@ def mssql_send_recv(req, timeout=15, check_status = true)
243
243
244
244
while ( not done )
245
245
head = sock . get_once ( 8 , timeout )
246
- if !( head and head . length == 8 )
246
+ if !( head && head . length == 8 )
247
247
return false
248
248
end
249
249
@@ -285,77 +285,77 @@ def mssql_prelogin(enc_error=false)
285
285
pkt_data = ""
286
286
287
287
288
- pkt_hdr = [
289
- TYPE_PRE_LOGIN_MESSAGE , #type
290
- STATUS_END_OF_MESSAGE , #status
291
- 0x0000 , #length
292
- 0x0000 , # SPID
293
- 0x00 , # PacketID
294
- 0x00 #Window
295
- ]
288
+ pkt_hdr = [
289
+ TYPE_PRE_LOGIN_MESSAGE , #type
290
+ STATUS_END_OF_MESSAGE , #status
291
+ 0x0000 , #length
292
+ 0x0000 , # SPID
293
+ 0x00 , # PacketID
294
+ 0x00 #Window
295
+ ]
296
296
297
- version = [ 0x55010008 , 0x0000 ] . pack ( "Vv" )
298
- encryption = ENCRYPT_NOT_SUP # off
299
- instoptdata = "MSSQLServer\0 "
297
+ version = [ 0x55010008 , 0x0000 ] . pack ( "Vv" )
298
+ encryption = ENCRYPT_NOT_SUP # off
299
+ instoptdata = "MSSQLServer\0 "
300
300
301
- threadid = "\0 \0 " + Rex ::Text . rand_text ( 2 )
301
+ threadid = "\0 \0 " + Rex ::Text . rand_text ( 2 )
302
302
303
- idx = 21 # size of pkt_data_token
304
- pkt_data_token << [
305
- 0x00 , # Token 0 type Version
306
- idx , # VersionOffset
307
- version . length , # VersionLength
303
+ idx = 21 # size of pkt_data_token
304
+ pkt_data_token << [
305
+ 0x00 , # Token 0 type Version
306
+ idx , # VersionOffset
307
+ version . length , # VersionLength
308
308
309
- 0x01 , # Token 1 type Encryption
310
- idx = idx + version . length , # EncryptionOffset
311
- 0x01 , # EncryptionLength
309
+ 0x01 , # Token 1 type Encryption
310
+ idx = idx + version . length , # EncryptionOffset
311
+ 0x01 , # EncryptionLength
312
312
313
- 0x02 , # Token 2 type InstOpt
314
- idx = idx + 1 , # InstOptOffset
315
- instoptdata . length , # InstOptLength
313
+ 0x02 , # Token 2 type InstOpt
314
+ idx = idx + 1 , # InstOptOffset
315
+ instoptdata . length , # InstOptLength
316
316
317
- 0x03 , # Token 3 type Threadid
318
- idx + instoptdata . length , # ThreadIdOffset
319
- 0x04 , # ThreadIdLength
317
+ 0x03 , # Token 3 type Threadid
318
+ idx + instoptdata . length , # ThreadIdOffset
319
+ 0x04 , # ThreadIdLength
320
320
321
- 0xFF
322
- ] . pack ( "CnnCnnCnnCnnC" )
321
+ 0xFF
322
+ ] . pack ( "CnnCnnCnnCnnC" )
323
323
324
- pkt_data << pkt_data_token
325
- pkt_data << version
326
- pkt_data << encryption
327
- pkt_data << instoptdata
328
- pkt_data << threadid
324
+ pkt_data << pkt_data_token
325
+ pkt_data << version
326
+ pkt_data << encryption
327
+ pkt_data << instoptdata
328
+ pkt_data << threadid
329
329
330
- pkt_hdr [ 2 ] = pkt_data . length + 8
330
+ pkt_hdr [ 2 ] = pkt_data . length + 8
331
331
332
- pkt = pkt_hdr . pack ( "CCnnCC" ) + pkt_data
332
+ pkt = pkt_hdr . pack ( "CCnnCC" ) + pkt_data
333
333
334
- resp = mssql_send_recv ( pkt )
335
-
336
- idx = 0
334
+ resp = mssql_send_recv ( pkt )
337
335
338
- while resp and resp [ 0 , 1 ] != "\xff " and resp . length > 5
339
- token = resp . slice! ( 0 , 5 )
340
- token = token . unpack ( "Cnn" )
341
- idx -= 5
342
- if token [ 0 ] == 0x01
336
+ idx = 0
343
337
344
- idx += token [ 1 ]
345
- break
346
- end
347
- end
348
- if idx > 0
349
- encryption_mode = resp [ idx , 1 ] . unpack ( "C" ) [ 0 ]
350
- else
351
- #force to ENCRYPT_NOT_SUP and hope for the best
352
- encryption_mode = ENCRYPT_NOT_SUP
338
+ while resp && resp [ 0 , 1 ] != "\xff " && resp . length > 5
339
+ token = resp . slice! ( 0 , 5 )
340
+ token = token . unpack ( "Cnn" )
341
+ idx -= 5
342
+ if token [ 0 ] == 0x01
343
+ idx += token [ 1 ]
344
+ break
353
345
end
346
+ end
354
347
355
- if encryption_mode != ENCRYPT_NOT_SUP and enc_error
356
- raise RuntimeError , "Encryption is not supported"
357
- end
358
- encryption_mode
348
+ if idx > 0
349
+ encryption_mode = resp [ idx , 1 ] . unpack ( "C" ) [ 0 ]
350
+ else
351
+ # force to ENCRYPT_NOT_SUP and hope for the best
352
+ encryption_mode = ENCRYPT_NOT_SUP
353
+ end
354
+
355
+ if encryption_mode != ENCRYPT_NOT_SUP && enc_error
356
+ raise RuntimeError , "Encryption is not supported"
357
+ end
358
+ encryption_mode
359
359
end
360
360
361
361
#
@@ -415,13 +415,12 @@ def mssql_login(user='sa', pass='', db='')
415
415
dname = Rex ::Text . to_unicode ( db )
416
416
417
417
workstation_name = Rex ::Text . rand_text_alpha ( rand ( 8 ) +1 )
418
- domain_name = datastore [ 'DOMAIN' ]
419
418
420
419
ntlm_client = ::Net ::NTLM ::Client . new (
421
420
user ,
422
421
pass ,
423
422
workstation : workstation_name ,
424
- domain : domain_name ,
423
+ domain : datastore [ 'DOMAIN' ] ,
425
424
)
426
425
type1 = ntlm_client . init_context
427
426
# SQL 2012, at least, does not support KEY_EXCHANGE
@@ -653,17 +652,17 @@ def mssql_print_reply(info)
653
652
654
653
print_status ( "SQL Query: #{ info [ :sql ] } " )
655
654
656
- if ( info [ :done ] and info [ :done ] [ :rows ] . to_i > 0 )
655
+ if info [ :done ] && info [ :done ] [ :rows ] . to_i > 0
657
656
print_status ( "Row Count: #{ info [ :done ] [ :rows ] } (Status: #{ info [ :done ] [ :status ] } Command: #{ info [ :done ] [ :cmd ] } )" )
658
657
end
659
658
660
- if ( info [ :errors ] and not info [ :errors ] . empty? )
659
+ if info [ :errors ] && ! info [ :errors ] . empty?
661
660
info [ :errors ] . each do |err |
662
661
print_error ( err )
663
662
end
664
663
end
665
664
666
- if ( info [ :rows ] and not info [ :rows ] . empty? )
665
+ if info [ :rows ] && ! info [ :rows ] . empty?
667
666
668
667
tbl = Rex ::Ui ::Text ::Table . new (
669
668
'Indent' => 1 ,
@@ -749,7 +748,7 @@ def mssql_parse_tds_reply(data, info)
749
748
750
749
col [ :msg_len ] = data . slice! ( 0 , 1 ) . unpack ( 'C' ) [ 0 ]
751
750
752
- if ( col [ :msg_len ] and col [ :msg_len ] > 0 )
751
+ if col [ :msg_len ] && col [ :msg_len ] > 0
753
752
col [ :name ] = data . slice! ( 0 , col [ :msg_len ] * 2 ) . gsub ( "\x00 " , '' )
754
753
end
755
754
info [ :colnames ] << ( col [ :name ] || 'NULL' )
@@ -808,15 +807,15 @@ def mssql_parse_tds_row(data, info)
808
807
when :hex
809
808
str = ""
810
809
len = data . slice! ( 0 , 2 ) . unpack ( 'v' ) [ 0 ]
811
- if ( len > 0 and len < 65535 )
810
+ if len > 0 && len < 65535
812
811
str << data . slice! ( 0 , len )
813
812
end
814
813
row << str . unpack ( "H*" ) [ 0 ]
815
814
816
815
when :string
817
816
str = ""
818
817
len = data . slice! ( 0 , 2 ) . unpack ( 'v' ) [ 0 ]
819
- if ( len > 0 and len < 65535 )
818
+ if len > 0 && len < 65535
820
819
str << data . slice! ( 0 , len )
821
820
end
822
821
row << str . gsub ( "\x00 " , '' )
@@ -842,12 +841,12 @@ def mssql_parse_tds_row(data, info)
842
841
when :image
843
842
str = ''
844
843
len = data . slice! ( 0 , 1 ) . unpack ( 'C' ) [ 0 ]
845
- str = data . slice! ( 0 , len ) if ( len and len > 0 )
844
+ str = data . slice! ( 0 , len ) if len && len > 0
846
845
row << str . unpack ( "H*" ) [ 0 ]
847
846
848
847
when :int
849
848
len = data . slice! ( 0 , 1 ) . unpack ( "C" ) [ 0 ]
850
- raw = data . slice! ( 0 , len ) if ( len and len > 0 )
849
+ raw = data . slice! ( 0 , len ) if len && len > 0
851
850
852
851
case len
853
852
when 0 , 255
0 commit comments