Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/lib/msf/core/exploit/brute.rb
Views: 11784
# -*- coding: binary -*-1module Msf23###4#5# This modules provides a target-aware brute-forcing wrapper. It implements6# the exploit method and calls exploit_brute with target supplied information.7# If the selected target is not a bruteforce target, then single_exploit is8# called.9#10###11module Exploit::Brute1213#14# Initializes an instance of an exploit module that supports brute force15# targets.16#17def initialize(info = {})18super1920#21# Register BruteWait and BruteStep as two advanced options for this22# exploit even though not all targets may be brute force targets.23#24register_advanced_options(25[26OptInt.new('BruteWait', [ false, "Delay between brute force attempts" ]),27OptInt.new('BruteStep', [ false, "Step size between brute force attempts" ])28], Msf::Exploit::Brute)29end3031#32# Entry point for initiating an exploit. This module wrappers the exploit33# method and determines whether or not the selected target supports brute34# force. If it does, it does some special things and wraps the brute35# forcing logic.36#37def exploit38# Is the selected target a brute force target?39if (target.bruteforce?)40# The step direction is automatically calculated41direction = {}4243bf = target.bruteforce4445# Get the start and stop address hashes46start = bf.start_addresses ? bf.start_addresses.dup : {}47stop = bf.stop_addresses ? bf.stop_addresses.dup : {}48step = bf.step_size49delay = bf.delay5051# Enumerate each start address and try to figure out the direction52start.each_pair { |name, addr|53# If there's a stop address, figure out if it's above or below54# the start address55if (stop[name])56if (stop[name] < addr)57direction[name] = -158else59direction[name] = 160end61# If there's no stop address, infer the direction based on62# the default63else64direction[name] = bf.default_direction65end66}6768# Import start/stop address overrides from the datastore69import_from_datastore(start, 'Start')70import_from_datastore(stop, 'Stop')7172# User-defined brute wait?73if self.datastore['BruteWait'] and self.datastore['BruteWait'] > 074delay = self.datastore['BruteWait'].to_i75end7677# User-defined brute step?78if self.datastore['BruteStep'] and self.datastore['BruteStep'] > 079step = self.datastore['BruteStep'].to_i80end8182# Sane defaults83delay = 1 if delay.nil? or delay == 08485# Okay, we've got all this crap out of the way, let's actually brute86# force87stopped = []88curr = start.dup8990# Automatically determine the step size based off the nop sled length91if step == 092step = payload.nop_sled_size9394if step == 095raise Msf::OptionValidateError.new(96{97'BruteStep' => 'The step size for this exploit is invalid'98}99)100end101end102103# Keep going until we run out of options104while (curr.length != stopped.length)105106# Stop brute forcing once a session is found107break if session_created?108109# Fire off an exploit attempt with the supplied addresses110brute_exploit(curr)111112# Give it time before we try again113brute_wait(delay)114115# Scan each current key, increasing it or decreasing it by the116# step size according to its direction117curr.each_key { |k|118119# Has movement been stopped on this address? If so, skip it.120next if (stopped.include?(k))121122# Calculate the next address before we move it to see if123# we're going to go over124next_addr = step * direction[k]125126# If this item has hit a stop address, add it to the stopped127# hash and move it no further128if (stop[k])129if ((direction[k] == 1 and curr[k] + next_addr >= stop[k]) or130(direction[k] == -1 and curr[k] + next_addr < stop[k]))131stopped << k132next133end134end135136# If it's not time to stop, move it137curr[k] += next_addr138}139end140else141single_exploit142end143end144145#146# This routine is called once per brute force iteration. The addresses147# parameter is a hash of addresses that are incremented each iteration and148# are derived from the target's bruteforce information or the module's149# datastore in case they are being overridden.150#151def brute_exploit(addrs)152end153154#155# Call if the target is not a brute force target.156#157def single_exploit158end159160#161# Waits for the provide delay.162#163def brute_wait(delay)164sleep(delay)165end166167protected168169#170# Imports information into the supplied hash from the datastore.171# This is a way of allowing the user to override values for a172# specific brute force target by name without them actually173# being conveyed in the options list. This is a bit of a change174# from 2.x, but 2.x didn't have per-target brute force175# addresses, which I think is more valuable.176#177def import_from_datastore(hash, prefix = '')178hash.each_key { |k|179if (self.datastore[prefix + k])180hash[k] = self.datastore[prefix + k]181end182}183end184185end186187end188189190