__import__
函数
我们都知道import
是导入模块的,但是其实import
实际上是使用builtin
函数import
来工作的。在一些程序中,我们可以动态去调用函数,如果我们知道模块的名称(字符串)的时候,我们可以很方便的使用动态调用
def getfunctionbyname(module_name, function_name):
module = __import__(module_name)
return getattr(module, function_name)
通过这段代码,我们就可以简单调用一个模块的函数了
插件系统开发流程
一个插件系统运转工作,主要进行以下几个方面的操作
- 获取插件,通过对一个目录里的
.py
文件扫描得到 - 将插件目录加入到环境变量
sys.path
- 爬虫将扫描好的URL和网页源码传递给插件
- 插件工作,工作完毕后将主动权还给扫描器
插件系统代码
在lib/core/plugin.py
中创建一个spiderplus
类,实现满足我们要求的代码
# __author__ = 'mathor'
import os
import sys
class spiderplus(object):
def __init__(self, plugin, disallow = []):
self.dir_exploit = []
self.disallow = ['__init__']
self.disallow.extend(disallow)
self.plugin = os.getcwd() + '/' + plugin
sys.path.append(plugin)
def list_plusg(self):
def filter_func(file):
if not file.endswith('.py'):
return False
for disfile in self.disallow:
if disfile in file:
return False
return True
dir_exploit = filter(filter_func, os.listdir(self.plugin)
return list(dir_exploit)
def work(self, url, html):
for _plugin in self.list_plusg():
try:
m = __import__(_plugin.split('.')[0])
spider = getattr(m, 'spider')
p = spider()
s = p.run(url, html)
except Exception as e:
print (e)
work
函数中需要传递url,html,这个就是我们扫描器传给插件系统的,通过代码
spider = getattr(m, 'spider')
p = spider()
s = p.run(url, html)
我们定义插件必须使用class spider
中的run
方法调用
扫描器中调用插件
我们主要用爬虫调用插件,因为插件需要传递url和网页源码这两个参数,所以我们在爬虫获取到这两个的地方加入插件系统代码即可
首先打开Spider.py
,在Spider.py
文件开头加上
from lib.core import plugin
然后在文件的末尾加上
disallow = ['sqlcheck']
_plugin = plugin.spiderplus('script', disallow)
_plugin.work(_str['url'], _str['html'])
disallow
是不允许的插件列表,为了方便测试,我们可以把sqlcheck填上
SQL注入融入插件系统
其实非常简单,只需要修改script/sqlcheck.py
为下面即可
关于Download
模块,其实就是Downloader
模块,把Downloader.py
复制一份命名为Download.py
就行
import re, random
from lib.core import Download
class spider:
def run(self, url, html):
if (not url.find("?")): # Pseudo-static page
return false;
Downloader = Download.Downloader()
BOOLEAN_TESTS = (" AND %d=%d", " OR NOT (%d=%d)")
DBMS_ERRORS = {
# regular expressions used for DBMS recognition based on error message response
"MySQL": (r"SQL syntax.*MySQL", r"Warning.*mysql_.*", r"valid MySQL result", r"MySqlClient\."),
"PostgreSQL": (r"PostgreSQL.*ERROR", r"Warning.*\Wpg_.*", r"valid PostgreSQL result", r"Npgsql\."),
"Microsoft SQL Server": (r"Driver.* SQL[\-\_\ ]*Server", r"OLE DB.* SQL Server", r"(\W|\A)SQL Server.*Driver", r"Warning.*mssql_.*", r"(\W|\A)SQL Server.*[0-9a-fA-F]{8}", r"(?s)Exception.*\WSystem\.Data\.SqlClient\.", r"(?s)Exception.*\WRoadhouse\.Cms\."),
"Microsoft Access": (r"Microsoft Access Driver", r"JET Database Engine", r"Access Database Engine"),
"Oracle": (r"\bORA-[0-9][0-9][0-9][0-9]", r"Oracle error", r"Oracle.*Driver", r"Warning.*\Woci_.*", r"Warning.*\Wora_.*"),
"IBM DB2": (r"CLI Driver.*DB2", r"DB2 SQL error", r"\bdb2_\w+\("),
"SQLite": (r"SQLite/JDBCDriver", r"SQLite.Exception", r"System.Data.SQLite.SQLiteException", r"Warning.*sqlite_.*", r"Warning.*SQLite3::", r"\[SQLITE_ERROR\]"),
"Sybase": (r"(?i)Warning.*sybase.*", r"Sybase message", r"Sybase.*Server message.*"),
}
_url = url + "%29%28%22%27"
_content = Downloader.get(_url)
for (dbms, regex) in ((dbms, regex) for dbms in DBMS_ERRORS for regex in DBMS_ERRORS[dbms]):
if (re.search(regex,_content)):
return True
content = {}
content['origin'] = Downloader.get(_url)
for test_payload in BOOLEAN_TESTS:
# Right Page
RANDINT = random.randint(1, 255)
_url = url + test_payload % (RANDINT, RANDINT)
content["true"] = Downloader.get(_url)
_url = url + test_payload % (RANDINT, RANDINT + 1)
content["false"] = Downloader.get(_url)
if content["origin"] == content["true"] != content["false"]:
return "sql found: %" % url
E-Mail搜索插件
最后一个简单的例子,搜索网页中的E-Mail,因为插件系统会传递网页源码,我们用一个正则表达式([\w-]+@[\w-]+\.[\w-]+)+
搜索出所有的邮件。创建script/email_check.py
文件
# __author__ = 'mathor'
import re
class spider():
def run(self, url, html):
#print(html)
pattern = re.compile(r'([\w-]+@[\w-]+\.[\w-]+)+')
email_list = re.findall(pattern, html)
if (email_list):
print(email_list)
return True
return False
运行python w8ay.py
可以看到网页中的邮箱都被采集到了
[...]Via www.wmathor.com[...]
[...]Via www.wmathor.com[...]