PHP webshell检查工具 python实现代码

yipeiwu_com6年前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中拆分字符串。 .split()方法 在Python中,字符串表示为不可...

python使用Flask框架获取用户IP地址的方法

本文实例讲述了python使用Flask框架获取用户IP地址的方法。分享给大家供大家参考。具体如下: 下面的代码包含了html页面和python代码,非常详细,如果你正使用Flask,也...

python画微信表情符的实例代码

python画微信表情符的实例代码

#@project = facepalm #@file = main #@author = Maoliang Ran #@create_time = 2018/8/28 22:...

在SQLite-Python中实现返回、查询中文字段的方法

博主在这个问题上卡了挺久的,贴出来解决方法帮助需要的朋友,直接上代码(测试环境:win10+Python2.7): # coding=utf-8 import sqlite3...

Python读取mat文件,并保存为pickle格式的方法

这两天在搞Theano,要把mat文件转成pickle格式载入Python。 Matlab是把一维数组当做n*1的矩阵的,但Numpy里还是有vector和matrix的区别,Thean...