复制代码 代码如下:
#coding:gbk
import os,sys
import re
findtype=['.php','.inc'] #要检查的文件后缀类型
#要检查的关键字正则表达式和日志中的描述,是一个二维数组。
keywords=[ ["eval\(\$\_POST","发现PHP一句话木马!"],\
["(system|shell_exec|exec|popen)","发现PHP命令执行函数!"]\
]
writelog = open('log.txt', 'w+')
def checkfile(filename):
fp=open(filename)
content = fp.read()
for keyword in keywords:
if re.search(keyword[0],content,re.I):
log="%s:%s" % (filename,keyword[1])
#print log
print >>writelog,log
fp.close()
def checkdir(dirname):
try:
ls=os.listdir(dirname)
except:
print 'access deny'
else:
for l in ls:
temp=os.path.join(dirname,l)
if(os.path.isdir(temp)):
checkdir(temp)
else:
ext = temp[temp.rindex('.'):]
if ext in findtype:
checkfile(temp)
if __name__=="__main__":
print "PHP webshell check for Python!"
print "By:Neeao"
print "http://Neeao.com"
if len(sys.argv) print "%s C:\\" % sys.argv[0]
else:
print "Check start!"
dirs=sys.argv[1:]
#print dirs[0]
if os.path.exists(dirs[0]):
checkdir(dirs[0])
else:
print "Dir:'%s' not exists!" % dirs[0]
print "Check finsh!"
writelog.close()