Skip to content

Commit 399b3d2

Browse files
committed
Land rapid7#5629, moar cmd_exec refactoring
2 parents d6261a5 + 02cd2a9 commit 399b3d2

File tree

7 files changed

+14
-16
lines changed

7 files changed

+14
-16
lines changed

lib/msf/core/post/common.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,11 @@ def cmd_exec(cmd, args=nil, time_out=15)
122122

123123
process.close
124124
when /shell/
125-
o = session.shell_command_token("#{cmd} #{args}", time_out)
125+
if args.nil? || args.empty?
126+
o = session.shell_command_token("#{cmd}", time_out)
127+
else
128+
o = session.shell_command_token("#{cmd} #{args}", time_out)
129+
end
126130
o.chomp! if o
127131
end
128132
return "" if o.nil?

lib/msf/core/post/windows/registry.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ def shell_registry_cmd(suffix, view = REGISTRY_VIEW_NATIVE)
170170
elsif view == REGISTRY_VIEW_64_BIT
171171
cmd += " /reg:64"
172172
end
173-
session.shell_command_token_win32("#{cmd} #{suffix}")
173+
cmd_exec("#{cmd} #{suffix}")
174174
end
175175

176176
def shell_registry_cmd_result(suffix, view = REGISTRY_VIEW_NATIVE)

modules/post/multi/gather/multi_command.rb

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def initialize(info={})
1616
'License' => MSF_LICENSE,
1717
'Author' => [ 'Carlos Perez <carlos_perez[at]darkoperator.com>'],
1818
'Platform' => %w{ bsd linux osx unix win },
19-
'SessionTypes' => [ 'meterpreter','shell' ]
19+
'SessionTypes' => ['meterpreter']
2020
))
2121
register_options(
2222
[
@@ -27,7 +27,6 @@ def initialize(info={})
2727

2828
# Run Method for when run command is issued
2929
def run
30-
session_type = session.type
3130
print_status("Running module against #{sysinfo['Computer']}")
3231
if not ::File.exists?(datastore['RESOURCE'])
3332
raise "Resource File does not exists!"
@@ -41,11 +40,7 @@ def run
4140
tmpout << " Output of #{cmd}\n"
4241
tmpout << "*****************************************\n"
4342
print_status "Running command #{cmd.chomp}"
44-
if session_type =~ /meterpreter/
45-
tmpout << cmd_exec(cmd.chomp)
46-
elsif session_type =~ /shell/
47-
tmpout << session.shell_command_token(cmd.chomp).chomp
48-
end
43+
tmpout << cmd_exec(cmd.chomp)
4944
vprint_status tmpout
5045
command_log = store_loot("host.command", "text/plain", session,tmpout ,
5146
"#{cmd.gsub(/\.|\/|\s/,"_")}.txt", "Command Output \'#{cmd.chomp}\'")

modules/post/multi/manage/shell_to_meterpreter.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,12 +196,12 @@ def transmit_payload(exe)
196196
sent = 0
197197
aborted = false
198198
cmds.each { |cmd|
199-
ret = session.shell_command_token(cmd)
199+
ret = cmd_exec(cmd)
200200
if !ret
201201
aborted = true
202202
else
203203
ret.strip!
204-
aborted = true if !ret.empty?
204+
aborted = true if !ret.empty? && ret !~ /The process tried to write to a nonexistent pipe./
205205
end
206206
if aborted
207207
print_error('Error: Unable to execute the following command: ' + cmd.inspect)

modules/post/multi/manage/system_session.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def run
5959

6060
if not cmd.empty?
6161
print_status("Executing reverse tcp shel to #{lhost} on port #{lport}")
62-
session.shell_command_token("(#{cmd} &)")
62+
cmd_exec("(#{cmd} &)")
6363
end
6464
end
6565

modules/post/windows/gather/enum_computers.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ def initialize(info={})
2929
def run
3030
print_status("Running module against #{sysinfo['Computer']}") if not sysinfo.nil?
3131
domain = get_domain()
32-
3332
if not domain.empty?
3433
hostname_list = get_domain_computers()
3534
list_computers(domain, hostname_list)
@@ -49,7 +48,7 @@ def gethost(hostname)
4948
def get_domain_computers()
5049
computer_list = []
5150
devisor = "-------------------------------------------------------------------------------\r\n"
52-
raw_list = client.shell_command_token("net view").split(devisor)[1]
51+
raw_list = cmd_exec('net view').split(devisor)[1]
5352
if raw_list =~ /The command completed successfully/
5453
raw_list.sub!(/The command completed successfully\./,'')
5554
raw_list.gsub!(/\\\\/,'')

modules/post/windows/gather/enum_domain_tokens.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ def run
5858
# List local group members
5959
def list_group_mem(group)
6060
devisor = "-------------------------------------------------------------------------------\r\n"
61-
raw_list = client.shell_command_token("net localgroup #{group}").split(devisor)[1]
61+
raw_list = cmd_exec("net localgroup #{group}").split(devisor)[1]
6262
account_list = raw_list.split("\r\n")
6363
account_list.delete("The command completed successfully.")
6464
return account_list
@@ -68,7 +68,7 @@ def list_group_mem(group)
6868
def list_domain_group_mem(group)
6969
account_list = []
7070
devisor = "-------------------------------------------------------------------------------\r\n"
71-
raw_list = client.shell_command_token("net groups \"#{group}\" /domain").split(devisor)[1]
71+
raw_list = cmd_exec("net groups \"#{group}\" /domain").split(devisor)[1]
7272
raw_list.split(" ").each do |m|
7373
account_list << m
7474
end

0 commit comments

Comments
 (0)