windows篡改腳本提醒

? 功能簡介


該監控系統具備如下主要功能:
📁 目錄監控?? ?實時監聽指定主目錄及其所有子目錄內文件的變動情況。
🔒 文件哈希校驗?? ?對文件內容生成 SHA256 哈希,確保變更檢測基于內容而非時間戳。
🚫 排除機制?? ?支持設置需排除的子目錄或特定文件,避免頻繁改動目錄帶來的干擾。
🧾 日志記錄?? ?所有非排除目錄中的新增或被篡改的文件將自動寫入 update.txt 日志文件中。
🧠 基線建立?? ?系統首次運行時自動建立哈希基線作為“可信狀態”,便于后續變更比對

python代碼

import os
import hashlib
import json
import time
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler# === 配置路徑(可改為環境變量支持更靈活)===
MONITOR_FOLDER = "D:\\WorkRoot\\xxxx\\fms"
HASH_DB_FILE = "D:\\hash_baseline.json"
LOG_FILE = "D:\\update.txt"
#D:\WorkRoot\xxxx\fms\UploadFiles
# ? 排除監控的相對路徑(相對于 MONITOR_FOLDER)
EXCLUDE_PATHS = ["UploadFiles","areweb\\database"
]# === 工具函數 ===
def is_excluded(path):rel_path = os.path.relpath(path, MONITOR_FOLDER)for ex in EXCLUDE_PATHS:if rel_path == ex or rel_path.startswith(ex + os.sep):return Truereturn Falsedef calculate_hash(file_path):hasher = hashlib.sha256()try:with open(file_path, 'rb') as f:while chunk := f.read(8192):hasher.update(chunk)return hasher.hexdigest()except Exception:return Nonedef log_change(message):timestamp = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())with open(LOG_FILE, 'a', encoding='utf-8') as f:f.write(f"[{timestamp}] {message}\n")def build_baseline():hash_dict = {}for root, _, files in os.walk(MONITOR_FOLDER):for file in files:full_path = os.path.join(root, file)if is_excluded(full_path):continuerel_path = os.path.relpath(full_path, MONITOR_FOLDER)file_hash = calculate_hash(full_path)if file_hash:hash_dict[rel_path] = file_hashwith open(HASH_DB_FILE, 'w') as f:json.dump(hash_dict, f, indent=4)print("? 哈希基線已建立")def load_baseline():if not os.path.exists(HASH_DB_FILE):return {}with open(HASH_DB_FILE, 'r') as f:return json.load(f)# === 文件監控事件處理 ===
class FileChangeHandler(FileSystemEventHandler):def __init__(self, baseline):self.baseline = baselinedef on_modified(self, event):if event.is_directory or is_excluded(event.src_path):returnrel_path = os.path.relpath(event.src_path, MONITOR_FOLDER)current_hash = calculate_hash(event.src_path)old_hash = self.baseline.get(rel_path)if old_hash and current_hash and current_hash != old_hash:log_change(f"🚨 文件被篡改:{rel_path}")def on_created(self, event):if event.is_directory or is_excluded(event.src_path):returnrel_path = os.path.relpath(event.src_path, MONITOR_FOLDER)log_change(f"🆕 新增文件:{rel_path}")def on_deleted(self, event):if event.is_directory or is_excluded(event.src_path):returnrel_path = os.path.relpath(event.src_path, MONITOR_FOLDER)if rel_path in self.baseline:log_change(f"? 文件被刪除:{rel_path}")# === 啟動監控器 ===
def start_monitor():if not os.path.exists(HASH_DB_FILE):build_baseline()baseline = load_baseline()print(f"🚀 正在監控文件夾:{MONITOR_FOLDER}")print(f"🚫 排除路徑:{EXCLUDE_PATHS}")print(f"📄 日志寫入文件:{LOG_FILE}")event_handler = FileChangeHandler(baseline)observer = Observer()observer.schedule(event_handler, MONITOR_FOLDER, recursive=True)observer.start()try:while True:time.sleep(1)except KeyboardInterrupt:observer.stop()observer.join()# === 程序入口 ===
if __name__ == "__main__":start_monitor()

