__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[...]