@@ -212,23 +212,6 @@ class CredentialCollection < PrivateCredentialCollection
212
212
# @return [Boolean]
213
213
attr_accessor :anonymous_login
214
214
215
- # @!attribute ignore_private
216
- # Whether to ignore private (password). This is usually set when Kerberos
217
- # or Schannel authentication is requested and the credentials are
218
- # retrieved from cache or from a file. This attribute should be true in
219
- # these scenarios, otherwise validation will fail since the password is not
220
- # provided.
221
- # @return [Boolean]
222
- attr_accessor :ignore_private
223
-
224
- # @!attribute ignore_public
225
- # Whether to ignore public (username). This is usually set when Schannel
226
- # authentication is requested and the credentials are retrieved from a
227
- # file (certificate). This attribute should be true in this case,
228
- # otherwise validation will fail since the password is not provided.
229
- # @return [Boolean]
230
- attr_accessor :ignore_public
231
-
232
215
# @option opts [Boolean] :blank_passwords See {#blank_passwords}
233
216
# @option opts [String] :pass_file See {#pass_file}
234
217
# @option opts [String] :password See {#password}
@@ -257,29 +240,29 @@ def add_public(public_str='')
257
240
# @yieldparam credential [Metasploit::Framework::Credential]
258
241
# @return [void]
259
242
def each_filtered
260
- if ignore_private
261
- if ignore_public
262
- yield Metasploit ::Framework ::Credential . new ( public : nil , private : nil , realm : realm )
263
- else
264
- yield Metasploit ::Framework ::Credential . new ( public : username , private : nil , realm : realm )
265
- end
266
- elsif password_spray
267
- each_unfiltered_password_first do |credential |
268
- next unless self . filter . nil? || self . filter . call ( credential )
269
-
270
- yield credential
271
- end
272
- else
273
- each_unfiltered_username_first do |credential |
274
- next unless self . filter . nil? || self . filter . call ( credential )
243
+ each_unfiltered do |credential |
244
+ next unless self . filter . nil? || self . filter . call ( credential )
275
245
276
- yield credential
277
- end
246
+ yield credential
278
247
end
279
248
end
280
249
281
250
alias each each_filtered
282
251
252
+ def each_unfiltered ( &block )
253
+ prepended_creds . each { |c | yield c }
254
+
255
+ if anonymous_login
256
+ yield Metasploit ::Framework ::Credential . new ( public : '' , private : '' , realm : realm , private_type : :password )
257
+ end
258
+
259
+ if password_spray
260
+ each_unfiltered_password_first ( &block )
261
+ else
262
+ each_unfiltered_username_first ( &block )
263
+ end
264
+ end
265
+
283
266
# When password spraying is enabled, do first passwords then usernames
284
267
# i.e.
285
268
# username1:password1
@@ -293,117 +276,72 @@ def each_filtered
293
276
# @yieldparam credential [Metasploit::Framework::Credential]
294
277
# @return [void]
295
278
def each_unfiltered_password_first
296
- if user_file . present?
297
- user_fd = File . open ( user_file , 'r:binary' )
298
- end
299
-
300
- prepended_creds . each { |c | yield c }
301
-
302
- if anonymous_login
303
- yield Metasploit ::Framework ::Credential . new ( public : '' , private : '' , realm : realm , private_type : :password )
304
- end
305
-
306
- if user_as_pass
307
- if user_fd
308
- user_fd . each_line do |user_from_file |
309
- user_from_file . chomp!
310
- yield Metasploit ::Framework ::Credential . new ( public : user_from_file , private : user_from_file , realm : realm , private_type : private_type ( password ) )
311
- end
312
- user_fd . seek ( 0 )
279
+ if nil_passwords
280
+ each_username do |username |
281
+ yield Metasploit ::Framework ::Credential . new ( public : username , private : nil , realm : realm , private_type : :password )
313
282
end
314
283
end
315
284
316
285
if password . present?
317
- if nil_passwords
318
- yield Metasploit ::Framework ::Credential . new ( public : username , private : nil , realm : realm , private_type : :password )
319
- end
320
- if username . present?
286
+ each_username do |username |
321
287
yield Metasploit ::Framework ::Credential . new ( public : username , private : password , realm : realm , private_type : private_type ( password ) )
322
288
end
323
- if user_as_pass
289
+ end
290
+
291
+ if user_as_pass
292
+ each_username do |username |
324
293
yield Metasploit ::Framework ::Credential . new ( public : username , private : username , realm : realm , private_type : :password )
325
294
end
326
- if blank_passwords
295
+ end
296
+
297
+ if blank_passwords
298
+ each_username do |username |
327
299
yield Metasploit ::Framework ::Credential . new ( public : username , private : "" , realm : realm , private_type : :password )
328
300
end
329
- if user_fd
330
- user_fd . each_line do |user_from_file |
331
- user_from_file . chomp!
332
- yield Metasploit ::Framework ::Credential . new ( public : user_from_file , private : password , realm : realm , private_type : private_type ( password ) )
333
- end
334
- user_fd . seek ( 0 )
335
- end
336
301
end
337
302
338
303
if pass_file . present?
339
304
File . open ( pass_file , 'r:binary' ) do |pass_fd |
340
305
pass_fd . each_line do |pass_from_file |
341
306
pass_from_file . chomp!
342
- if username . present?
343
- yield Metasploit ::Framework ::Credential . new ( public : username , private : pass_from_file , realm : realm , private_type : :password )
344
- end
345
- next unless user_fd
346
307
347
- user_fd . each_line do |user_from_file |
348
- user_from_file . chomp!
349
- yield Metasploit ::Framework ::Credential . new ( public : user_from_file , private : pass_from_file , realm : realm , private_type : private_type ( pass_from_file ) )
308
+ each_username do |username |
309
+ yield Metasploit ::Framework ::Credential . new ( public : username , private : pass_from_file , realm : realm , private_type : private_type ( pass_from_file ) )
350
310
end
351
- user_fd . seek ( 0 )
352
311
end
353
312
end
354
313
end
355
314
356
- if userpass_file . present?
357
- File . open ( userpass_file , 'r:binary' ) do |userpass_fd |
358
- userpass_fd . each_line do |line |
359
- user , pass = line . split ( " " , 2 )
360
- if pass . blank?
361
- pass = ''
362
- else
363
- pass . chomp!
364
- end
365
- yield Metasploit ::Framework ::Credential . new ( public : user , private : pass , realm : realm )
366
- end
367
- end
315
+ each_user_pass_from_userpass_file do |user , pass |
316
+ yield Metasploit ::Framework ::Credential . new ( public : user , private : pass , realm : realm )
368
317
end
369
318
370
319
additional_privates . each do |add_private |
371
- if username . present?
320
+ each_username do | username |
372
321
yield Metasploit ::Framework ::Credential . new ( public : username , private : add_private , realm : realm , private_type : private_type ( add_private ) )
373
322
end
374
- user_fd . each_line do |user_from_file |
375
- user_from_file . chomp!
376
- yield Metasploit ::Framework ::Credential . new ( public : user_from_file , private : add_private , realm : realm , private_type : private_type ( add_private ) )
377
- end
378
- user_fd . seek ( 0 )
379
323
end
324
+ end
380
325
381
- additional_publics . each do |add_public |
382
- if password . present?
383
- yield Metasploit ::Framework ::Credential . new ( public : add_public , private : password , realm : realm , private_type : private_type ( password ) )
384
- end
385
- if user_as_pass
386
- yield Metasploit ::Framework ::Credential . new ( public : add_public , private : user_from_file , realm : realm , private_type : :password )
387
- end
388
- if blank_passwords
389
- yield Metasploit ::Framework ::Credential . new ( public : add_public , private : "" , realm : realm , private_type : :password )
390
- end
391
- if nil_passwords
392
- yield Metasploit ::Framework ::Credential . new ( public : add_public , private : nil , realm : realm , private_type : :password )
393
- end
394
- if user_fd
326
+ # Iterates over all possible usernames
327
+ def each_username
328
+ if username . present?
329
+ yield username
330
+ end
331
+
332
+ if user_file . present?
333
+ File . open ( user_file , 'r:binary' ) do |user_fd |
395
334
user_fd . each_line do |user_from_file |
396
335
user_from_file . chomp!
397
- yield Metasploit :: Framework :: Credential . new ( public : add_public , private : user_from_file , realm : realm , private_type : private_type ( user_from_file ) )
336
+ yield user_from_file
398
337
end
399
338
user_fd . seek ( 0 )
400
339
end
401
- additional_privates . each do |add_private |
402
- yield Metasploit ::Framework ::Credential . new ( public : add_public , private : add_private , realm : realm , private_type : private_type ( add_private ) )
403
- end
404
340
end
405
- ensure
406
- user_fd . close if user_fd && !user_fd . closed?
341
+
342
+ additional_publics . each do |add_public |
343
+ yield add_public
344
+ end
407
345
end
408
346
409
347
# When password spraying is not enabled, do first usernames then passwords
@@ -418,108 +356,79 @@ def each_unfiltered_password_first
418
356
# @yieldparam credential [Metasploit::Framework::Credential]
419
357
# @return [void]
420
358
def each_unfiltered_username_first
421
- if pass_file . present?
422
- pass_fd = File . open ( pass_file , 'r:binary' )
423
- end
424
-
425
- prepended_creds . each { |c | yield c }
426
-
427
- if anonymous_login
428
- yield Metasploit ::Framework ::Credential . new ( public : '' , private : '' , realm : realm , private_type : :password )
429
- end
430
-
431
359
if username . present?
432
- if nil_passwords
433
- yield Metasploit ::Framework ::Credential . new ( public : username , private : nil , realm : realm , private_type : :password )
434
- end
435
- if password . present?
436
- yield Metasploit ::Framework ::Credential . new ( public : username , private : password , realm : realm , private_type : private_type ( password ) )
437
- end
438
- if user_as_pass
439
- yield Metasploit ::Framework ::Credential . new ( public : username , private : username , realm : realm , private_type : :password )
440
- end
441
- if blank_passwords
442
- yield Metasploit ::Framework ::Credential . new ( public : username , private : "" , realm : realm , private_type : :password )
443
- end
444
- if pass_fd
445
- pass_fd . each_line do |pass_from_file |
446
- pass_from_file . chomp!
447
- yield Metasploit ::Framework ::Credential . new ( public : username , private : pass_from_file , realm : realm , private_type : private_type ( pass_from_file ) )
448
- end
449
- pass_fd . seek ( 0 )
450
- end
451
- additional_privates . each do |add_private |
452
- yield Metasploit ::Framework ::Credential . new ( public : username , private : add_private , realm : realm , private_type : private_type ( add_private ) )
360
+ each_password ( username ) do |password , private_type |
361
+ yield Metasploit ::Framework ::Credential . new ( public : username , private : password , realm : realm , private_type : private_type )
453
362
end
454
363
end
455
364
456
365
if user_file . present?
457
366
File . open ( user_file , 'r:binary' ) do |user_fd |
458
367
user_fd . each_line do |user_from_file |
459
368
user_from_file . chomp!
460
- if nil_passwords
461
- yield Metasploit ::Framework ::Credential . new ( public : user_from_file , private : nil , realm : realm , private_type : :password )
462
- end
463
- if password . present?
464
- yield Metasploit ::Framework ::Credential . new ( public : user_from_file , private : password , realm : realm , private_type : private_type ( password ) )
465
- end
466
- if user_as_pass
467
- yield Metasploit ::Framework ::Credential . new ( public : user_from_file , private : user_from_file , realm : realm , private_type : :password )
468
- end
469
- if blank_passwords
470
- yield Metasploit ::Framework ::Credential . new ( public : user_from_file , private : "" , realm : realm , private_type : :password )
471
- end
472
- if pass_fd
473
- pass_fd . each_line do |pass_from_file |
474
- pass_from_file . chomp!
475
- yield Metasploit ::Framework ::Credential . new ( public : user_from_file , private : pass_from_file , realm : realm , private_type : private_type ( pass_from_file ) )
476
- end
477
- pass_fd . seek ( 0 )
478
- end
479
- additional_privates . each do |add_private |
480
- yield Metasploit ::Framework ::Credential . new ( public : user_from_file , private : add_private , realm : realm , private_type : private_type ( add_private ) )
369
+ each_password ( user_from_file ) do |password , private_type |
370
+ yield Metasploit ::Framework ::Credential . new ( public : user_from_file , private : password , realm : realm , private_type : private_type )
481
371
end
482
372
end
483
373
end
484
374
end
485
375
486
- if userpass_file . present?
487
- File . open ( userpass_file , 'r:binary' ) do |userpass_fd |
488
- userpass_fd . each_line do |line |
489
- user , pass = line . split ( " " , 2 )
490
- if pass . blank?
491
- pass = ''
492
- else
493
- pass . chomp!
494
- end
495
- yield Metasploit ::Framework ::Credential . new ( public : user , private : pass , realm : realm )
496
- end
497
- end
376
+ each_user_pass_from_userpass_file do |user , pass |
377
+ yield Metasploit ::Framework ::Credential . new ( public : user , private : pass , realm : realm )
498
378
end
499
379
500
380
additional_publics . each do |add_public |
501
- if password . present?
502
- yield Metasploit ::Framework ::Credential . new ( public : add_public , private : password , realm : realm , private_type : private_type ( password ) )
381
+ each_password ( add_public ) do | password , private_type |
382
+ yield Metasploit ::Framework ::Credential . new ( public : add_public , private : password , realm : realm , private_type : private_type )
503
383
end
504
- if user_as_pass
505
- yield Metasploit ::Framework ::Credential . new ( public : add_public , private : user_from_file , realm : realm , private_type : :password )
506
- end
507
- if blank_passwords
508
- yield Metasploit ::Framework ::Credential . new ( public : add_public , private : "" , realm : realm , private_type : :password )
509
- end
510
- if pass_fd
384
+ end
385
+ end
386
+
387
+ # Iterates over all possible passwords
388
+ def each_password ( user )
389
+ if nil_passwords
390
+ yield [ nil , :password ]
391
+ end
392
+
393
+ if password . present?
394
+ yield [ password , private_type ( password ) ]
395
+ end
396
+
397
+ if user_as_pass
398
+ yield [ user , :password ]
399
+ end
400
+
401
+ if blank_passwords
402
+ yield [ "" , :password ]
403
+ end
404
+
405
+ if pass_file
406
+ File . open ( pass_file , 'r:binary' ) do |pass_fd |
511
407
pass_fd . each_line do |pass_from_file |
512
408
pass_from_file . chomp!
513
- yield Metasploit :: Framework :: Credential . new ( public : add_public , private : pass_from_file , realm : realm , private_type : private_type ( pass_from_file ) )
409
+ yield [ pass_from_file , private_type ( pass_from_file ) ]
514
410
end
515
411
pass_fd . seek ( 0 )
516
412
end
517
- additional_privates . each do |add_private |
518
- yield Metasploit ::Framework ::Credential . new ( public : add_public , private : add_private , realm : realm , private_type : private_type ( add_private ) )
413
+ end
414
+
415
+ additional_privates . each do |add_private |
416
+ yield [ add_private , private_type ( add_private ) ]
417
+ end
418
+ end
419
+
420
+ # Iterates on userpass file if present
421
+ def each_user_pass_from_userpass_file
422
+ return unless userpass_file . present?
423
+
424
+ File . open ( userpass_file , 'r:binary' ) do |userpass_fd |
425
+ userpass_fd . each_line do |line |
426
+ user , pass = line . split ( " " , 2 )
427
+ pass = pass . blank? ? '' : pass . chomp!
428
+
429
+ yield [ user , pass ]
519
430
end
520
431
end
521
- ensure
522
- pass_fd . close if pass_fd && !pass_fd . closed?
523
432
end
524
433
525
434
# Returns true when #each will have no results to iterate
@@ -533,14 +442,14 @@ def empty?
533
442
#
534
443
# @return [Boolean]
535
444
def has_users?
536
- username . present? || user_file . present? || userpass_file . present? || !additional_publics . empty? || !! ignore_public
445
+ username . present? || user_file . present? || userpass_file . present? || !additional_publics . empty?
537
446
end
538
447
539
448
# Returns true when there are any private values set
540
449
#
541
450
# @return [Boolean]
542
451
def has_privates?
543
- super || userpass_file . present? || user_as_pass || !! ignore_private
452
+ super || userpass_file . present? || user_as_pass
544
453
end
545
454
546
455
end
0 commit comments