-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathbeaconlogtracker.py
More file actions
executable file
·199 lines (155 loc) · 6.3 KB
/
beaconlogtracker.py
File metadata and controls
executable file
·199 lines (155 loc) · 6.3 KB
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
#!/usr/local/bin/python3
from sleep_python_bridge.striker import CSConnector
from argparse import ArgumentParser
from pprint import pp, pprint
import time
import signal
from pathlib import Path
import json
# Initialize lists
beaconlogresult = [] # raw data from CS
beaconLogs = [] # Cleaned list to feed final JSON
# JSON file
datafile = "output/html/data/beaconlogs.json"
####################
## FUNCTIONS
def handler(signum, frame):
exit(1)
def parseArguments():
parser = ArgumentParser()
parser.add_argument('host', help='The teamserver host.')
parser.add_argument('port', help='The teamserver port.')
parser.add_argument('username', help='The desired username.')
parser.add_argument('password', help='The teamserver password.')
parser.add_argument('path', help="Directory to CobaltStrike")
args = parser.parse_args()
return args
def main(args):
signal.signal(signal.SIGINT, handler)
cs_host = args.host
cs_port = args.port
cs_user = args.username
cs_pass = args.password
cs_directory = args.path
sleeptime = 30
####################
## Connect to server
cs = CSConnector(
cs_host=cs_host,
cs_user=cs_user,
cs_pass=cs_pass,
cs_directory=cs_directory)
while(1):
print(f"[*] Connecting to teamserver: {cs_host}")
try:
cs.connectTeamserver()
break
except:
print(f"[!] Unable to connect to the teamserver, is it running? Waiting {sleeptime} seconds to try again.")
time.sleep(sleeptime)
continue
while(1):
print("[Beacon Log Tracker] Getting beacon logs from teamserver...")
cs.logToEventLog("[Beacon Log Tracker] Getting beacon logs from teamserver",event_type="external")
beaconlogresult = cs.get_beaconlog()
####################
## Process Logs
cs.logToEventLog("[Beacon Log Tracker] Processing logs",event_type="external")
# JSON field reference: type, beacon_id, user, command, result, timestamp
if beaconlogresult is None:
print(f"[!] No logs yet. Waiting {sleeptime} seconds for a beacon to check in.")
time.sleep(sleeptime)
continue
for log in beaconlogresult:
# job types
beacon_checkin_types = ["beacon_checkin"]
beacon_input_types = ["beacon_input"]
beacon_output_types = ["beacon_tasked",
"beacon_output",
"beacon_output_alt",
"beacon_output_ls",
"beacon_output_ps",
"beacon_output_jobs"
]
beacon_error_types = ["beacon_error"]
# initialize a dict record
logDict = {}
logType = log[0]
# Checkins
if logType in beacon_checkin_types:
logDict["type"] = str(log[0])
logDict["beacon_id"] = str(log[1])
logDict["user"] = ""
logDict["command"] = ""
logDict["result"] = str(log[2])
logDict["timestamp"] = str(log[3])
# Inputs
elif logType in beacon_input_types:
logDict["type"] = str(log[0])
logDict["beacon_id"] = str(log[1])
logDict["user"] = str(log[2])
logDict["command"] = str(log[3])
logDict["result"] = ""
logDict["timestamp"] = str(log[4])
# Outputs
elif logType in beacon_output_types:
logDict["type"] = str(log[0])
logDict["beacon_id"] = str(log[1])
logDict["user"] = ""
logDict["command"] = ""
logDict["result"] = str(log[2])
logDict["timestamp"] = str(log[3])
# Beacon Errors
elif logType in beacon_error_types:
logDict["type"] = str(log[0])
logDict["beacon_id"] = str(log[1])
logDict["user"] = ""
logDict["command"] = ""
logDict["result"] = str(log[2])
logDict["timestamp"] = str(log[3])
else:
print(f"Unknown log type: {logType}")
print(log)
beaconLogs.append(logDict)
####################
## Read log file
print(f"[Beacon Log Tracker] Log count: {len(beaconLogs)}")
path = Path(datafile)
# Load existing data file
if path.is_file():
print("[*] Found log file")
f = open(datafile)
fileLogs = json.loads(f.read())
f.close()
currentLogCount = len(beaconlogresult)
fileLogCount = len(fileLogs["data"])
print(f"[Beacon Log Tracker] Current Log Count : {currentLogCount}")
print(f"[Beacon Log Tracker] Current Log File Count: {fileLogCount}")
cs.logToEventLog(f"[Beacon Log Tracker] Log count since teamserver started: {currentLogCount}",event_type="external")
cs.logToEventLog(f"[Beacon Log Tracker] Log count saved to JSON : {fileLogCount}",event_type="external")
# Check for missing entrys in the beaconlogs.json file from the current log data
updatedBeaconLog = fileLogs["data"]
for log in beaconLogs:
if log in fileLogs["data"]:
pass
else:
updatedBeaconLog.append(log)
# Update beaconlogs.json with new data
f = open(datafile,"w+")
f.write(json.dumps({"data":updatedBeaconLog}))
f.close()
else:
# Create data file it does not exist and load current data
print("[!] Missing log file. Creating...")
f = open(datafile,"w+")
logs = {"data":beaconLogs}
f.write(json.dumps(logs))
f.close()
print(f"[*] Wait {sleeptime} ...")
time.sleep(sleeptime)
if __name__ == "__main__":
print("------------------")
print("Beacon Log Tracker")
print("------------------")
args = parseArguments()
main(args)