0x01 場景
比如編寫掃描器時,需要驗證某個POC或者復現某個漏洞,但環境受限於企業版、不公開源碼等原因無法搭建,目標漏洞在搜索引擎內又大部分已修復,這里搜索引擎使用Fofa,使用API獲取會有大量冗余的問題,因為API獲取的url貌似都是固定的,我獲取多少數量的url,前面的都一樣,導致我認為這些url存在漏洞的可能性較低,所以我需要搜索引擎靠后位置的一些目標URL來學習,可能靠后位置的url我驗證100個就可以找到所需要的,而靠前位置的我需要驗證1000個才能找到需要的
0x02 思路
- 1.加工自某位師傅(感謝)的腳本,忘記在哪看到的了 0.0! , 完善了一個bug,Fofa的某些搜索結果的url存在有小鎖鏈和沒有的情況(就是URL旁邊點擊后直接訪問目標的小鎖鏈),這時的標簽是不同的,所以需要特別處理
- 2.獲得URL是使用python的PyQuery模塊加工網頁源代碼,獲取標簽內容
PyQuery模塊用到的語法 from pyquery import PyQuery as pq #導入PyQuery模塊 req = requests.get(url=url,headers=headers,cookies=cookies) doc=pq(req.text) #將獲得的源代碼進行標簽化 doc('div.results_content .list_mod_t').items() #針對多個標簽時,獲得的標簽集合在一起,后續獲得標簽內容為遍歷集合后獲得 relDoc('.ip-no-url').text() #針對1個標簽時,獲得標簽的class為ip-no-url的標簽內容 doc.find('a').eq(0).attr.href #查找標簽下的a標簽並獲得href屬性內容
- 3.獲得總頁數是使用正則提取的PyQuery處理過的標簽內容(看到這的師傅,推薦去簡單了解一下python的這個pq庫或者那個beautifulsoup庫,對以后獲取自己需要的信息會有幫助,有思路的時候就會有想法嘛,對吧~)
pattern = re.compile(u'獲得 (.*?) 條匹配結果') #使用(.*?)大法,強迫症師傅饒我一命~ result = re.findall(pattern,page) result = result[0].replace(',','')
- 4.然后根據實際情況來輸入從哪頁開始,共獲得多少頁,最后加工一下交互就完成了,代碼簡單,能解決我的小需求就好 -.- 。
startPage = input("[+ 搜索結果共有{}頁,請輸入從第幾頁開始收集地址(例:5):".format(allPage)) page = input("[+ 搜索結果共有{}頁,請輸入准備收集頁數(例:20):".format(allPage))
- 5.順帶說下Fofa開個普通會員蠻實用的(界面友好,交互友好),大不了就跟認識的朋友,同事,同學幾個人或者這個帖子評論區找找有沒有想買的一起買,畢竟賬號沒那么多登錄限制的
0x03 使用
- Usage: python3 X-Fofa.py 'app="CouchDB"' CouchDB 94bbbb177c4a564feddb8c7d413d5d61
- Usage: python3 X-Fofa.py Fofa搜索語法 搜索結果文件名 Fofa的Cookie的_fofapro_ars_session值
- 按照需求輸入 從哪頁開始 和 獲取多少頁數 即可
- 獲得_fofapro_ars_session值
0x04 源碼
1 #!/usr/bin/python3 2 3 import requests,time,random,os,re,sys,base64,urllib.parse 4 from pyquery import PyQuery as pq 5 6 banner = ''' 7 __ __ ______ __ 8 \ \ / / | ____| / _| 9 \ V / ______ | |__ ___ | |_ __ _ 10 > < |______| | __| / _ \ | _| / _` | 11 / . \ | | | (_) | | | | (_| | 12 /_/ \_\ |_| \___/ |_| \__,_| 13 14 by 斯文 15 ''' 16 17 def usera(): 18 #user_agent 集合 19 user_agent_list = [ 20 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) ' 21 'Chrome/45.0.2454.85 Safari/537.36 115Browser/6.0.3', 22 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_8; en-us) AppleWebKit/534.50 (KHTML, like Gecko) Version/5.1 Safari/534.50', 23 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-us) AppleWebKit/534.50 (KHTML, like Gecko) Version/5.1 Safari/534.50', 24 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0)', 25 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)', 26 'Mozilla/5.0 (Windows NT 6.1; rv:2.0.1) Gecko/20100101 Firefox/4.0.1', 27 'Opera/9.80 (Windows NT 6.1; U; en) Presto/2.8.131 Version/11.11', 28 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_0) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11', 29 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Trident/4.0; SE 2.X MetaSr 1.0; SE 2.X MetaSr 1.0; .NET CLR 2.0.50727; SE 2.X MetaSr 1.0)', 30 'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0', 31 'Mozilla/5.0 (Windows NT 6.1; rv:2.0.1) Gecko/20100101 Firefox/4.0.1', 32 ] 33 #隨機選擇一個 34 user_agent = random.choice(user_agent_list) 35 #傳遞給header 36 headers = { 'User-Agent': user_agent } 37 return headers 38 39 def getPage(cookie,search): 40 41 url='https://classic.fofa.so/result?page=1&qbase64={}'.format(search) 42 cookies = {'_fofapro_ars_session':cookie} 43 req = requests.get(url=url,headers=usera(),cookies=cookies) 44 pageHtml = pq(req.text) 45 page = (pageHtml('div.list_jg')).text() 46 # page = page.find('') 47 48 pattern = re.compile(u'獲得 (.*?) 條匹配結果') 49 result = re.findall(pattern,page) 50 result = result[0].replace(',','') 51 52 if (int(result) % 10) >0: 53 allPage = int(result) // 10 + 1 54 else: 55 allPage = int(result) // 10 56 57 return allPage 58 59 def start(search,file,cookie): 60 61 search=search.encode(encoding="utf-8") 62 search=base64.b64encode(search).decode() 63 search=urllib.parse.quote(search) 64 # if os.path.exists("result.txt"): #刪除存在的文件 65 # os.remove("result.txt") 66 # cookie = input("請輸入Fofa的Cookie的_fofapro_ars_session值:") 67 allPage = getPage(cookie,search) 68 print(banner) 69 startPage = input("[+ 搜索結果共有{}頁,請輸入從第幾頁開始收集地址(例:5):".format(allPage)) 70 page = input("[+ 搜索結果共有{}頁,請輸入准備收集頁數(例:20):".format(allPage)) 71 endPage = int(startPage) + int(page) 72 73 cookies={'_fofapro_ars_session':cookie}#這里是你的fofa賬號登錄后的cookie值 74 url='https://fofa.so/result?qbase64={}'.format(search) 75 # doc=pq(url) 76 print("[+ 正在向{}.txt文件寫入結果".format(file)) 77 with open('%s.txt'%file,'a+',encoding='utf-8') as f: 78 for i in range(int(startPage),endPage): 79 url='https://classic.fofa.so/result?page={}&qbase64={}'.format(i,search) 80 req = requests.get(url=url,headers=usera(),cookies=cookies) 81 if '游客使用高級語法' in req.text: 82 print('[- Cookie已失效,請重新填寫https://classic.fofa.so的Cookie,不是https://fofa.so的Cookie') 83 break 84 print("[+ 正在讀取第{}頁 狀態碼:{}".format(i,req.status_code)) 85 doc=pq(req.text) 86 87 url=doc('div.results_content .list_mod_t').items() 88 title=doc('div.list_mod_c ul').items() 89 90 for u,t in zip(url,title): 91 t.find('i').remove() 92 relUrl = u.find('a').eq(0).attr.href 93 relTitle = t.find('li').eq(0).text() 94 95 if 'result?qbase64=' in relUrl: 96 relDoc = pq(u) 97 relIp = relDoc('.ip-no-url').text() 98 relPort = (relDoc('.span')).find('a').eq(0).text() 99 relUrl = 'http://{}:{}'.format(str(relIp),relPort) 100 if relTitle == '': 101 relTitle = '空' 102 print("Url: %s Title: %s"%(relUrl, relTitle)) 103 f.write("%s\n"%(relUrl)) 104 f.flush() 105 106 time.sleep(3) 107 108 109 if __name__ == '__main__': 110 if len(sys.argv)==1: 111 print(banner) 112 print('''Usage:請輸入參數\n例如:python X-Fofa.py 'app="Solr"' Solr 94bbbb177c4a564feddb8c7d413d5d61\n例如:python FofaCrawler.py 'app="Solr"'(Fofa搜索語法) Solr(搜索結果文件名) 94bbbb177c4a564feddb8c7d413d5d61(Fofa的Cookie的_fofapro_ars_session值)''') 113 sys.exit(0) 114 115 search=sys.argv[1] 116 file=sys.argv[2] 117 cookie = sys.argv[3] 118 start(search,file,cookie)