python實現(xiàn)web郵箱掃描的示例(附源碼)
信息收集是進行滲透測試的關(guān)鍵部分,掌握大量的信息對于攻擊者來說是一件非常重要的事情,比如,我們知道一個服務(wù)器的版本信息,我們就可以利用該服務(wù)器框架的相關(guān)漏洞對該服務(wù)器進行測試。那么如果我們掌握了該服務(wù)器的管理員的郵箱地址,我們就可以展開一個釣魚攻擊。所以,對web站點進行郵箱掃描,是進行釣魚攻擊的一種前提條件。
下面,我們利用python腳本來實現(xiàn)一個web站點的郵箱掃描爬取。目的是在實現(xiàn)這個腳本的過程中對python進行學(xué)習(xí)
最后有完整代碼
基本思路
- 我們向工具傳入目標(biāo)站點之后,首先要對輸入進行一個基本的檢查和分析,因為我們會可能會傳入各種樣式的地址,比如http://www.xxxx.com/、http://www.xxxx.com/123/456/789.html等等,我們需要對其進行簡單的拆分,以便于后面鏈接的爬取
- 通過requests庫爬取目標(biāo)地址的內(nèi)容,并且在內(nèi)容通過正則表達式中尋找郵箱地址
- 查找爬取的網(wǎng)站中的超鏈接,通過這些超鏈接我們就能進入到該站點的另外一個頁面繼續(xù)尋找我們想要的郵箱地址。
- 開工:
該腳本所需要的一些庫
from bs4 import BeautifulSoup #BeautifulSoup最主要的功能是從網(wǎng)頁抓取數(shù)據(jù),Beautiful Soup自動將輸入文檔轉(zhuǎn)換為Unicode編碼 import requests #requests是python實現(xiàn)的最簡單易用的HTTP庫 import requests.exceptions import urllib.parse from collections import deque #deque 是一個雙端隊列, 如果要經(jīng)常從兩端append 的數(shù)據(jù), 選擇這個數(shù)據(jù)結(jié)構(gòu)就比較好了, 如果要實現(xiàn)隨機訪問,不建議用這個,請用列表. import re #是一個正則表達式的庫
獲取掃描目標(biāo)
user_url=str(input('[+] Enter Target URL to Scan:')) urls =deque([user_url]) #把目標(biāo)地址放入deque對象列表 scraped_urls= set()#set() 函數(shù)創(chuàng)建一個無序不重復(fù)元素集,可進行關(guān)系測試,刪除重復(fù)數(shù)據(jù),還可以計算交集、差集、并集等。 emails = set()
對網(wǎng)頁進行郵箱地址爬?。?00條)
首先要對目標(biāo)地址進行分析,拆分目標(biāo)地址的協(xié)議,域名以及路徑。然后利用requests的get方法訪問網(wǎng)頁,通過正則表達式過濾出是郵箱地址的內(nèi)容。'[a-z0-0.-+]+@[a-z0-9.-+]+.[a-z]+',符合郵箱格式的內(nèi)容就進行收錄。
count=0 try: while len(urls): #如果urls有長度的話進行循環(huán) count += 1 #添加計數(shù)器來記錄爬取鏈接的條數(shù) if count ==101: break url = urls.popleft() #popleft()會刪除urls里左邊第一條數(shù)據(jù)并傳給url scraped_urls.add(url) parts = urllib.parse.urlsplit(url) # 打印 parts會顯示:SplitResult(scheme='http', netloc='www.baidu.com', path='', query='', fragment='') base_url = '{0.scheme}://{0.netloc}'.format(parts)#scheme:協(xié)議;netloc:域名 path = url[:url.rfind('/')+1] if '/' in parts.path else url#提取路徑 print('[%d] Processing %s' % (count,url)) try: head = {'User-Agent':"Opera/9.80 (Macintosh; Intel Mac OS X 10.6.8; U; en) Presto/2.8.131 Version/11.11"} response = requests.get(url,headers = head) except(requests.exceptions.MissingSchema,requests.exceptions.ConnectionError): continue new_emails = set(re.findall(r'[a-z0-0\.\-+_]+@[a-z0-9\.\-+_]+\.[a-z]+', response.text ,re.I))#通過正則表達式從獲取的網(wǎng)頁中提取郵箱,re.I表示忽略大小寫 emails.update(new_emails)#將獲取的郵箱地址存在emalis中。
通過錨點進入下一網(wǎng)頁繼續(xù)搜索
soup = BeautifulSoup(response.text, features='lxml') for anchor in soup.find_all('a'): #尋找錨點。在html中,<a>標(biāo)簽代表一個超鏈接,herf屬性就是鏈接地址 link = anchor.attrs['href'] if 'href' in anchor.attrs else '' #如果,我們找到一個超鏈接標(biāo)簽,并且該標(biāo)簽有herf屬性,那么herf后面的地址就是我們需要錨點鏈接。 if link.startswith('/'):#如果該鏈接以/開頭,那它只是一個路徑,我們就需要加上協(xié)議和域名,base_url就是剛才分離出來的協(xié)議+域名 link = base_url + link elif not link.startswith('http'):#如果不是以/和http開頭的話,就要加上路徑。 link =path + link if not link in urls and not link in scraped_urls:#如果該鏈接在之前沒還有被收錄的話,就把該鏈接進行收錄。 urls.append(link) except KeyboardInterrupt: print('[+] Closing') for mail in emails: print(mail)
完整代碼
from bs4 import BeautifulSoup import requests import requests.exceptions import urllib.parse from collections import deque import re user_url=str(input('[+] Enter Target URL to Scan:')) urls =deque([user_url]) scraped_urls= set() emails = set() count=0 try: while len(urls): count += 1 if count ==100: break url = urls.popleft() scraped_urls.add(url) parts = urllib.parse.urlsplit(url) base_url = '{0.scheme}://{0.netloc}'.format(parts) path = url[:url.rfind('/')+1] if '/' in parts.path else url print('[%d] Processing %s' % (count,url)) try: head = {'User-Agent':"Opera/9.80 (Macintosh; Intel Mac OS X 10.6.8; U; en) Presto/2.8.131 Version/11.11"} response = requests.get(url,headers = head) except(requests.exceptions.MissingSchema,requests.exceptions.ConnectionError): continue new_emails = set(re.findall(r'[a-z0-0\.\-+_]+@[a-z0-9\.\-+_]+\.[a-z]+', response.text ,re.I)) emails.update(new_emails) soup = BeautifulSoup(response.text, features='lxml') for anchor in soup.find_all('a'): link = anchor.attrs['href'] if 'href' in anchor.attrs else '' if link.startswith('/'): link = base_url + link elif not link.startswith('http'): link =path + link if not link in urls and not link in scraped_urls: urls.append(link) except KeyboardInterrupt: print('[+] Closing') for mail in emails: print(mail)
實驗………………
以上就是python實現(xiàn)web郵箱掃描的示例(附源碼)的詳細內(nèi)容,更多關(guān)于python web郵箱掃描的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
python中使用iterrows()對dataframe進行遍歷的實例
今天小編就為大家分享一篇python中使用iterrows()對dataframe進行遍歷的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-06-06PyTorch?TensorFlow機器學(xué)習(xí)框架選擇實戰(zhàn)
這篇文章主要為大家介紹了PyTorch?TensorFlow機器學(xué)習(xí)框架選擇實戰(zhàn),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-10-10詳解Python prometheus_client使用方式
本文主要介紹了Python prometheus_client使用方式,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-02-02python將ansible配置轉(zhuǎn)為json格式實例代碼
這篇文章主要介紹了python將ansible配置轉(zhuǎn)為json格式實例代碼的相關(guān)資料,需要的朋友可以參考下2017-05-05pytorch nn.Conv2d()中的padding以及輸出大小方式
今天小編就為大家分享一篇pytorch nn.Conv2d()中的padding以及輸出大小方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-01-01