Python實(shí)現(xiàn)E-Mail收集插件實(shí)例教程
__import__函數(shù)
我們都知道import是導(dǎo)入模塊的,但是其實(shí)import實(shí)際上是使用builtin函數(shù)import來工作的。在一些程序中,我們可以動(dòng)態(tài)去調(diào)用函數(shù),如果我們知道模塊的名稱(字符串)的時(shí)候,我們可以很方便的使用動(dòng)態(tài)調(diào)用
def getfunctionbyname(module_name, function_name): module = __import__(module_name) return getattr(module, function_name)
通過這段代碼,我們就可以簡單調(diào)用一個(gè)模塊的函數(shù)了
插件系統(tǒng)開發(fā)流程
一個(gè)插件系統(tǒng)運(yùn)轉(zhuǎn)工作,主要進(jìn)行以下幾個(gè)方面的操作
- 獲取插件,通過對一個(gè)目錄里的.py文件掃描得到
- 將插件目錄加入到環(huán)境變量sys.path
- 爬蟲將掃描好的 URL 和網(wǎng)頁源碼傳遞給插件
- 插件工作,工作完畢后將主動(dòng)權(quán)還給掃描器
插件系統(tǒng)代碼
在lib/core/plugin.py中創(chuàng)建一個(gè)spiderplus類,實(shí)現(xiàn)滿足我們要求的代碼
# __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函數(shù)中需要傳遞 url,html,這個(gè)就是我們掃描器傳給插件系統(tǒng)的,通過代碼
spider = getattr(m, 'spider') p = spider() s = p.run(url, html)
我們定義插件必須使用class spider中的run方法調(diào)用
掃描器中調(diào)用插件
我們主要用爬蟲調(diào)用插件,因?yàn)椴寮枰獋鬟f url 和網(wǎng)頁源碼這兩個(gè)參數(shù),所以我們在爬蟲獲取到這兩個(gè)的地方加入插件系統(tǒng)代碼即可
首先打開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 注入融入插件系統(tǒng)
其實(shí)非常簡單,只需要修改script/sqlcheck.py為下面即可
關(guān)于Download模塊,其實(shí)就是Downloader模塊,把Downloader.py復(fù)制一份命名為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 搜索插件
最后一個(gè)簡單的例子,搜索網(wǎng)頁中的 E-Mail,因?yàn)椴寮到y(tǒng)會(huì)傳遞網(wǎng)頁源碼,我們用一個(gè)正則表達(dá)式([\w-]+@[\w-]+\.[\w-]+)+搜索出所有的郵件。創(chuàng)建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
運(yùn)行python w8ay.py

可以看到網(wǎng)頁中的郵箱都被采集到了
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。
相關(guān)文章
python3 讀取Excel表格中的數(shù)據(jù)
這篇文章主要介紹了python3 讀取Excel表格中的數(shù)據(jù)的相關(guān)資料,需要的朋友可以參考下2018-10-10
基于python進(jìn)行桶排序與基數(shù)排序的總結(jié)
今天小編就為大家分享一篇基于python進(jìn)行桶排序與基數(shù)排序的總結(jié),具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-05-05
python 在指定范圍內(nèi)隨機(jī)生成不重復(fù)的n個(gè)數(shù)實(shí)例
今天小編就為大家分享一篇python 在指定范圍內(nèi)隨機(jī)生成不重復(fù)的n個(gè)數(shù)實(shí)例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-01-01
詳解python中Numpy的屬性與創(chuàng)建矩陣
這篇文章給大家分享了關(guān)于python中Numpy的屬性與創(chuàng)建矩陣的相關(guān)知識(shí)點(diǎn)內(nèi)容,有興趣的朋友們可以學(xué)習(xí)參考下。2018-09-09
Python將運(yùn)行結(jié)果導(dǎo)出為CSV格式的兩種常用方法
這篇文章主要給大家介紹了關(guān)于Python將運(yùn)行結(jié)果導(dǎo)出為CSV格式的兩種常用方法,Python生成(導(dǎo)出)csv文件其實(shí)很簡單,我們一般可以用csv模塊或者pandas庫來實(shí)現(xiàn),需要的朋友可以參考下2023-07-07