打包exe程序??--icon=logo.ico是logo標識,可刪除

?pyinstaller --noconsole --onefile --icon=logo.ico 篡改腳本.py

優化:

可配置路徑config.json讀取路徑地址

{"monitor_folder": "D:\\WX\\fms_bak","hash_db_file": "D:\\hash_baseline.json","log_file": "D:\\update.txt","exclude_paths": ["areweb"]
}

?python代碼

import os
import hashlib
import json
import time
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler# === 加載配置 ===
CONFIG_FILE = "C:\\config.json"def load_config():with open(CONFIG_FILE, 'r', encoding='utf-8') as f:return json.load(f)config = load_config()
MONITOR_FOLDER = config['monitor_folder']
HASH_DB_FILE = config['hash_db_file']
LOG_FILE = config['log_file']
EXCLUDE_PATHS = config['exclude_paths']# === 工具函數 ===
def is_excluded(path):rel_path = os.path.relpath(path, MONITOR_FOLDER)for ex in EXCLUDE_PATHS:if rel_path == ex or rel_path.startswith(ex + os.sep):return Truereturn Falsedef calculate_hash(file_path):hasher = hashlib.sha256()try:with open(file_path, 'rb') as f:while chunk := f.read(8192):hasher.update(chunk)return hasher.hexdigest()except Exception:return Nonedef log_change(message):timestamp = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())with open(LOG_FILE, 'a', encoding='utf-8') as f:f.write(f"[{timestamp}] {message}\n")def build_baseline():hash_dict = {}for root, _, files in os.walk(MONITOR_FOLDER):for file in files:full_path = os.path.join(root, file)if is_excluded(full_path):continuerel_path = os.path.relpath(full_path, MONITOR_FOLDER)file_hash = calculate_hash(full_path)if file_hash:hash_dict[rel_path] = file_hashwith open(HASH_DB_FILE, 'w') as f:json.dump(hash_dict, f, indent=4)print("? 哈希基線已建立")def load_baseline():if not os.path.exists(HASH_DB_FILE):return {}with open(HASH_DB_FILE, 'r') as f:return json.load(f)# === 文件監控處理器 ===
class FileChangeHandler(FileSystemEventHandler):def __init__(self, baseline):self.baseline = baselinedef on_modified(self, event):if event.is_directory or is_excluded(event.src_path):returnrel_path = os.path.relpath(event.src_path, MONITOR_FOLDER)current_hash = calculate_hash(event.src_path)old_hash = self.baseline.get(rel_path)if old_hash and current_hash and current_hash != old_hash:log_change(f"🚨 文件被篡改:{rel_path}")def on_created(self, event):if event.is_directory or is_excluded(event.src_path):returnrel_path = os.path.relpath(event.src_path, MONITOR_FOLDER)log_change(f"🆕 新增文件:{rel_path}")def on_deleted(self, event):if event.is_directory or is_excluded(event.src_path):returnrel_path = os.path.relpath(event.src_path, MONITOR_FOLDER)log_change(f"? 文件被刪除:{rel_path}")# === 啟動監控器 ===
def start_monitor():if not os.path.exists(HASH_DB_FILE):build_baseline()baseline = load_baseline()print(f"🚀 正在監控文件夾:{MONITOR_FOLDER}")print(f"🚫 排除路徑:{EXCLUDE_PATHS}")print(f"📄 日志寫入文件:{LOG_FILE}")event_handler = FileChangeHandler(baseline)observer = Observer()observer.schedule(event_handler, MONITOR_FOLDER, recursive=True)observer.start()try:while True:time.sleep(1)except KeyboardInterrupt:observer.stop()observer.join()# === 程序入口 ===
if __name__ == "__main__":start_monitor()

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/pingmian/82039.shtml
繁體地址,請注明出處:http://hk.pswp.cn/pingmian/82039.shtml
英文地址,請注明出處:http://en.pswp.cn/pingmian/82039.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

文章記單詞 | 第102篇(六級)

