Python在日志中隱藏明文密碼的方法
Python如何在日志中隱藏明文密碼
前言
在項(xiàng)目開發(fā)中,有的時(shí)候會遇到一些安全需求,用以提升程序整體的安全性,提高外來非法攻擊的門檻,而在日志中隱藏明文密碼打印便是最典型的安全需求之一。
在Python中,明文密碼往往發(fā)生于命令執(zhí)行參數(shù)、debug日志、依賴庫打印等場景中。對于程序自身的明文密碼打印,很輕易地就能通過修改相應(yīng)代碼行的方式修復(fù),而對于非程序自身打印,比如依賴庫、外部命令等,則比較棘手,無法通過直接修改代碼的方式解決。其實(shí),在Python中,logging
日志模塊提供了一些自定義方法以過濾特定字符串,絕大多數(shù)的Python程序均使用logging
模塊作為其日志記錄系統(tǒng),如果開發(fā)者已經(jīng)得知相關(guān)明文密碼打印的規(guī)則,且使用logging
模塊記錄日志,那么使用在logging
模塊中過濾特定字符串的方法不失為一個(gè)很好的選擇。
概念
logging
日志模塊是python的一個(gè)內(nèi)置模塊,該模塊定義了一些函數(shù)和類,為上層應(yīng)用程序或庫實(shí)現(xiàn)了一個(gè)強(qiáng)大而又靈活的日志記錄系統(tǒng)。
logging模塊將日志的處理分為四個(gè)層次,分別是:
- logger:logger向上層應(yīng)用程序暴露接口,程序通過調(diào)用logger打印日志,比如logger.info,logger.error等等;
- handler:handler用于將logger創(chuàng)建的日志記錄輸出至適合的目的地,比如標(biāo)準(zhǔn)輸出、錯(cuò)誤、文件等;
- filter:filter對如何將日志記錄輸出提供了更細(xì)粒度的控制;
- formatter:formatter指定了最終日志記錄輸出的格式。
如上,filter以及formatter層次均提供了對日志行為擴(kuò)展的手段,針對明文密碼打印問題,我們可以通過自定義filter或者formatter,使用特定規(guī)則過濾明文密碼字段的方式實(shí)現(xiàn)。
LogRecord
LogRecord是日志的基本單元,每次應(yīng)用程序調(diào)用Logger打印日志時(shí),logging模塊都會自動創(chuàng)建一個(gè)LogRecord實(shí)例,其記錄了日志文本、參數(shù)、模塊、行數(shù)乃至進(jìn)程ID、線程ID等等有用的信息。
>>> type(record) <class 'logging.LogRecord'> >>> record.msg 'password=123456 %s %s' >>> record.args ('1', '2') >>> record.created 1697184354.6492243 >>> record.levelname 'INFO' >>> record.name '__main__' >>> record.process 200
上面列出了一些LogRecord對象的屬性,這些屬性大部分也同樣是最后格式化日志輸出的參數(shù)。
filter
filter一般用作匹配并過濾部分日志,判斷匹配條件的日志是否允許打印,它提供了一個(gè)filter方法,使用布爾值作為返回值,如果返回true則表示允許打印,否則表示不允許。
filter方法以LogRecord作為參數(shù),這也表示除了過濾指定日志的功能以外,也能夠?qū)θ罩咀龈?xì)的控制。
class Filter(object): """ Filter instances are used to perform arbitrary filtering of LogRecords. """ def filter(self, record: LogRecord) -> bool: """ Determine if the specified record is to be logged. Returns True if the record should be logged, or False otherwise. If deemed appropriate, the record may be modified in-place. """
formatter
formatter負(fù)責(zé)將LogRecord轉(zhuǎn)化為最終的輸出字符串,它主要是使用args來渲染msg,除此之外,如果LogRecord包含異常堆棧,那么也會打印出來。
formatter方法以LogRecord作為參數(shù),并返回渲染處理后的字符串,當(dāng)自定義formatter類時(shí),我們能夠既能夠處理渲染前的LogRecord,也能修改渲染后的字符串。
class Formatter(object): """ Formatter instances are used to convert a LogRecord to text. """ def format(self, record: LogRecord) -> str: """ Format the specified record as text. The record's attribute dictionary is used as the operand to a string formatting operation which yields the returned string. Before formatting the dictionary, a couple of preparatory steps are carried out. The message attribute of the record is computed using LogRecord.getMessage(). If the formatting string uses the time (as determined by a call to usesTime(), formatTime() is called to format the event time. If there is exception information, it is formatted using formatException() and appended to the message. """
使用formatter實(shí)現(xiàn)明文密碼隱藏
import re import logging import logging.config # 自定義formatter類 class SensitiveFormatter(logging.Formatter): """Formatter that removes sensitive information in urls.""" @staticmethod def _mask_passwd(s) -> str: return re.sub(r'(?<=password=)\S+', r'***', s) def format(self, record) -> str: s = super().format(record) return self._mask_passwd(s) LOGGING_CONFIG = { "version": 1, "formatters": { "default": { "()": SensitiveFormatter, "format": "%(asctime)s - %(name)s - %(levelname)s - %(message)s", } }, "handlers": { "console": { "class": "logging.StreamHandler", "formatter": "default", "stream": "ext://sys.stdout" }, }, "loggers": {}, "root": { "level": "DEBUG", "handlers": [ "console", ] } } logging.config.dictConfig(LOGGING_CONFIG) LOG = logging.getLogger(__name__) LOG.info('password=123456') # 2023-10-13 16:58:50,443 - __main__ - INFO - password=***
使用filter實(shí)現(xiàn)明文密碼隱藏
import re import logging import logging.config # 自定義filter類 class SensitiveFilter(logging.Filter): def __init__(self, patterns): super().__init__() self._patterns = patterns def _mask(self, msg): if not isinstance(msg, str): return msg for pattern in self._patterns: msg = re.sub(pattern, r'***', msg) return msg def filter(self, record): record.msg = self._mask(record.msg) if isinstance(record.args, dict): for k in record.args.keys(): record.args[k] = self._mask(record.args[k]) elif isinstance(record.args, tuple): record.args = tuple(self._mask(arg) for arg in record.args) return super().filter(record) LOGGING_CONFIG = { "version": 1, "filters": { "default": { "()": SensitiveFilter, "patterns": [ r'(?<=password=)\S+', ], }, }, "formatters": { "default": { "format": "%(asctime)s - %(name)s - %(levelname)s - %(message)s", } }, "handlers": { "console": { "class": "logging.StreamHandler", "formatter": "default", "filters": [ "default", ], "stream": "ext://sys.stdout" }, }, "loggers": {}, "root": { "level": "DEBUG", "handlers": [ "console", ] } } logging.config.dictConfig(LOGGING_CONFIG) LOG = logging.getLogger(__name__) LOG.info('password=123456') # 2023-10-13 16:59:22,545 - __main__ - INFO - password=***
附錄
Hiding Sensitive Data from Logs with Python (relaxdiego.com)
logging — Logging facility for Python — Python 3.12.0 documentation
到此這篇關(guān)于Python如何在日志中隱藏明文密碼的文章就介紹到這了,更多相關(guān)Python日志中隱藏明文密碼內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python自定義函數(shù)的創(chuàng)建、調(diào)用和函數(shù)的參數(shù)詳解
這篇文章主要介紹了Python自定義函數(shù)的創(chuàng)建、調(diào)用和函數(shù)的參數(shù)、變量作用域等常見問題,需要的朋友可以參考下2014-03-03Python爬蟲JSON及JSONPath運(yùn)行原理詳解
這篇文章主要介紹了Python爬蟲JSON及JSONPath運(yùn)行原理詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06python3新特性函數(shù)注釋Function Annotations用法分析
這篇文章主要介紹了python3新特性函數(shù)注釋Function Annotations用法,結(jié)合實(shí)例形式分析了Python3函數(shù)注釋的定義方法與使用技巧,需要的朋友可以參考下2016-07-07Python實(shí)現(xiàn)的KMeans聚類算法實(shí)例分析
這篇文章主要介紹了Python實(shí)現(xiàn)的KMeans聚類算法,結(jié)合實(shí)例形式較為詳細(xì)的分析了KMeans聚類算法概念、原理、定義及使用相關(guān)操作技巧,需要的朋友可以參考下2018-12-12Python pandas進(jìn)行數(shù)據(jù)預(yù)處理的實(shí)現(xiàn)
本案例通過使用pandas庫對電子商務(wù)客戶數(shù)據(jù)進(jìn)行數(shù)據(jù)預(yù)處理,包括數(shù)據(jù)導(dǎo)入、查看、缺失值處理等處理,具有一定的參考價(jià)值,感興趣的可以了解一下2025-01-01神經(jīng)網(wǎng)絡(luò)python源碼分享
這篇文章主要介紹了神經(jīng)網(wǎng)絡(luò)python源碼分享,具有一定借鑒價(jià)值,需要的朋友可以參考下。2017-12-12詳解四種Python中基本形態(tài)學(xué)濾波的實(shí)現(xiàn)
最基礎(chǔ)的形態(tài)學(xué)操作有四個(gè),分別是腐蝕、膨脹、開計(jì)算和閉計(jì)算。這篇文章主要介紹了這四種形態(tài)學(xué)濾波的實(shí)現(xiàn),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-04-04