Python在日志中隱藏明文密碼的方法
Python如何在日志中隱藏明文密碼
前言
在項目開發(fā)中,有的時候會遇到一些安全需求,用以提升程序整體的安全性,提高外來非法攻擊的門檻,而在日志中隱藏明文密碼打印便是最典型的安全需求之一。
在Python中,明文密碼往往發(fā)生于命令執(zhí)行參數(shù)、debug日志、依賴庫打印等場景中。對于程序自身的明文密碼打印,很輕易地就能通過修改相應代碼行的方式修復,而對于非程序自身打印,比如依賴庫、外部命令等,則比較棘手,無法通過直接修改代碼的方式解決。其實,在Python中,logging
日志模塊提供了一些自定義方法以過濾特定字符串,絕大多數(shù)的Python程序均使用logging
模塊作為其日志記錄系統(tǒng),如果開發(fā)者已經(jīng)得知相關明文密碼打印的規(guī)則,且使用logging
模塊記錄日志,那么使用在logging
模塊中過濾特定字符串的方法不失為一個很好的選擇。
概念
logging
日志模塊是python的一個內置模塊,該模塊定義了一些函數(shù)和類,為上層應用程序或庫實現(xiàn)了一個強大而又靈活的日志記錄系統(tǒng)。
logging模塊將日志的處理分為四個層次,分別是:
- logger:logger向上層應用程序暴露接口,程序通過調用logger打印日志,比如logger.info,logger.error等等;
- handler:handler用于將logger創(chuàng)建的日志記錄輸出至適合的目的地,比如標準輸出、錯誤、文件等;
- filter:filter對如何將日志記錄輸出提供了更細粒度的控制;
- formatter:formatter指定了最終日志記錄輸出的格式。
如上,filter以及formatter層次均提供了對日志行為擴展的手段,針對明文密碼打印問題,我們可以通過自定義filter或者formatter,使用特定規(guī)則過濾明文密碼字段的方式實現(xiàn)。
LogRecord
LogRecord是日志的基本單元,每次應用程序調用Logger打印日志時,logging模塊都會自動創(chuàng)建一個LogRecord實例,其記錄了日志文本、參數(shù)、模塊、行數(shù)乃至進程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一般用作匹配并過濾部分日志,判斷匹配條件的日志是否允許打印,它提供了一個filter方法,使用布爾值作為返回值,如果返回true則表示允許打印,否則表示不允許。
filter方法以LogRecord作為參數(shù),這也表示除了過濾指定日志的功能以外,也能夠對日志做更精細的控制。
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負責將LogRecord轉化為最終的輸出字符串,它主要是使用args來渲染msg,除此之外,如果LogRecord包含異常堆棧,那么也會打印出來。
formatter方法以LogRecord作為參數(shù),并返回渲染處理后的字符串,當自定義formatter類時,我們能夠既能夠處理渲染前的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實現(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實現(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
到此這篇關于Python如何在日志中隱藏明文密碼的文章就介紹到這了,更多相關Python日志中隱藏明文密碼內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Python自定義函數(shù)的創(chuàng)建、調用和函數(shù)的參數(shù)詳解
這篇文章主要介紹了Python自定義函數(shù)的創(chuàng)建、調用和函數(shù)的參數(shù)、變量作用域等常見問題,需要的朋友可以參考下2014-03-03python3新特性函數(shù)注釋Function Annotations用法分析
這篇文章主要介紹了python3新特性函數(shù)注釋Function Annotations用法,結合實例形式分析了Python3函數(shù)注釋的定義方法與使用技巧,需要的朋友可以參考下2016-07-07Python pandas進行數(shù)據(jù)預處理的實現(xiàn)
本案例通過使用pandas庫對電子商務客戶數(shù)據(jù)進行數(shù)據(jù)預處理,包括數(shù)據(jù)導入、查看、缺失值處理等處理,具有一定的參考價值,感興趣的可以了解一下2025-01-01詳解四種Python中基本形態(tài)學濾波的實現(xiàn)
最基礎的形態(tài)學操作有四個,分別是腐蝕、膨脹、開計算和閉計算。這篇文章主要介紹了這四種形態(tài)學濾波的實現(xiàn),感興趣的小伙伴可以跟隨小編一起學習一下2023-04-04