一,單詞釋義 apologize /??p?l?d?a?z/ v. 道歉;認錯discharge /d?s?t?ɑ?rd?/ v./n. 排出;釋放;解雇; dischargequiver /?kw?v?r/ v./n. 顫抖;抖動;箭筒plantation /pln?te??…

【DCGMI專題1】---DCGMI 在 Ubuntu 22.04 上的深度安裝指南與原理分析(含架構圖解)

目錄 一、DCGMI 概述與應用場景 二、Ubuntu 22.04 系統準備 2.1 系統要求 2.2 環境清理(可選) 三、DCGMI 安裝步驟(詳細圖解) 3.1 安裝流程總覽 3.2 分步操作指南 3.2.1 系統更新與依賴安裝 3.2.2 添加 NVIDIA 官方倉庫 3.2.3 安裝數據中心驅動與 DCGM 3.2.4 服務…

主成分分析(PCA)法例題——給定協方差矩陣

已知樣本集合的協方差矩陣為 C x 1 10 [ 3 1 1 1 3 ? 1 1 ? 1 3 ] {\bm C}_x \frac{1}{10} \begin{bmatrix} 3 & 1 & 1 \\ 1 & 3 & -1 \\ 1 & -1 & 3 \end{bmatrix} Cx?101? ?311?13?1?1?13? ? 使用PCA方法將樣本向量降到二維 。 求解 計…

uni-app(4):js語法、css語法

1 js語法 uni-app的js API由標準ECMAScript的js API 和 uni 擴展 API 這兩部分組成。標準ECMAScript的js僅是最基礎的js。瀏覽器基于它擴展了window、document、navigator等對象。小程序也基于標準js擴展了各種wx.xx、my.xx、swan.xx的API。node也擴展了fs等模塊。uni-app基于E…

Idea 配合 devtools 依賴 實現熱部署

核心依賴 <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId><scope>runtime</scope><optional>true</optional></dependency> yaml配置 spring: #…

leetcode513.找樹左下角的值:遞歸深度優先搜索中的最左節點追蹤之道

一、題目本質與核心訴求解析 在二叉樹算法問題中&#xff0c;"找樹左下角的值"是一個典型的結合深度與位置判斷的問題。題目要求我們找到二叉樹中最深層最左邊的節點值&#xff0c;這里的"左下角"有兩個關鍵限定&#xff1a; 深度優先&#xff1a;必須是…

Python入門手冊:Python基礎語法

Python是一種簡潔、易讀且功能強大的編程語言&#xff0c;非常適合初學者入門。無論你是編程新手&#xff0c;還是有一定編程基礎但想學習Python的開發者&#xff0c;掌握Python的基礎語法都是邁向高效編程的第一步。本文將詳細介紹Python的基本語法&#xff0c;包括變量和數據…

postgresql 常用參數配置

#01 - Connection-Authentication 優化點&#xff1a; listen_addresses 0.0.0.0 建議&#xff1a;生產環境應限制為具體IP&#xff08;如 192.168.1.0/24,127.0.0.1&#xff09;&#xff0c;避免暴露到公網。 ssl off 建議&#xff1a;啟用SSL&#xff08;ssl on&#xf…

POI模板生成EXCEL 64000 style in a .xlsx Workbook

業務場景&#xff1a; 項目需要生成多個EXCEL表格&#xff0c;每個表格根據數據列表的大小動態增加Excel的行數&#xff0c;要保證新插入行的樣式與模板完全一致 考慮使用以下方法保證樣式的統一 cloneStyleFrom(templateStyle); 但是由于數據量比較大&#xff0c;拋出如下的…

HJ106 字符逆序【牛客網】

文章目錄 零、原題鏈接一、題目描述二、測試用例三、解題思路四、參考代碼 零、原題鏈接 HJ106 字符逆序 一、題目描述 二、測試用例 三、解題思路 基本思路&#xff1a; ??考慮到可能會有多個空格&#xff0c;使用使用 getline 函數直接讀取一行。 ??如果可以直接打印的…

CI/CD的演進之路

CI/CD的演進之路 一、CI/CD的成長演變 早期起源與初步實踐&#xff1a;CI/CD的概念可以追溯到軟件開發的早期階段&#xff0c;但真正開始受到關注是在敏捷開發方法興起之后。在傳統的瀑布模型開發模式下&#xff0c;軟件開發周期長、發布頻率低&#xff0c;更新往往需要數月甚…

