PHP webshell检查工具 python实现代码

yipeiwu_com5年前Python基础
1.使用方法:find.py 目录名称
2. 主要是采用python正则表达式来匹配的,可以在keywords中添加自己定义的正则,格式:
["eval\(\$\_POST","发现PHP一句话木马!"] #前面为正则,后面为对这个正则的描述,会在日志中显示。
3.修改下文件后缀和关键字的正则表达式就可以成为其他语言的webshell检查工具了,^_^。
4.开发环境是windows xp+ActivePython 2.6.2.2,家里电脑没有Linux环境,懒得装虚拟机了,明天到公司Linux虚拟机测试下。
5.目前只是一个框架,随后慢慢完善。
复制代码 代码如下:

#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) < 2:
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()

相关文章

全面理解Python中self的用法

刚开始学习Python的类写法的时候觉得很是麻烦,为什么定义时需要而调用时又不需要,为什么不能内部简化从而减少我们敲击键盘的次数?你看完这篇文章后就会明白所有的疑问。 self代表类的实...

Python实现HTTP协议下的文件下载方法总结

本文介绍了几种常用的python下载文件的方法,具体使用到了htttplib2,urllib等包,希望对大家有帮忙。 1.简单文件下载 使用htttplib2,具体代码如下: h =...

Python3实现将文件树中所有文件和子目录归档到tar压缩文件的方法

本文实例讲述了Python3实现将文件树中所有文件和子目录归档到tar压缩文件的方法。分享给大家供大家参考。具体实现方法如下: # 这里将一个文件树中的所有文件和子目录归档到一个ta...

解决PyCharm同目录下导入模块会报错的问题

在PyCharm2017中同目录下import其他模块,会出现No model named ...的报错,但实际可以运行 这是因为PyCharm不会将当前文件目录自动加入source_p...

Python封装shell命令实例分析

本文实例讲述了Python封装shell命令的方法。分享给大家供大家参考。具体实现方法如下: # -*- coding: utf-8 -*- import os import sub...