使用Python自動化自定義字體混淆信息的方法實例
注意:本示例僅供學(xué)習(xí)參考~
混淆原理
出于某種原因,明文信息通過自定義字體進行渲染,達到混淆目的。
舉個例子:
網(wǎng)頁源碼 <p>123</p>
在正常字體的渲染下,瀏覽者看到的是 123 這 3 個數(shù)字。
如果創(chuàng)建一種自定義字體,把 1 渲染成 5,那么瀏覽者看到的便是 523 這 3 個數(shù)字。
這樣便達到混淆信息的效果,常見于對付爬蟲之類的自動化工具。
運行以下代碼
# -*- coding: utf-8 -*- 本例采用 tesseract OCR 引擎,根據(jù)字體文件自動生成密文與明文的字符映射表,實現(xiàn)解密功能。 @author: 李毅 from tesserocr import PyTessBaseAPI, PSM from PIL import Image, ImageDraw, ImageFont from fontTools.ttLib import TTFont import string class OCR(object): default_config = { # ocr engine 'data_path': None, 'lang': 'chi_sim', 'white_list': None, 'black_list': None, # image 'font': None, 'image_size': (60, 60), 'font_size': 30, 'text_offset': (15, 15), } def __init__(self, config={}): c = dict(self.default_config) c.update(config) self.api = PyTessBaseAPI(path=c['data_path'], lang=c['lang'], psm=PSM.SINGLE_CHAR) self.img = Image.new('RGB', c['image_size'], color='white') self.draw = ImageDraw.Draw(self.img) self.font = ImageFont.truetype(c['font'], size=c['font_size']) self.text_offset = c['text_offset'] if c['white_list']: self.api.SetVariable('tessedit_char_whitelist', c['white_list']) if c['black_list']: self.api.SetVariable('tessedit_char_blacklist', c['black_list']) self.font_tool = TTFont(c['font']) self.empty_char = self._predict_empty_char() def _predict_empty_char(self): self.api.SetImage(self.img) return self.api.GetUTF8Text().strip() def is_char_in_font(self, char): for t in self.font_tool['cmap'].tables: if t.isUnicode(): if ord(char) in t.cmap: return True return False def predict(self, char): ''' 返回轉(zhuǎn)換后的字符,或空串'' ''' if not self.is_char_in_font(char): return char # 若字體無法渲染該字符,則原樣返回。此處可酌情移除。 self.img.paste('white', (0, 0, self.img.size[0], self.img.size[1])) self.draw.text(self.text_offset, char, fill='black', font=self.font) self.api.SetImage(self.img) c2 = self.api.GetUTF8Text().strip() if c2 == self.empty_char: return '' # 某些字符可能渲染成空白,此時返回空串。 return c2 class Decoder(object): def __init__(self, data_path, font): self.cache = {} # 緩存已知的映射關(guān)系。 OCR.default_config.update(dict(data_path=data_path, font=font)) self.ocr_digit = OCR(dict( lang='eng', white_list=string.digits, black_list=string.ascii_letters, )) self.ocr_letter = OCR(dict( lang='eng', black_list=string.digits, white_list=string.ascii_letters, )) self.ocr_other = OCR() def decode(self, char): if char not in self.cache: c2 = self._decode_when_cache_miss(char) self.cache[char] = c2 or char return self.cache[char] def _decode_when_cache_miss(self, char): ocr = self.ocr_other if char in string.digits: ocr = self.ocr_digit elif char in string.ascii_letters: ocr = self.ocr_letter return ocr.predict(char) if __name__ == '__main__': s = '''你好,青劃長務(wù), 8175-13-79''' d = Decoder('tessdata/', 'a.ttf') print(''.join(map(d.decode, s)))
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,謝謝大家對腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請查看下面相關(guān)鏈接
相關(guān)文章
一行代碼解決動態(tài)執(zhí)行Python函數(shù)方法實例
這篇文章主要為大家介紹了如何用一行代碼解決動態(tài)執(zhí)行Python函數(shù)方法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-12-12在PyTorch中實現(xiàn)可解釋的神經(jīng)網(wǎng)絡(luò)模型的方法詳解
這篇文章主要為大家介紹在PyTorch如何中實現(xiàn)可解釋的神經(jīng)網(wǎng)絡(luò)模型,并為您提供使用簡單的 PyTorch 接口實現(xiàn)最先進的基于概念的模型的工具,需要的朋友可以參考下2023-06-06