今天寫自己的爆破靶場WP時候,遇到有驗證碼的網站除了使用pkav的工具我們同樣可以通過py強大的第三方庫來實現識別驗證碼+后台登錄爆破,這里做個筆記~~~
0x01關於selenium
selenium 是一套完整的web應用程序測試系統,包含了測試的錄制(selenium IDE),編寫及運行(Selenium Remote Control)和測試的並行處理(Selenium Grid)。Selenium的核心Selenium Core基於JsUnit,完全由JavaScript編寫,因此可以用於任何支持JavaScript的瀏覽器上。
selenium可以模擬真實瀏覽器,自動化測試工具,支持多種瀏覽器,爬蟲中主要用來解決JavaScript渲染問題。
selenium安裝和使用
安裝selenium之前需安裝些必要工具
1. 安裝setuptools
下載地址:https://pypi.python.org/pypi/setuptools
在頁面找到zip安裝包,下載后解壓。在命令行(運行->cmd)進入解壓目錄
執行 python setup.py install 即可安裝,注意解壓路徑不要包含中文,否則安裝會報錯。
2.安裝pip
下載地址:https://pypi.python.org/pypi/pip
在頁面找到pip-9.x.tar.gz,下載后解壓。同樣在命令行進入解壓目錄,執行 python setup.py install 即可自動安裝。
3.安裝selenium
上面2個工具安裝好后,安裝selenium只需在命令行進入python安裝路徑Script目錄下,執行 pip install -U selenium 即可自動安裝。
完成安裝后在IDLE輸入 from selenium import webdriver ,如果沒報錯即代表安裝成功。
1. selenium3.0需要獨立安裝Firefox驅動,不再自帶驅動,下載地址: https://github.com/mozilla/geckodriver/releases 下載對應版本,解壓放在python安裝路徑下即可;
2. geckodriver驅動要求Friefox瀏覽器必須48版本以上,如果不是,更新Firefox;
3. 如果用Java開發,需注意3.0必須用JDK1.8版本才行;
4. Chromedriver下載:https://sites.google.com/a/chromium.org/chromedriver/downloads 同樣也是下載后放在python安裝路徑下即可。
執行結果如下,從結果中我們也可以看出基本山支持了常見的所有瀏覽器:
這里要說一下比較重要的PhantomJS,PhantomJS是一個而基於WebKit的服務端JavaScript API,支持Web而不需要瀏覽器支持,其快速、原生支持各種Web標准:Dom處理,CSS選擇器,JSON等等。PhantomJS可以用用於頁面自動化、網絡監測、網頁截屏,以及無界面測試
聲明瀏覽器對象
上面我們知道了selenium支持很多的瀏覽器,但是如果想要聲明並調用瀏覽器則需要:
from selenium import webdriver browser = webdriver.Chrome() browser = webdriver.Firefox()
訪問頁面
from selenium import webdriver browser = webdriver.Firefox() browser.get("http://www.baidu.com") print(browser.page_source) browser.close()
0x02 關於pytesseract
pytesseract最新版本0.1.6,網址:https://pypi.python.org/pypi/pytesseract
a、Python-tesseract是一個基於google's Tesseract-OCR的獨立封裝包;
b、Python-tesseract功能是識別圖片文件中文字,並作為返回參數返回識別結果;
c、Python-tesseract默認支持tiff、bmp格式圖片,只有在安裝PIL之后,才能支持jpeg、gif、png等其他圖片格式;
注意:tesserocr與pytesseract是Python的一個OCR識別庫,但其實是對tesseract做的一層Python API封裝,pytesseract是Google的Tesseract-OCR引擎包裝器;所以它們的核心是tesseract,因此在安裝tesserocr之前,我們需要先安裝tesseract
pytesseract安裝 && tesseract安裝
下載tesseract:https://digi.bib.uni-mannheim.de/tesseract/tesseract-ocr-w64-setup-v4.0.0-beta.1.20180414.exe
然后雙擊程序安裝即可,可以勾選Additional language data(download)選項來安裝OCR識別支持的語言包,但下載語言包實在是慢,我們可以直接從https://github.com/tesseract-ocr/tessdata下載zip的語言包壓縮文件,解壓后將tessdata-master中的文件復制到Tesseract的安裝目錄C:\Program Files (x86)\Tesseract-OCR\tessdata目錄下,最后我們配置下環境變量,我們將C:\Program Files (x86)\Tesseract-OCR添加到環境變量中
在測試之前先了解下tesseract的命令程序格式:
tesseract imagename outputbase [-l lang]
imagename指定圖片名稱,outputbase指定輸出文件名,-l指定識別的語言
#顯示安裝的語言包 tesseract --list-langs #顯示幫助 tesseract --help tesseract --help-extra tesseract --version
#使用一張圖片測試,成功識別字符串 tesseract image.png result -l eng |type result.txt
由於tesserocr在windows環境下會出現各種不兼容問題,並且與pycharm虛擬環境不兼容等問題,所以在windows系統環境下,選擇pytesseract模塊進行安裝,如果實在要安裝請使用whl文件安裝或者使用conda安裝
pip install pytesseract
如果在pytesseract運行是找不到tesseract解釋器,這種情況一般是在虛擬環境下會發生,我們需要將tesseract-OCR的執行文件tesseract.ext配置到windows系統中的PATH環境中,或者修改pytesseract.py文件,將其中的“tesseract_cmd”字段指定為tesseract.exe的完整路徑即可
測試識別功能:
import pytesseract from PIL import Image im=Image.open('image.png') print(pytesseract.image_to_string(im))
tesserocr與pytesseract模塊的使用
(1)tesserocr的使用
#從文件識別圖像字符 In [7]: tesserocr.file_to_text('image.png') Out[7]: 'Python3WebSpider\n\n' #查看tesseract已安裝的語言包 In [8]: tesserocr.get_languages() Out[8]: ('/usr/share/tesseract/tessdata/', ['eng']) #從圖片數據識別圖像字符 In [9]: tesserocr.image_to_text(im) Out[9]: 'Python3WebSpider\n\n' #查看版本信息 In [10]: tesserocr.tesseract_version() Out[10]: 'tesseract 3.04.00\n leptonica-1.72\n libgif 4.1.6(?) : libjpeg 6b (libjpeg-turbo 1.2.90) : libpng 1.5.13 : libtiff 4.0.3 : zlib 1.2.7 : libwebp 0.3.0\n'
(2)pytesseract使用
功能:
- get_tesseract_version 返回系統中安裝的Tesseract版本。
- image_to_string 將圖像上的Tesseract OCR運行結果返回到字符串
- image_to_boxes 返回包含已識別字符及其框邊界的結果
- image_to_data 返回包含框邊界,置信度和其他信息的結果。需要Tesseract 3.05+。有關更多信息,請查看Tesseract TSV文檔
- image_to_osd 返回包含有關方向和腳本檢測的信息的結果。
參數:
image_to_data(image, lang=None, config='', nice=0, output_type=Output.STRING)
- image object 圖像對象
- lang String,Tesseract 語言代碼字符串
- config String 任何其他配置為字符串,例如:
config='--psm 6'
- nice Integer 修改Tesseract運行的處理器優先級。Windows不支持。尼斯調整了類似unix的流程的優點。
- output_type 類屬性,指定輸出的類型,默認為
string
。有關所有支持類型的完整列表,請檢查pytesseract.Output類的定義。
from PIL import Image import pytesseract #如果PATH中沒有tesseract可執行文件,請指定tesseract路徑 pytesseract.pytesseract.tesseract_cmd='C:\Program Files (x86)\Tesseract-OCR\\tesseract.exe' #打印識別的圖像的字符串 print(pytesseract.image_to_string(Image.open('test.png'))) #指定語言識別圖像字符串,eng為英語 print(pytesseract.image_to_string(Image.open('test-european.jpg'), lang='eng')) #獲取圖像邊界框 print(pytesseract.image_to_boxes(Image.open('test.png'))) #獲取包含邊界框,置信度,行和頁碼的詳細數據 print(pytesseract.image_to_data(Image.open('test.png'))) #獲取方向和腳本檢測 print(pytesseract.image_to_osd(Image.open('test.png'))
圖像識別簡單應用
一般圖像處理驗證,需要通過對圖像進行灰度處理、二值化后增加圖像文字的辨識度,下面是一個簡單的對圖像驗證碼識別處理,如遇到復雜點的圖像驗證碼如中間帶多條同等大小划線的驗證碼需要對文字進行喬正切割等操作,但它的識別度也只有百分之30左右,所以得另外想別的辦法來繞過驗證
from PIL import Image import pytesseract im = Image.open('66.png') #二值化圖像傳入圖像和閾值 def erzhihua(image,threshold): ''':type image:Image.Image''' image=image.convert('L') table=[] for i in range(256): if i < threshold: table.append(0) else: table.append(1) return image.point(table,'1') image=erzhihua(im,127) image.show() result=pytesseract.image_to_string(image,lang='eng') print(result)
模擬自動識別驗證碼登陸:
#!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2018/7/13 8:58 # @Author : Py.qi # @File : login.py # @Software: PyCharm from selenium import webdriver from selenium.common.exceptions import TimeoutException,WebDriverException from selenium.webdriver.common.by import By from selenium.webdriver.common.keys import Keys from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.remote.webelement import WebElement from io import BytesIO from PIL import Image import pytesseract import time user='zhang' password='123' url='http://10.0.0.200' driver=webdriver.Chrome() wait=WebDriverWait(driver,10) #識別驗證碼 def acker(content): im_erzhihua=erzhihua(content,127) result=pytesseract.image_to_string(im_erzhihua,lang='eng') return result #驗證碼二值化 def erzhihua(image,threshold): ''':type image:Image.Image''' image=image.convert('L') table=[] for i in range(256): if i < threshold: table.append(0) else: table.append(1) return image.point(table,'1') #自動登陸 def login(): try: driver.get(url) #獲取用戶輸入框 input=wait.until(EC.presence_of_element_located((By.CSS_SELECTOR,'#loginname'))) #type:WebElement input.clear() #發送用戶名 input.send_keys(user) #獲取密碼框 inpass=wait.until(EC.presence_of_element_located((By.CSS_SELECTOR,'#password'))) #type:WebElement inpass.clear() #發送密碼 inpass.send_keys(password) #獲取驗證輸入框 yanzheng=wait.until(EC.presence_of_element_located((By.CSS_SELECTOR,'#code'))) #type:WebElement #獲取驗證碼在畫布中的位置 codeimg=wait.until(EC.presence_of_element_located((By.CSS_SELECTOR,'#codeImg'))) #type:WebElement image_location = codeimg.location #截取頁面圖像並截取掩碼碼區域圖像 image=driver.get_screenshot_as_png() im=Image.open(BytesIO(image)) imag_code=im.crop((image_location['x'],image_location['y'],488,473)) #輸入驗證碼並登陸 yanzheng.clear() yanzheng.send_keys(acker(imag_code)) time.sleep(2) yanzheng.send_keys(Keys.ENTER) except TimeoutException as e: print('timeout:',e) except WebDriverException as e: print('webdriver error:',e) if __name__ == '__main__': login()