DomXss檢測模塊整合開源掃描器框架golismero

有時候我們寫一個檢測模塊,想在真實的網絡環境中去檢測它的能力和穩定性,想大批量的對網絡中的網站進行探測,往往我們的解決方案是:


1,依靠API調各種搜索引擎的搜索結果2,自己針對檢測模塊寫爬蟲3,網上先採集一批潛在站點域名,寫個臨時的批量腳本。


No.1的問題是搜索引擎有數量和頻次上的限制,而且只能依賴搜索引擎的爬取結果,機動性差。

No.2的問題是難道以後每次寫一個檢測模塊都要寫個爬蟲配合麼,那樣太痛苦了。

No.3的問題和1一樣,機動性差,適合小批量的掃描。

於是接下來我介紹一款國外的開源掃描框架golismero(向開源致敬)能夠將我們自己寫的檢測模塊靈活的嵌入到引擎中,配合爬蟲等插件,進行定製化的掃描,從而避免我們把精力過多的放在爬蟲等外部套件中而是專注提高檢測模塊的自身能力。

這裏引用golismero官網的一段描述:

“GoLismeroisanOpenSourcesecuritytoolsthatcanruntheirownsecuritytestsandmanagealotofwellknownsecuritytools(OpenVas,Wfuzz,SQLMap,DNSrecon,robotanalyzer...)taketheirresults,feedbacktotherestoftoolsandmergeallofresults.Andallofthisautomatically.”

“Golismero是一個開源的安全工具,不但能運行自帶的檢測模塊,也能夠操作一系列知名的安全工具(Openvas,Wfuzz,SQLMap,DNSrecon,robotanalyzer等),並獲取他們的掃描結果彙總在一起,而所有這些過程都是自動化的。”忘了一句很重要的,golismero使用python語言編寫。

20131103161434_24132.png

golismero經過了幾個版本的改良,現在無論從功能性還是穩定性上都較爲強大,這篇文章主要介紹如何將自己編寫的檢測模塊嵌入到golismero掃描框架中。

DomXssFuzz:(自己寫的domxss檢測模塊,基於phantomjs和casperjs,修改了webkit源碼,保證在IE和Webkit下通用,fuzz模式)

Golismero:強大的開源網站漏洞掃描框架

domxss檢測模塊是一個Javascript腳本,首先需要考慮如何用python調用,這裏我使用subprocess模塊開子進程調用domXssFuzz.js,然後檢測完畢後js腳本會輸出檢測結果({…}的形式),python中直接通過communicate()函數讀出子進程的輸出,將結果字符串eval成字典,再做後期的處理。這是整體的思路,其中的細節,子進程超時處理等等不在此討論。

20131103162817_14388.png

調用部分主要代碼如下:

origin_wkdir=os.getcwd()os.chdir(os.path.dirname(os.path.realpath(__file__))+'/domxss')try:p=subprocess.Popen("phantomjs./domXssFu.js\""+url+"\"--hash",stdout=subprocess.PIPE,stderr=subprocess.PIPE,shell=True)Logger.log_verbose("Testing%sfordomxss..."%url)os.chdir(origin_wkdir)except:pass#return{}start=datetime.datetime.now()whilep.poll()isNone:time.sleep(0.1)now=datetime.datetime.now()if(now-start).seconds>timeout:os.kill(p.pid,signal.SIGKILL)os.waitpid(-1,os.WNOHANG)break#importpdb;pdb.set_trace()results=[]rsStr=p.communicate()[0].strip()

接下來考慮如何嵌入golismero框架中,這裏給出插件部分的相關API:

classgolismero.api.plugin.TestingPlugin[source]Bases:golismero.api.plugin._InformationPluginTestingpluginsaretheonesthatperformthesecuritytests.ThisisthebaseclassforallTestingplugins.__init__x.__init__(...)initializesx;seehelp(type(x))forsignatureget_accepted_info()Returnalistofconstantsdescribingwhichdatatypesareacceptedbytherecv_infomethod.Returns:Datatypeconstants.Returntype:listrecv_info(info)Callbackmethodtoreceivedatatobeprocessed.Thisisthemostimportantmethodofaplugin.Hereswheremostofthelogicresides.Parameters:info(Data)Datatobeprocessed.Returns:Sharedpluginstatevariables.Returntype:PluginStateupdate_status(progress=None)Pluginscancallthismethodtotelltheuserofthecurrentprogressofwhateverthepluginisdoing.

其中所有掃描插件類都要繼承golismero.api.plugin.TestingPlugin,__init__函數可以自行處理,這裏比較重要的兩個函數是get_accepted_info和recv_info,主掃描進程調用每個插件的recv_info函數,因此自定義的插件主體內容應該在該函數內,最後要將檢測結果返回,該函數接受主進程傳入的Url對象供插件處理。而get_accepted_info則告訴主進程當前自定義檢測插件的返回結果類型是什麼str,list,dist或者其他什麼。update_status則是用來向主進程彙報當前插件的檢測進度百分比。


結果返回也比較有講究,我這裏返回一個list,list中是Url對象存放domxss檢測結果,因此在get_accepted_info中要寫return[Url]。

最後整個插件代碼如下(domxssfuzz.py):

