信息收集是進(jìn)行滲透測(cè)試的關(guān)鍵部分,掌握大量的信息對(duì)于攻擊者來(lái)說(shuō)是一件非常重要的事情,比如,我們知道一個(gè)服務(wù)器的版本信息,我們就可以利用該服務(wù)器框架的相關(guān)漏洞對(duì)該服務(wù)器進(jìn)行測(cè)試。那么如果我們掌握了該服務(wù)器的管理員的郵箱地址,我們就可以展開(kāi)一個(gè)釣魚(yú)攻擊。所以,對(duì)web站點(diǎn)進(jìn)行郵箱掃描,是進(jìn)行釣魚(yú)攻擊的一種前提條件。
下面,我們利用python腳本來(lái)實(shí)現(xiàn)一個(gè)web站點(diǎn)的郵箱掃描爬取。目的是在實(shí)現(xiàn)這個(gè)腳本的過(guò)程中對(duì)python進(jìn)行學(xué)習(xí)
最后有完整代碼
from bs4 import BeautifulSoup #BeautifulSoup最主要的功能是從網(wǎng)頁(yè)抓取數(shù)據(jù),Beautiful Soup自動(dòng)將輸入文檔轉(zhuǎn)換為Unicode編碼 import requests #requests是python實(shí)現(xiàn)的最簡(jiǎn)單易用的HTTP庫(kù) import requests.exceptions import urllib.parse from collections import deque #deque 是一個(gè)雙端隊(duì)列, 如果要經(jīng)常從兩端append 的數(shù)據(jù), 選擇這個(gè)數(shù)據(jù)結(jié)構(gòu)就比較好了, 如果要實(shí)現(xiàn)隨機(jī)訪(fǎng)問(wèn),不建議用這個(gè),請(qǐng)用列表. import re #是一個(gè)正則表達(dá)式的庫(kù)
user_url=str(input('[+] Enter Target URL to Scan:')) urls =deque([user_url]) #把目標(biāo)地址放入deque對(duì)象列表 scraped_urls= set()#set() 函數(shù)創(chuàng)建一個(gè)無(wú)序不重復(fù)元素集,可進(jìn)行關(guān)系測(cè)試,刪除重復(fù)數(shù)據(jù),還可以計(jì)算交集、差集、并集等。 emails = set()
首先要對(duì)目標(biāo)地址進(jìn)行分析,拆分目標(biāo)地址的協(xié)議,域名以及路徑。然后利用requests的get方法訪(fǎng)問(wèn)網(wǎng)頁(yè),通過(guò)正則表達(dá)式過(guò)濾出是郵箱地址的內(nèi)容。'[a-z0-0.-+]+@[a-z0-9.-+]+.[a-z]+',符合郵箱格式的內(nèi)容就進(jìn)行收錄。
count=0 try: while len(urls): #如果urls有長(zhǎng)度的話(huà)進(jìn)行循環(huán) count += 1 #添加計(jì)數(shù)器來(lái)記錄爬取鏈接的條數(shù) if count ==101: break url = urls.popleft() #popleft()會(huì)刪除urls里左邊第一條數(shù)據(jù)并傳給url scraped_urls.add(url) parts = urllib.parse.urlsplit(url) # 打印 parts會(huì)顯示: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))#通過(guò)正則表達(dá)式從獲取的網(wǎng)頁(yè)中提取郵箱,re.I表示忽略大小寫(xiě) emails.update(new_emails)#將獲取的郵箱地址存在emalis中。
soup = BeautifulSoup(response.text, features='lxml') for anchor in soup.find_all('a'): #尋找錨點(diǎn)。在html中,a>標(biāo)簽代表一個(gè)超鏈接,herf屬性就是鏈接地址 link = anchor.attrs['href'] if 'href' in anchor.attrs else '' #如果,我們找到一個(gè)超鏈接標(biāo)簽,并且該標(biāo)簽有herf屬性,那么herf后面的地址就是我們需要錨點(diǎn)鏈接。 if link.startswith('/'):#如果該鏈接以/開(kāi)頭,那它只是一個(gè)路徑,我們就需要加上協(xié)議和域名,base_url就是剛才分離出來(lái)的協(xié)議+域名 link = base_url + link elif not link.startswith('http'):#如果不是以/和http開(kāi)頭的話(huà),就要加上路徑。 link =path + link if not link in urls and not link in scraped_urls:#如果該鏈接在之前沒(méi)還有被收錄的話(huà),就把該鏈接進(jìn)行收錄。 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實(shí)現(xiàn)web郵箱掃描的示例(附源碼)的詳細(xì)內(nèi)容,更多關(guān)于python web郵箱掃描的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
標(biāo)簽:安慶 日照 隨州 錦州 股票 白城 天水 西安
巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《python實(shí)現(xiàn)web郵箱掃描的示例(附源碼)》,本文關(guān)鍵詞 python,實(shí)現(xiàn),web,郵箱,掃描,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。