ZDNetChina中文社區(qū) » 網(wǎng)絡(luò)安全 » c#類,封裝了鍵盤,和鼠標(biāo)模擬,和內(nèi)存讀?。ㄉ昃?/div>
2007-11-19 01:38 yadianna521
c#類,封裝了鍵盤,和鼠標(biāo)模擬,和內(nèi)存讀?。ㄉ昃?/strong>
[原創(chuàng)]c#類,封裝了鍵盤,和鼠標(biāo)模擬,和內(nèi)存讀取 發(fā)一個(gè)自己寫的c#類,封裝了鍵盤,和鼠標(biāo)模擬,和內(nèi)存讀取 鍵盤和鼠標(biāo)模擬用了2種方式,一種是系統(tǒng)api,還有一種是winio 鼠標(biāo)模擬不是很好,建議不要用,外帶一個(gè)很簡(jiǎn)單的內(nèi)存讀取方法。 大家把類的功能多多修改,讓我們寫外掛更方便,呵呵。 我下面將開源 本主題包含附件: sf_200779104937.rar (28181bytes) key kk = new key(); // IntPtr a = new IntPtr(); kk.sendwinio(); kk.MykeyDown((int)key.VirtualKeys.VK_F1); System.Threading.Thread.Sleep(2000); kk.MykeyUp((int)key.VirtualKeys.VK_F1); 這是模擬鍵盤的調(diào)用方式 源碼: using System; using System.Collections.Generic; using System.Text; using System.Runtime.InteropServices; using System.Diagnostics; using System.Management; namespace sendkey { public class key { const uint PROCESS_ALL_ACCESS = 0x001F0FFF; const uint KEYEVENTF_EXTENDEDKEY = 0x1; const uint KEYEVENTF_KEYUP = 0x2; private readonly int MOUSEEVENTF_LEFTDOWN = 0x2; private readonly int MOUSEEVENTF_LEFTUP = 0x4; const uint KBC_KEY_CMD = 0x64; const uint KBC_KEY_DATA = 0x60; //得到窗體句柄的函數(shù),FindWindow函數(shù)用來返回符合指定的類名( ClassName )和窗口名( WindowTitle )的窗口句柄 [DllImport("user32.dll", CharSet = CharSet.Auto)] public static extern IntPtr FindWindow( string lpClassName, // pointer to class name string lpWindowName // pointer to window name ); [DllImport("user32.dll")] private static extern int GetWindowThreadProcessId(IntPtr id,int pid); [DllImport("kernel32.dll")] private static extern void CloseHandle ( uint hObject //Handle to object ); //讀取進(jìn)程內(nèi)存的函數(shù) [DllImport("kernel32.dll")] static extern bool ReadProcessMemory(uint hProcess, IntPtr lpBaseAddress, IntPtr lpBuffer, uint nSize, ref uint lpNumberOfBytesRead); //得到目標(biāo)進(jìn)程句柄的函數(shù) [DllImport("kernel32.dll")] public static extern uint OpenProcess(uint dwDesiredAccess, bool bInheritHandle, int dwProcessId); //鼠標(biāo)事件聲明 [DllImport("user32.dll")] static extern bool setcursorpos(int x, int y); [DllImport("user32.dll")] static extern void mouse_event(mouseeventflag flags, int dx, int dy, uint data, UIntPtr extrainfo); //鍵盤事件聲明 [DllImport("user32.dll")] static extern byte MapVirtualKey(byte wCode, int wMap); [DllImport("user32.dll")] static extern short GetKeyState(int nVirtKey); [DllImport("user32.dll")] static extern void keybd_event( byte bVk, byte bScan,uint dwFlags,uint dwExtraInfo); //鍵盤事件聲明winio [DllImport("winio.dll")] public static extern bool InitializeWinIo(); [DllImport("winio.dll")] public static extern bool GetPortVal(IntPtr wPortAddr, out int pdwPortVal, byte bSize); [DllImport("winio.dll")] public static extern bool SetPortVal(uint wPortAddr, IntPtr dwPortVal, byte bSize); [DllImport("winio.dll")] public static extern byte MapPhysToLin(byte pbPhysAddr, uint dwPhysSize, IntPtr PhysicalMemoryHandle); [DllImport("winio.dll")] public static extern bool UnmapPhysicalMemory(IntPtr PhysicalMemoryHandle, byte pbLinAddr); [DllImport("winio.dll")] public static extern bool GetPhysLong(IntPtr pbPhysAddr, byte pdwPhysVal); [DllImport("winio.dll")] public static extern bool SetPhysLong(IntPtr pbPhysAddr, byte dwPhysVal); [DllImport("winio.dll")] public static extern void ShutdownWinIo(); /// <summary> /// 獲取進(jìn)程pid /// </summary> /// <param name="name"></param> /// <returns></returns> private int pid(String name) { try { ObjectQuery oQuery = new ObjectQuery("select * from Win32_Process where Name='" + name + "'"); ManagementObjectSearcher oSearcher = new ManagementObjectSearcher(oQuery); ManagementObjectCollection oReturnCollection = oSearcher.Get(); string pid = ""; string cmdLine; StringBuilder sb = new StringBuilder(); foreach (ManagementObject oReturn in oReturnCollection) { pid = oReturn.GetPropertyvalue("ProcessId").ToString(); //cmdLine = (string)oReturn.GetPropertyvalue("CommandLine"); //string pattern = "-ap \"(.*)\""; //Regex regex = new Regex(pattern, RegexOptions.IgnoreCase); // Match match = regex.Match(cmdLine); //string appPoolName = match.Groups[1].ToString(); //sb.AppendFormat("W3WP.exe PID: {0} AppPoolId:{1}\r\n", pid, appPoolName); } return Convert.ToInt32(pid); } catch (Exception ss) { return 0; } } private int pid(IntPtr id) { int pid=0; pid=GetWindowThreadProcessId(id, pid); return 260; } /// <summary> /// 讀取內(nèi)存值 /// </summary> /// <param name="name">進(jìn)程id</param> /// <param name="dizhi">讀取的內(nèi)存地址</param> /// <returns></returns> //public String getread(String QEC,String EC, IntPtr dizhi, uint size) //{ // Byte bt = new Byte(); // IntPtr id=FindWindow(QEC, EC); // uint hProcess = OpenProcess(PROCESS_ALL_ACCESS, false, pid(id)); // IntPtr fanhui = new IntPtr(); // String gg = null; // if (hProcess == 0) // { // // gg = ReadProcessMemory(hProcess, dizhi, fanhui, size, 0); // // CloseHandle(hProcess); // } // return gg; //} public String getread(String jincheng, String EC, IntPtr dizhi, uint size) { byte[] vBuffer = new byte[4]; IntPtr vBytesAddress = Marshal.UnsafeAddrOfPinnedArrayElement(vBuffer, 0); // 得到緩沖區(qū)的地址 uint vNumberOfBytesRead = 0; Byte bt = new Byte(); //IntPtr id = FindWindow(QEC, EC); uint hProcess = OpenProcess(PROCESS_ALL_ACCESS, false, pid(jincheng)); //pid(0); IntPtr fanhui = new IntPtr(); String gg = null; //if (hProcess == 0) //{ if (ReadProcessMemory(hProcess, dizhi, vBytesAddress, (uint)vBuffer.Length, ref hProcess)) { CloseHandle(hProcess); } else { CloseHandle(hProcess); } // } int vInt = Marshal.ReadInt32(vBytesAddress); return vInt.ToString() ; } /// <summary> /// 獲取鍵盤狀態(tài) 2007-11-19 01:39 yadianna521
/// </summary>
/// <param name="Key"></param> /// <returns></returns> public bool GetState(VirtualKeys Key) { return (GetKeyState((int)Key) == 1); } /// <summary> /// 發(fā)送鍵盤事件 /// </summary> /// <returns></returns> public void Send(VirtualKeys Key, bool State) { if (State != GetState(Key)) { byte a= MapVirtualKey((byte)Key, 0); keybd_event((byte)Key, MapVirtualKey((byte)Key, 0), 0, 0); System.Threading.Thread.Sleep(1000); keybd_event((byte)Key, MapVirtualKey((byte)Key, 0), KEYEVENTF_KEYUP, 0); } } /// <summary> /// 初始化winio /// </summary> public void sendwinio() { if (InitializeWinIo()) { KBCWait4IBE(); } } private void KBCWait4IBE() //等待鍵盤緩沖區(qū)為空 { //int[] dwVal = new int[] { 0 }; int dwVal = 0; do { //這句表示從&H64端口讀取一個(gè)字節(jié)并把讀出的數(shù)據(jù)放到變量dwVal中 //GetPortVal函數(shù)的用法是GetPortVal 端口號(hào),存放讀出數(shù)據(jù)的變量,讀入的長(zhǎng)度 bool flag = GetPortVal((IntPtr)0x64, out dwVal, 1); } while ((dwVal & 0x2) > 0); } /// <summary> /// 模擬鍵盤標(biāo)按下 /// </summary> /// <param name="vKeyCoad"></param> public void MykeyDown(int vKeyCoad) { int btScancode = 0; btScancode = MapVirtualKey((byte)vKeyCoad, 0); // btScancode = vKeyCoad; KBCWait4IBE(); // '發(fā)送數(shù)據(jù)前應(yīng)該先等待鍵盤緩沖區(qū)為空 SetPortVal(KBC_KEY_CMD, (IntPtr)0xD2, 1);// '發(fā)送鍵盤寫入命令 //SetPortVal函數(shù)用于向端口寫入數(shù)據(jù),它的用法是SetPortVal 端口號(hào),欲寫入的數(shù)據(jù),寫入數(shù)據(jù)的長(zhǎng)度 KBCWait4IBE(); SetPortVal(KBC_KEY_DATA, (IntPtr)0xe2, 1);// '寫入按鍵信息,按下鍵 KBCWait4IBE(); // '發(fā)送數(shù)據(jù)前應(yīng)該先等待鍵盤緩沖區(qū)為空 SetPortVal(KBC_KEY_CMD, (IntPtr)0xD2, 1);// '發(fā)送鍵盤寫入命令 //SetPortVal函數(shù)用于向端口寫入數(shù)據(jù),它的用法是SetPortVal 端口號(hào),欲寫入的數(shù)據(jù),寫入數(shù)據(jù)的長(zhǎng)度 KBCWait4IBE(); SetPortVal(KBC_KEY_DATA, (IntPtr)btScancode, 1);// '寫入按鍵信息,按下鍵 } /// <summary> /// 模擬鍵盤彈出 2007-11-19 01:39 yadianna521
/// </summary>
/// <param name="vKeyCoad"></param> public void MykeyUp(int vKeyCoad) { int btScancode = 0; btScancode = MapVirtualKey((byte)vKeyCoad, 0); //btScancode = vKeyCoad; KBCWait4IBE(); // '發(fā)送數(shù)據(jù)前應(yīng)該先等待鍵盤緩沖區(qū)為空 SetPortVal(KBC_KEY_CMD,(IntPtr) 0xD2, 1); //'發(fā)送鍵盤寫入命令 KBCWait4IBE(); SetPortVal(KBC_KEY_DATA, (IntPtr)0xe0, 1);// '寫入按鍵信息,釋放鍵 KBCWait4IBE(); // '發(fā)送數(shù)據(jù)前應(yīng)該先等待鍵盤緩沖區(qū)為空 SetPortVal(KBC_KEY_CMD, (IntPtr)0xD2, 1); //'發(fā)送鍵盤寫入命令 KBCWait4IBE(); SetPortVal(KBC_KEY_DATA, (IntPtr)btScancode, 1);// '寫入按鍵信息,釋放鍵 } /// <summary> /// 模擬鼠標(biāo)按下 /// </summary> /// <param name="vKeyCoad"></param> public void MyMouseDown(int vKeyCoad) { int btScancode = 0; btScancode = MapVirtualKey((byte)vKeyCoad, 0); //btScancode = vKeyCoad; KBCWait4IBE(); // '發(fā)送數(shù)據(jù)前應(yīng)該先等待鍵盤緩沖區(qū)為空 SetPortVal(KBC_KEY_CMD,(IntPtr)0xD3, 1);// '發(fā)送鍵盤寫入命令 //SetPortVal函數(shù)用于向端口寫入數(shù)據(jù),它的用法是SetPortVal 端口號(hào),欲寫入的數(shù)據(jù),寫入數(shù)據(jù)的長(zhǎng)度 KBCWait4IBE(); SetPortVal(KBC_KEY_DATA, (IntPtr)(btScancode|0x80), 1);// '寫入按鍵信息,按下鍵 } /// <summary> /// 模擬鼠標(biāo)彈出 /// </summary> /// <param name="vKeyCoad"></param> public void MyMouseUp(int vKeyCoad) { int btScancode = 0; btScancode = MapVirtualKey((byte)vKeyCoad, 0); // btScancode = vKeyCoad; KBCWait4IBE(); // '發(fā)送數(shù)據(jù)前應(yīng)該先等待鍵盤緩沖區(qū)為空 SetPortVal(KBC_KEY_CMD,(IntPtr) 0xD3, 1); //'發(fā)送鍵盤寫入命令 KBCWait4IBE(); SetPortVal(KBC_KEY_DATA, (IntPtr)(btScancode | 0x80), 1);// '寫入按鍵信息,釋放鍵 } /// <summary> /// 發(fā)送鼠標(biāo)事件 /// </summary> /// <returns></returns> public void SendMouse() { } /// <summary> /// 鼠標(biāo)動(dòng)作枚舉 /// </summary> public enum mouseeventflag : uint { move = 0x0001, leftdown = 0x0002, leftup = 0x0004, rightdown = 0x0008, rightup = 0x0010, middledown = 0x0020, middleup = 0x0040, xdown = 0x0080, xup = 0x0100, wheel = 0x0800, virtualdesk = 0x4000, absolute = 0x8000 } /// <summary> /// 鍵盤動(dòng)作枚舉 /// </summary> public enum VirtualKeys : byte { 2007-11-19 01:39 yadianna521
/// </summary>
/// <param name="vKeyCoad"></param> public void MykeyUp(int vKeyCoad) { int btScancode = 0; btScancode = MapVirtualKey((byte)vKeyCoad, 0); //btScancode = vKeyCoad; KBCWait4IBE(); // '發(fā)送數(shù)據(jù)前應(yīng)該先等待鍵盤緩沖區(qū)為空 SetPortVal(KBC_KEY_CMD,(IntPtr) 0xD2, 1); //'發(fā)送鍵盤寫入命令 KBCWait4IBE(); SetPortVal(KBC_KEY_DATA, (IntPtr)0xe0, 1);// '寫入按鍵信息,釋放鍵 KBCWait4IBE(); // '發(fā)送數(shù)據(jù)前應(yīng)該先等待鍵盤緩沖區(qū)為空 SetPortVal(KBC_KEY_CMD, (IntPtr)0xD2, 1); //'發(fā)送鍵盤寫入命令 KBCWait4IBE(); SetPortVal(KBC_KEY_DATA, (IntPtr)btScancode, 1);// '寫入按鍵信息,釋放鍵 } /// <summary> /// 模擬鼠標(biāo)按下 /// </summary> /// <param name="vKeyCoad"></param> public void MyMouseDown(int vKeyCoad) { int btScancode = 0; btScancode = MapVirtualKey((byte)vKeyCoad, 0); //btScancode = vKeyCoad; KBCWait4IBE(); // '發(fā)送數(shù)據(jù)前應(yīng)該先等待鍵盤緩沖區(qū)為空 SetPortVal(KBC_KEY_CMD,(IntPtr)0xD3, 1);// '發(fā)送鍵盤寫入命令 //SetPortVal函數(shù)用于向端口寫入數(shù)據(jù),它的用法是SetPortVal 端口號(hào),欲寫入的數(shù)據(jù),寫入數(shù)據(jù)的長(zhǎng)度 KBCWait4IBE(); SetPortVal(KBC_KEY_DATA, (IntPtr)(btScancode|0x80), 1);// '寫入按鍵信息,按下鍵 } /// <summary> /// 模擬鼠標(biāo)彈出 /// </summary> /// <param name="vKeyCoad"></param> public void MyMouseUp(int vKeyCoad) { int btScancode = 0; btScancode = MapVirtualKey((byte)vKeyCoad, 0); // btScancode = vKeyCoad; KBCWait4IBE(); // '發(fā)送數(shù)據(jù)前應(yīng)該先等待鍵盤緩沖區(qū)為空 SetPortVal(KBC_KEY_CMD,(IntPtr) 0xD3, 1); //'發(fā)送鍵盤寫入命令 KBCWait4IBE(); SetPortVal(KBC_KEY_DATA, (IntPtr)(btScancode | 0x80), 1);// '寫入按鍵信息,釋放鍵 } /// <summary> /// 發(fā)送鼠標(biāo)事件 /// </summary> /// <returns></returns> public void SendMouse() { } /// <summary> /// 鼠標(biāo)動(dòng)作枚舉 /// </summary> public enum mouseeventflag : uint { move = 0x0001, leftdown = 0x0002, leftup = 0x0004, rightdown = 0x0008, rightup = 0x0010, middledown = 0x0020, middleup = 0x0040, xdown = 0x0080, xup = 0x0100, wheel = 0x0800, virtualdesk = 0x4000, absolute = 0x8000 } /// <summary> /// 鍵盤動(dòng)作枚舉 /// </summary> public enum VirtualKeys : byte { 2007-11-19 01:39 yadianna521
創(chuàng)建一個(gè)類文件復(fù)制進(jìn)去編譯一下就可以了
這個(gè)類,封裝了2個(gè)方式,一種是系統(tǒng)api[user32.dll]文件,調(diào)用方式 key kk = new key(); kk.send(key.VirtualKeys.VK_F1,GetState(key.VirtualKeys.VK_F1)); 這是模擬鍵盤的F1. 這是模擬鍵盤的winio調(diào)用方式 key kk = new key(); // IntPtr a = new IntPtr(); kk.sendwinio(); kk.MykeyDown((int)key.VirtualKeys.VK_F1); System.Threading.Thread.Sleep(2000); kk.MykeyUp((int)key.VirtualKeys.VK_F1); 也是模擬f1,之間的差別就是,winio調(diào)用方式能穿透一部分游戲,達(dá)到一部分外掛的效果. 2008-3-12 01:27 jackson0169
下載地址呢?
頁(yè): [1] Powered by Discuz! Archiver © 2001-2006 Comsenz Inc. |
|