2012-06-29 05:18:28 +00:00
|
|
|
# -*- coding: binary -*-
|
2012-07-17 21:48:06 +00:00
|
|
|
|
|
|
|
require 'msf/core/post/file'
|
|
|
|
|
2011-01-12 00:10:32 +00:00
|
|
|
module Msf
|
|
|
|
class Post
|
|
|
|
|
|
|
|
module Common
|
|
|
|
|
2012-06-08 04:24:59 +00:00
|
|
|
#
|
|
|
|
# Executes +cmd+ on the remote system
|
|
|
|
#
|
|
|
|
# On Windows meterpreter, this will go through CreateProcess as the
|
|
|
|
# "commandLine" parameter. This means it will follow the same rules as
|
|
|
|
# Windows' path disambiguation. For example, if you were to call this method
|
|
|
|
# thusly:
|
|
|
|
#
|
|
|
|
# cmd_exec("c:\\program files\\sub dir\\program name")
|
|
|
|
#
|
|
|
|
# Windows would look for these executables, in this order, passing the rest
|
|
|
|
# of the line as arguments:
|
|
|
|
#
|
|
|
|
# c:\program.exe
|
|
|
|
# c:\program files\sub.exe
|
|
|
|
# c:\program files\sub dir\program.exe
|
|
|
|
# c:\program files\sub dir\program name.exe
|
|
|
|
#
|
|
|
|
# On POSIX meterpreter, if +args+ is set or if +cmd+ contains shell
|
|
|
|
# metacharacters, the server will run the whole thing in /bin/sh. Otherwise,
|
|
|
|
# (cmd is a single path and there are no arguments), it will execve the given
|
|
|
|
# executable.
|
|
|
|
#
|
|
|
|
# On Java, it is passed through Runtime.getRuntime().exec(String) and PHP
|
|
|
|
# uses proc_open() both of which have similar semantics to POSIX.
|
|
|
|
#
|
|
|
|
# On shell sessions, this passes +cmd+ directly the session's
|
|
|
|
# +shell_command_token+ method.
|
|
|
|
#
|
|
|
|
# Returns a (possibly multi-line) String.
|
|
|
|
#
|
|
|
|
def cmd_exec(cmd, args=nil, time_out=15)
|
2011-05-30 22:11:43 +00:00
|
|
|
case session.type
|
|
|
|
when /meterpreter/
|
2012-06-08 04:24:59 +00:00
|
|
|
#
|
|
|
|
# The meterpreter API requires arguments to come seperately from the
|
|
|
|
# executable path. This has no effect on Windows where the two are just
|
|
|
|
# blithely concatenated and passed to CreateProcess or its brethren. On
|
|
|
|
# POSIX, this allows the server to execve just the executable when a
|
|
|
|
# shell is not needed. Determining when a shell is not needed is not
|
|
|
|
# always easy, so it assumes anything with arguments needs to go through
|
|
|
|
# /bin/sh.
|
|
|
|
#
|
|
|
|
# This problem was originally solved by using Shellwords.shellwords but
|
|
|
|
# unfortunately, it is retarded. When a backslash occurs inside double
|
|
|
|
# quotes (as is often the case with Windows commands) it inexplicably
|
|
|
|
# removes them. So. Shellwords is out.
|
|
|
|
#
|
|
|
|
# By setting +args+ to an empty string, we can get POSIX to send it
|
|
|
|
# through /bin/sh, solving all the pesky parsing troubles, without
|
|
|
|
# affecting Windows.
|
|
|
|
#
|
|
|
|
if args.nil? and cmd =~ /[^a-zA-Z0-9\/._-]/
|
|
|
|
args = ""
|
2011-07-05 17:17:27 +00:00
|
|
|
end
|
2012-06-08 04:24:59 +00:00
|
|
|
|
2011-05-30 22:11:43 +00:00
|
|
|
session.response_timeout = time_out
|
2012-06-08 04:24:59 +00:00
|
|
|
process = session.sys.process.execute(cmd, args, {'Hidden' => true, 'Channelized' => true})
|
2011-05-30 22:11:43 +00:00
|
|
|
o = ""
|
2011-07-05 17:17:27 +00:00
|
|
|
while (d = process.channel.read)
|
2011-05-30 22:11:43 +00:00
|
|
|
break if d == ""
|
2011-07-01 23:46:54 +00:00
|
|
|
o << d
|
2011-05-30 22:11:43 +00:00
|
|
|
end
|
2011-07-05 17:17:27 +00:00
|
|
|
process.channel.close
|
|
|
|
process.close
|
2011-05-30 22:11:43 +00:00
|
|
|
when /shell/
|
2012-06-08 04:24:59 +00:00
|
|
|
o = session.shell_command_token("#{cmd} #{args}", time_out)
|
2011-07-01 23:46:54 +00:00
|
|
|
o.chomp! if o
|
2011-01-12 00:10:32 +00:00
|
|
|
end
|
2011-07-01 23:46:54 +00:00
|
|
|
return "" if o.nil?
|
2011-01-12 00:10:32 +00:00
|
|
|
return o
|
|
|
|
end
|
|
|
|
|
2012-01-26 19:02:39 +00:00
|
|
|
def report_vm(vm)
|
|
|
|
return unless session
|
|
|
|
return unless vm
|
2012-02-26 08:09:44 +00:00
|
|
|
vm_normal = vm.to_s.strip
|
2012-01-26 19:02:39 +00:00
|
|
|
return if vm_normal.empty?
|
|
|
|
vm_data = {
|
|
|
|
:host => session.target_host,
|
|
|
|
:virtual_host => vm_normal
|
|
|
|
}
|
|
|
|
report_host(vm_data)
|
|
|
|
end
|
|
|
|
|
2011-01-12 00:10:32 +00:00
|
|
|
end
|
|
|
|
end
|
2011-07-01 23:46:54 +00:00
|
|
|
end
|