制作一款打飛機游戲55:擴散

子彈模式 ?瘋狂的子彈地獄?&#xff1a; 嘿&#xff0c;伙計們&#xff0c;今天我們要創造一些令人印象深刻的子彈模式。這就是所謂的“子彈地獄”&#xff01; ?問題與挑戰?&#xff1a; 在之前的開發中&#xff0c;我們遇到了一些問題。特別是關于如何處理子彈的角度問題…

Vortex GPGPU的github流程跑通與功能模塊波形探索(三)

文章目錄 前言一、./build/ci下的文件結構二、基于驅動進行仿真過程牽扯的文件2.1 blackbox.sh文件2.2 demo文件2.3 額外牽扯到的ramulator2.3.1 ramulator簡單介紹2.3.2 ramulator使用方法2.3.3 ramulator的輸出2.3.4 ramulator的復現2.3.4.1 調試與驗證&#xff08;第 4.1 節…

公有云AWS基礎架構與核心服務:從概念到實踐

??「炎碼工坊」技術彈藥已裝填! 點擊關注 → 解鎖工業級干貨【工具實測|項目避坑|源碼燃燒指南】 (初學者技術專欄) 一、基礎概念 定義:AWS(Amazon Web Services)是亞馬遜提供的云計算服務,包含計算、存儲、網絡、數據庫等核心能力,通過全球數據中心為用戶提供靈活…

wsl2 不能聯網

wsl2 安裝后用 wifi 共享是能聯網&#xff0c;問題出在公司網絡限制 wsl2 IP 訪問網絡&#xff0c;但是主機可以上網。 解決辦法&#xff0c;在主機用 nginx 設置代理&#xff0c;可能需要開端口權限 server {listen 9000;server_name localhost;location /ubuntu/ {#…

HarmonyOS鴻蒙應用規格開發指南

在鴻蒙生態系統中&#xff0c;應用規格是確保應用符合系統要求的基礎。本文將深入探討鴻蒙應用的規格開發實踐&#xff0c;幫助開發者打造符合規范的應用。 應用包結構規范 1. 基本配置要求 包結構規范 符合規范的應用包結構正確的HAP配置文件完整的應用信息 示例配置&…

異步日志分析:MongoDB與FastAPI的高效存儲揭秘

title: 異步日志分析:MongoDB與FastAPI的高效存儲揭秘 date: 2025/05/22 17:04:56 updated: 2025/05/22 17:04:56 author: cmdragon excerpt: MongoDB與FastAPI集成構建日志分析系統,通過Motor驅動實現異步操作,提升數據處理效率。使用Pydantic進行數據驗證,配置環境變量…

[原理理解] 超分使用到的RAM模型和LLAVA模型

文章目錄 前述RAM 模型介紹LLAVA 模型介紹 前述 最近在研究基于diffusion的超分模型&#xff0c;發現基本都文本編碼的時候都需要用到RAM模型或者LLAVA模型&#xff0c;兩個有什么區別呢&#xff1f; RAM 模型介紹 RAM&#xff08;Recognize Anything Model&#xff09; 是用…

基于 SpringBoot + Vue 的海濱體育館管理系統設計與實現

一、項目概述 本項目是一套基于SpringBoot Vue技術棧開發的海濱體育館管理系統&#xff0c;旨在幫助管理者更高效地管理體育館的各項資源和活動&#xff0c;同時也為學生提供方便的借還器材、預約活動等功能。系統采用了前后端分離的架構&#xff0c;后端使用Spring Boot框架…

【時時三省】(C語言基礎)對被調用函數的聲明和函數原型

山不在高&#xff0c;有仙則名。水不在深&#xff0c;有龍則靈。 ----CSDN 時時三省 在一個函數中調用另一個函數&#xff08;即被調用函數&#xff09;需要具備如下條件 ( 1 )首先被調用的函數必須是已經定義的函數(是庫函數或用戶自己定義的函數)&#xff0c;但僅有這一條件…