-
Notifications
You must be signed in to change notification settings - Fork 0
/
zlog.py.v2.0
335 lines (274 loc) · 13.5 KB
/
zlog.py.v2.0
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
#!/usr/bin/python
# 2.0 // 2020-12-28
# Questions? Problems? Need more? ask Jackie
import os
import sys
import datetime
import subprocess
import argparse
global user_agent_str
######## GLOBAL VARS #######
saveplace = "/srv/"
logfile = "/log/access.log" # Default
linecount = 0
NC='\033[0m' # No Color
YELLOW='\033[0;33m'
RED='\033[0;31m'
GREEN='\033[0;32m'
HPINK ='\033[1;45m'
if sys.version_info[0] < 3:
raise Exception(RED + "Zlog now uses Python 3: python3 ./zlog.py" + NC)
sys.exit()
######### EXTRA ARGUMENT MAGIC AKA LETS DO OTHER THINGS ##########
parser = argparse.ArgumentParser()
parser.add_argument('-n', '--newfile', action='store_true',
help="allows you to examine log file besides the default")
parser.add_argument('-w', '--whois', action='store_true',
help="checks whois by IPs for common traffic sources")
parser.add_argument('-a', '--attacks', action='store_true',
help="checks for common attacks/scans")
args = parser.parse_args()
######## GLOBAL FUNCTIONS ######
# Check logfile exists and if it's large
def checklog(logfile,saveplace):
if os.path.exists(logfile):
log_size = os.path.getsize(logfile)
else:
print("Can not find log at: " + logfile)
sys.exit()
filter_tmp_log = False
if log_size >= 250000000:
filter_tmp_log = True
print("\nThe log is huge ({}M), filtering...".format(log_size / 1000000))
now = datetime.datetime.now()
today_str = "{}/{}/{}:".format(now.strftime("%d"), now.strftime("%b"), now.strftime("%Y"))
os.popen("cat {} | grep {} > {}zLog-filtered-tmp.log".format(logfile, today_str, saveplace))
logfile = "{}zLog-filtered-tmp.log".format(saveplace)
# Get Log start time
def getlogstart(file):
vhead = os.popen("head -n1 " + file + " | awk -F ' ' '{print $3}'").read().replace(" ", " ")
vhead = vhead.replace("[", "").replace("\n", "")
vtail = os.popen("tail -n1 " + file + " | awk -F ' ' '{print $3}'").read().replace(" ", " ")
vtail = vtail.replace("[", "").replace("\n", "")
print(HPINK + "Data range: {} -> {} (now).".format(vhead, vtail) + NC)
# Get Top <num> IPs
def gettopips(file,amount,saveplace):
if amount==100:
cmdget = "awk {'print $1'} " + file + " | sort | uniq -c | sort -n > " + saveplace + "zLog.ips.log"
else:
cmdget = "awk {'print $1'} " + file + " | sort | uniq -c | sort -n | tail -n" + str(amount) +" > " + saveplace + "zLog.ips.log"
os.system(cmdget)
cmdip = "awk {'print $2'} " + saveplace + "zLog.ips.log > " + saveplace + "zLog.tmp.log"
os.system(cmdip)
# Get Agent of thing
def getagent(thing,logfile):
global user_agent_str
user_agent_str = os.popen("grep -m1 " + thing + " " + logfile + " | awk '{$1=$2=$3=$4=$5=$6=$7=$8=$9=$10=\"\"; print $0}'").read()
user_agent_str = user_agent_str.replace('"', '').replace(" ", '').replace(";", '').replace("\n", '').replace("\t", '')
return user_agent_str
######### --newfile -n IF DIFF FILE CHOSEN ###########
if args.newfile:
logfile = input("Enter logfile path (Ex. " + YELLOW + "/log/access.log.1" + NC + "): ")
if str(logfile) == "":
logfile = "/log/access.log"
#past this point uses same code as default zlog
######## --whois -w IF WHOIS OPTION CHOSEN #########
if args.whois:
wholist = [] #declare list for whois strings
whototals = {} #declare dict for whois totals
touch = "touch " + saveplace + "zLog.whois.log"
logfile = input("Enter logfile path (Ex. " + YELLOW + "/log/access.log" + NC + "): ")
if str(logfile) == "":
logfile = "/log/access.log"
# CHECK LOGFILE
checklog(logfile,saveplace)
# GET TIME START OF LOG FILE
getlogstart(logfile)
# GET IP COUNTS
print("\n" + YELLOW + str("Grabbing IPs...") + "\033[1;m")
gettopips(logfile,100,saveplace)
# WHO ARE THESE JOKERS?
#print "{} - {} - {}".format("Count", "Address", "Whois")
with open(saveplace + "zLog.tmp.log") as f:
contents = f.readlines()
for line in reversed(contents):
line = line.replace("\n", "")
linecount = linecount + 1
# grab count
tmp_str = os.popen("grep -m1 " + line + " " + saveplace + "zLog.ips.log | awk -F ' ' '{print $1,$2}'").read().replace("\n", "")
count_str = tmp_str.split(" ")[0]
whois = os.popen("whois " + line + " | grep -E -i 'OrgName|StateProv|Country' | sort -u").read().replace(" ",'').replace("\n", "")
whois = whois.replace("Country", " ").replace("OrgName", " ").replace("StateProv", " ").replace("'", "").replace(",", "")
savewho = "echo " + count_str + ", " + line + ", '" + whois + "' >> " + saveplace + "zLog.whois.log"
os.system(savewho)
# save just whois to list
wholist.append(whois)
# adjust output according to line number
if linecount == 50:
break
#print "--------------------------------------------------------------------"
print(" ")
print(YELLOW + "WHOIS - HITS / LOCATION / " + NC)
# Total # of hits in file
# cat /srv/zLog.whois.log | awk {'print $1'} | awk '{ SUM += $1} END { print SUM }'
# Number of hits from US
# cat /srv/zLog.whois.log | grep :US | awk {'print $1'} | awk '{ SUM += $1} END { print SUM }'
# Check total hits for each whois
for w in wholist:
docount = os.popen("grep '" + str(w) + "' " + saveplace + "zLog.whois.log | awk '{ SUM += $1} END { print SUM }'").read().replace("\n", "")
#print("docount = " + docount + " " + w) #debug
# Add each whois to a dictionary with total if its not already recorded
if w not in whototals:
whototals[w] = docount
#print("added above to dict") #debug
#print dictionary sorted by values instead of key
sort_who = sorted(whototals.items(), key=lambda x: int(x[1]), reverse=True)
for i in sort_who:
print("{} |{}".format(i[1], i[0]))
######### --attacks -a IF ATTACK CHECK IS CHOSEN ##########
if args.attacks:
logfile = input("Enter logfile path (Ex. " + YELLOW + "/log/access.log" + NC + "): ")
if str(logfile) == "":
logfile = "/log/access.log"
print(" ")
print(YELLOW + "PATH / HITS" + NC)
# payment-informtation API hits declined (carding attack)
carding = os.popen("grep payment-information " + logfile + " | grep ' 400 ' | grep POST | wc -l").read().replace("\n", "")
print("1. payment-information declines (M2 carding attacks): " + YELLOW + carding + NC)
account = os.popen("grep ' /customer/account/create' " + logfile + " | grep POST | wc -l").read().replace("\n", "")
print("2. customer/account/create POSTS: " + YELLOW + account + NC)
newsletter = os.popen("grep newsletter " + logfile + " | grep POST | wc -l").read().replace("\n", "")
print("3. newsletter POSTS: " + YELLOW + newsletter + NC)
sqlinj = os.popen("grep -E 'SELECT|UNION|1=1|SLEEP' " + logfile + " | wc -l").read().replace("\n", "")
print("4. possible SQL injections: " + YELLOW + sqlinj + NC)
wp = os.popen("grep -E 'wp-admin|wp-login|xmlrpc.php' " + logfile + " | grep POST | wc -l").read().replace("\n", "")
print("5. possible wp brute force: " + YELLOW + wp + NC)
dirt = os.popen("grep '\.\./' " + logfile + " | wc -l").read().replace("\n", "")
print("6. directory traversal attempts: " + YELLOW + dirt + NC)
wishlist = os.popen("grep wishlist " + logfile + " | grep POST | wc -l").read().replace("\n", "")
print("7. wishlist POSTS: " + YELLOW + wishlist + NC)
scan = os.popen("grep -Ei '\.zip|\.gz|\.tar' " + logfile + " | wc -l").read().replace("\n", "")
print("8. random scans: " + YELLOW + scan + NC)
m1 = os.popen("grep -E 'downloader|magmi' " + logfile + " | grep POST | wc -l").read().replace("\n", "")
print("9. M1 downloader/magmi POSTS: " + YELLOW + m1 + NC)
account2 = os.popen("grep ' /customer/account/login' " + logfile + " | grep POST | wc -l").read().replace("\n", "")
print("10. customer/account/login POSTS: " + YELLOW + account2 + NC)
print(" ")
pick = input("Examine one of the above? If so, input #: ")
#print("pick = " + pick + " carding = " + carding) #debug
# If examime an attack, is chosen, lets get the details
if int(pick) == 1:
if str(carding) == "0":
sys.exit(RED + "Results were 0 for carding. Nothing to examine." + NC)
else:
examine = "grep payment-information " + logfile + " | grep ' 400 ' | grep POST > " + saveplace + "zLog.tmp.attacks"
elif int(pick) == 2:
if str(account) == "0":
sys.exit(RED + "Results were 0 for account/create. Nothing to examine." + NC)
else:
examine = "grep ' /customer/account/create' " + logfile + " | grep POST > " + saveplace + "zLog.tmp.attacks"
elif int(pick) == 3:
if str(newsletter) == "0":
sys.exit(RED + "Results were 0 for newsletter posts. Nothing to examine." + NC)
else:
examine = "grep newsletter " + logfile + " | grep POST > " + saveplace + "zLog.tmp.attacks"
elif int(pick) == 4:
if str(sqlinj) == "0":
sys.exit(RED + "Results were 0 for sql injections. Nothing to examine." + NC)
else:
examine = "grep -E 'SELECT|UNION|1=1|SLEEP' " + logfile + " > " + saveplace + "zLog.tmp.attacks"
elif int(pick) == 5:
if str(wp) == "0":
sys.exit(RED + "Results were 0 for wp attacks. Nothing to examine." + NC)
else:
examine = "grep -E 'wp-admin|wp-login|xmlrpc.php' " + logfile + " | grep POST > " + saveplace + "zLog.tmp.attacks"
elif int(pick) == 6:
if str(dirt) == "0":
sys.exit(RED + "Results were 0 for traversal attempts. Nothing to examine." + NC)
else:
examine = "grep '\.\./' " + logfile + " > " + saveplace + "zLog.tmp.attacks"
elif int(pick) == 7:
if str(wishlist) == "0":
sys.exit(RED + "Results were 0 for wishlist posts. Nothing to examine." + NC)
else:
examine = "grep wishlist " + logfile + " | grep POST > " + saveplace + "zLog.tmp.attacks"
elif int(pick) == 8:
if str(scan) == "0":
sys.exit(RED + "Results were 0 for scans. Nothing to examine." + NC)
else:
examine = "grep -Ei '\.zip|\.gz|\.tar' " + logfile + " > " + saveplace + "zLog.tmp.attacks"
elif int(pick) == 9:
if str(m1) == "0":
sys.exit(RED + "Results were 0 for download/magmi. Nothing to examine." + NC)
else:
examine = "grep -E 'downloader|magmi' " + logfile + " | grep POST > " + saveplace + "zLog.tmp.attacks"
elif int(pick) == 10:
if str(account2) == "0":
sys.exit(RED + "Results were 0 for account/login. Nothing to examine." + NC)
else:
examine = "grep ' /customer/account/login' " + logfile + " | grep POST > " + saveplace + "zLog.tmp.attacks"
# if user chooses no option or hits enter go backto prompt
else:
sys.exit("Nothing chosen. Later!")
# print 5 chosen attack examples
print("\n" + HPINK + str("5 example logs") + "\033[1;m")
os.system(examine)
stop = 0
with open(saveplace + "zLog.tmp.attacks") as a:
for line in a:
line = line.replace("\n", "")
print(line)
stop = stop + 1
if stop == 5:
break
# get IPs
print("\n" + HPINK + str("Top 25 IPs associated") + "\033[1;m")
gettopips(saveplace + "zLog.tmp.attacks",25,saveplace)
# GET AGENT OF EACH IP
print("{} - {} - {}".format("Count", "Address", "User Agent"))
#max_count_len = 0
with open(saveplace + "zLog.tmp.log") as f:
contents = f.readlines()
for line in reversed(contents):
line = line.replace("\n", "")
# grab count
tmp_str = os.popen("grep -m1 " + line + " " + saveplace + "zLog.ips.log | awk -F ' ' '{print $1,$2}'").read().replace("\n", "")
count_str = tmp_str.split(" ")[0]
getagent(line,logfile)
print("{} - {} - {}".format(count_str, line, user_agent_str))
####### IF NO ARGUMENTS USED, DO SAME OLD REGULAR LOG STATS #######
if not len(sys.argv) > 1 or args.newfile:
#CHECK LOGFILE
checklog(logfile,saveplace)
# GET TIME START OF LOG FILE
getlogstart(logfile)
# GET TOP 25 IPs
print("\n" + HPINK + str("Top 25 IPs") + "\033[1;m")
gettopips(logfile,25,saveplace)
# GET AGENT OF EACH IP
print("{} - {} - {}".format("Count", "Address", "User Agent"))
#max_count_len = 0
with open(saveplace + "zLog.tmp.log") as f:
contents = f.readlines()
for line in reversed(contents):
line = line.replace("\n", "")
# grab count
tmp_str = os.popen("grep -m1 " + line + " " + saveplace + "zLog.ips.log | awk -F ' ' '{print $1,$2}'").read().replace("\n", "")
count_str = tmp_str.split(" ")[0]
getagent(line,logfile)
print("{} - {} - {}".format(count_str, line, user_agent_str))
vapi = os.popen("grep api " + logfile + " | wc -l").read().replace("\n", "")
vdownloader = os.popen("grep downloader " + logfile + " | wc -l").read().replace("\n", "")
vsoap = os.popen("grep soap " + logfile + " | wc -l").read().replace("\n", "")
print("\nAPI mentions: {} - SOAP mentions: {} - Downloader mentions: {}\n".format(vapi, vsoap, vdownloader))
# GET TOP USER AGENTS QUICK CHECK - IGNORES DIFFERENCES BETWEEN STANDARD MOZILLA AGENT
vagents = os.popen("awk -F\'\"\' \'/GET/ {print $6}\' " + logfile + " | cut -d\' \' -f1 | sort | uniq -c | sort -rn | head -n10").read()
print(HPINK + str("Top 10 User Agents") + "\033[1;m")
print(vagents)
#GET TOP HIT URLs
vURLs = os.popen("awk {'print $6'} " + logfile + " | sort | uniq -c | sort -n | tail -n15").read()
print("\n" + HPINK + str("Top 15 Hit URLs") + NC)
print(vURLs)
# Clean up our mess
os.system("rm {}zLog*".format(saveplace))
os.system("rm ./zlog.py")