#!/usr/bin/python#coding:utf-8#codebypnig0s@knownsecimportsysimportosimporttimeimportdatetimeimportsignalimportsubprocessimportselectimporturlparsefromgolismero.api.data.resource.urlimportUrlfromgolismero.api.data.vulnerability.suspicious.urlimportSuspiciousURLfromgolismero.api.loggerimportLoggerfromgolismero.api.pluginimportTestingPlugintimeout=100classDomXssFuzz(TestingPlugin):#----------------------------------------------------------------------def__init__(self):"""Constructor"""passdefget_accepted_info(self):return[Url]defrecv_info(self,info):ifnotinfoortype(info)isnotUrl:returnrsDict={}rsStr=''url=str(info.url)ext=url.split('.')[-1]ifnot(extin['html','html','shtml','xhtml']):return#importpdb;pdb.set_trace()origin_wkdir=os.getcwd()os.chdir(os.path.dirname(os.path.realpath(__file__))+'/domxss')try:p=subprocess.Popen("phantomjs./domXssFu.js\""+url+"\"--hash",stdout=subprocess.PIPE,stderr=subprocess.PIPE,shell=True)Logger.log_verbose("Testing%sfordomxss..."%url)os.chdir(origin_wkdir)except:pass#return{}start=datetime.datetime.now()whilep.poll()isNone:time.sleep(0.1)now=datetime.datetime.now()if(now-start).seconds>timeout:os.kill(p.pid,signal.SIGKILL)os.waitpid(-1,os.WNOHANG)break#importpdb;pdb.set_trace()results=[]rsStr=p.communicate()[0].strip()ifrsStrand(rsStr[0]=='{'andrsStr[-1]=='}'):rsDict=eval(rsStr)ifrsDictandrsDict.get('isVul','false')=='true':for_valueinrsDict.values():ifisinstance(_value,list)and_value:vul_url=_value[0]des="Finddomxssin[%s]"%vul_urlresults.append(SuspiciousURL(url=Url(url=vul_url),substring="noargs",level="informational",description=des))Logger.log_verbose(des)returnresults

除了這個核心的插件腳本之外,我們還需要建立一個domxssfuzz.golismero文件來說明當前插件的相關信息:

[Documentation]Description=DetectdomxssAuthor=pnig0sVersion=0.1Website=https://pnigos.comCopyright=Copyright(C)2011-2013GoLismeroProjectLicense=GNUPublicLicense[Core]Name=DetectDomXssModule=domxssfuzz.pyClass=DomXssFuzz

插件到此基本就結束了,可是沒人能保證一次執行成功,調試是必不可少的,可以直接啓golismero.py加載插件進行調試,但這樣太繁瑣了,況且golismero是多進程調用插件,調試起來很痛苦的。我們自己寫一個腳本,把Url資源傳到我們的插件中即可。

domxssfuzz_test.py:

#!/usr/bin/envpython#-*-coding:utf-8-*-importosfromosimportpathimportsysscript=__file__ifpath.islink(script):script=path.realpath(script)here=path.split(path.abspath(script))[0]ifnothere:#ifitfailsusecwdinsteadhere=path.abspath(os.getcwd())thirdparty_libs=path.join(here,"thirdparty_libs")ifpath.exists(thirdparty_libs):has_here=hereinsys.pathhas_thirdparty_libs=thirdparty_libsinsys.pathifnot(has_hereandhas_thirdparty_libs):ifhas_here:sys.path.remove(here)ifhas_thirdparty_libs:sys.path.remove(thirdparty_libs)if__name__=="__main__":#Asaportablescript:useourversionsalwayssys.path.insert(0,thirdparty_libs)sys.path.insert(0,here)else:#Wheninstalling:prefersystemversiontoourssys.path.insert(0,here)sys.path.append(thirdparty_libs)fromgolismero.main.testingimportPluginTesterfromgolismero.api.data.resource.urlimportUrldefmain():withPluginTester()ast:u=Url("http://www.gi.pku.edu.cn/czhuanti/?chan=5&did=6.html")printt.run_plugin("testing/scan/domxssfuzz",u)if__name__=='__main__':main()

這樣我們可以直接運行測試腳本來單進程(線程)調試插件了。

最後來兩張通過golismero運行我們domxssfuzz插件的圖:

20131103165908_53411.png

20131103170004_58500.png

除了將我們的檢測模塊嵌入類似golismero開源的掃描引擎,我們還可以寫一個extension整合進scrapy爬蟲框架中,這個思路之前也結合domxssfuzz實現了,只是還不完善,後面有機會再說。

以上是針對Web層面的檢測模塊的大規模測試方案,基於主機問題的相關探測可以基於ZMap來進行,一臺個人PC,幾個小時,整個C段空間掃一輪。

拙作一篇,輕拍。

給出一些Brainstorm的資源:

InternetCensus2012Portscanning/0usinginsecureembeddeddevices

http://internetcensus2012.bitbucket.org/paper.html

Zmap詳細用戶手冊及DDOS的可行性

http://www.freebuf.com/tools/11985.html

ZMap:在一個小時內掃描整個互聯網

http://www.freebuf.com/tools/11884.html

REF:

http://golismero-project.com/

http://blog.csdn.net/hujkay/article/details/11800847

轉載:http://www.pnigos.com/?p=147

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章