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单例设计模式实现解析

python单例设计模式实现解析

这篇文章主要介绍了python单例设计模式实现解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 所谓单例,就是让类创建对象的时候,在...

解决python中导入win32com.client出错的问题

准备写一个操作Excel脚本却在导入包的时候出现了一个小问题 导入包 from Tkinter import Tk from time import sleep, ctime fro...

Python linecache.getline()读取文件中特定一行的脚本

Python linecache.getline()读取文件中特定一行的脚本

比如: ˂!-- Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHig...

Python简单操作sqlite3的方法示例

本文实例讲述了Python简单操作sqlite3的方法。分享给大家供大家参考,具体如下: import sqlite3 def Test1(): #con =sqlite3.co...

Python创建xml的方法

本文实例讲述了Python创建xml的方法。分享给大家供大家参考。具体实现方法如下: from xml.dom.minidom import Document class write...