由于n种原因,一个分析入侵日志的任务落在了我身上,1g的日志,怎么去快速分析呢??刺总说可以搞个脚本解析入库,再到数据库分析。。。算了,那就蛋疼了,直接码个脚本把有问题的日志拿出来分析吧。于是就有了这个小脚本。至于怎么用就要看你自己了,哈哈,比如查到sql注入语句,然后看到ip,就可以改下脚本,用ip为特征取出日志,分析入侵过程。速度很快哦,我那破机器,跑1g日志文件也就几秒钟的啦。
在工作中写程序完成任务是很快乐的事,也很有意思。哈哈
使用参数:seay.py e:/1.log
#coding = utf8
#filename = seay.py
import os
import sys
#特征,可以随意改,两块五一次
_tezheng = {'union','select','file_put_contents'}
def checkfile(_path):
_f = open(_path,r)
_all_line = _f.readlines()
_f.close()
_count_line =0
_len_line = len(_all_line)
_ex_str = ''
print('read over --')
while _count_line<_len_line:
_str = _all_line[_count_line]
for _tz_str in _tezheng:
if _tz_str in _str: #可以加and条件,这个贵一点,5毛一次
_ex_str+=_tz_str+_str+'\r\n'
_count_line+=1
_f1 = open(_path+'.seay.txt',w)
_f1.write(_ex_str)
_f1.close()
print 'find over--'
if len(sys.argv)==2:
_file = sys.argv[1]
if os.path.lexists(_file):
checkfile(_file)
else:
print('file does not exist!')
else:
print 'parameter error'
print sys.argv[0]+' filepath'
最终生成一个文件为:原文件名.seay.txt在同目录下,格式为匹配的特征+日志