unit DRSecuClientMain; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ExtCtrls, RSecuClient, Vcl.Menus, ManagerRdpSecu, Tocsg.Trace, Tocsg.Process, Tocsg.CommonData, GlobalDefine; const // CAPTURE_APPS = 'Bandicamera.exe|SnippingTool.exe|RuntimeBroker.exe'; CAPTURE_APPS = 'Bandicamera.exe|SnippingTool.exe'; type TDlgCoeAgentMain = class(TForm) mmLog: TMemo; MainMenu: TMainMenu; miConnection: TMenuItem; miConnect: TMenuItem; miDisconnect: TMenuItem; N1: TMenuItem; miExit: TMenuItem; N2: TMenuItem; RDP1: TMenuItem; tInit: TTimer; pnBottom: TPanel; lbTime: TLabel; lbStatus: TLabel; tStatus: TTimer; btnScrSecu: TButton; procedure miExitClick(Sender: TObject); procedure miConnectionClick(Sender: TObject); procedure miConnectClick(Sender: TObject); procedure miDisconnectClick(Sender: TObject); procedure tInitTimer(Sender: TObject); procedure tStatusTimer(Sender: TObject); procedure RDP1Click(Sender: TObject); procedure btnScrSecuClick(Sender: TObject); private { Private declarations } bActive_: Boolean; sDllPath_: String; bIsWow64_: Boolean; Client_: TRSecuClient; MgRdp_: TManagerRdpSecu; Trace_: TTgTrace; TgAppList_: TSTringList; ThdAppMon_: TThdProcessWatch; SharedData_: TTgFileMapping; procedure Log(sLog: String); overload; procedure Log(sFormat: String; const Args: array of const); overload; procedure OnAppNotify(aSender: TThdProcessWatch; pEnt: PPwEnt; aKind: TProcessWatchKind); procedure SetSharedData(aActive: Boolean); function StartHookWatch: Boolean; function StopHookWatch: Boolean; public { Public declarations } Constructor Create(aOwner: TComponent); override; Destructor Destroy; override; procedure process_WM_NOTI_CREATE_IMAGE_FILE(var msg: TMessage); Message WM_NOTI_CREATE_IMAGE_FILE; stdcall; procedure process_WM_SYSCOMMAND(var msg: TWMSysCommand); Message WM_SYSCOMMAND; procedure process_WM_RDPSECU_LOG(var msg: TMessage); Message WM_RDPSECU_LOG; procedure process_WM_CRMCLIENT_NOTIFICATION(var msg: TMessage); Message WM_CRMCLIENT_NOTIFICATION; procedure process_WM_COPYDATA(var msg: TMessage); Message WM_COPYDATA; end; var DlgCoeAgentMain: TDlgCoeAgentMain; implementation uses // BoxedAppSDK_Static, Tocsg.Packet, Tocsg.PacketDefine, Tocsg.Registry, System.IniFiles, Tocsg.Safe, Tocsg.Path, DConnectInfo, Define, Tocsg.DateTime, DSelRdpDest, DRdpLink, Tocsg.WinInfo, Tocsg.Strings, Tocsg.Exception, superobject, DImgView, Soap.EncdDecd; {$R *.dfm} Constructor TDlgCoeAgentMain.Create(aOwner: TComponent); begin Inherited Create(aOwner); Trace_ := TTgTrace.Create(GetRunExePathDir, CutFileExt(ExtractFileName(GetRunExePath)) + '.log'); Client_ := TRSecuClient.Create(Handle); MgRdp_ := TManagerRdpSecu.Create(Handle); Caption := APP_TITLE; ChangeWindowMessageFilter(WM_COPYDATA, MSGFLT_ADD); ChangeWindowMessageFilter(WM_NOTI_CREATE_IMAGE_FILE, MSGFLT_ADD); // BoxedAppSDK_SetContext('21f2f010-06e4-465f-af8f-cde6a3752c39'); // BoxedAppSDK_Init; bActive_ := false; sDllPath_ := GetRunExePathDir + DLL_APIHOOK; bIsWow64_ := IsWow64; TgAppList_ := TStringList.Create; TgAppList_.CaseSensitive := false; SplitString(CAPTURE_APPS, '|', TgAppList_); ThdAppMon_ := nil; SharedData_ := TTgFileMapping.Create(MAP_FILENAME_APIHOOK, SizeOf(TSharedData)); ZeroMemory(SharedData_.Data, SizeOf(SharedData_.Data)); Log(APP_TITLE + 'ÀÌ ½ÇÇà µÇ¾ú½À´Ï´Ù.'); tInit.Enabled := true; end; Destructor TDlgCoeAgentMain.Destroy; begin if ThdAppMon_ <> nil then FreeAndNil(ThdAppMon_); FreeAndNil(TgAppList_); tStatus.Enabled := false; Inherited; FreeAndNil(MgRdp_); FreeAndNil(Client_); if SharedData_ <> nil then FreeAndNil(SharedData_); FreeAndNil(Trace_); end; procedure TDlgCoeAgentMain.Log(sLog: String); begin Trace_.T(sLog); mmLog.Lines.Add(Format('[%s] %s', [DateTimeToStr(Now), sLog])); end; procedure TDlgCoeAgentMain.Log(sFormat: String; const Args: array of const); var str: String; begin FmtStr(str, sFormat, Args); Log(str); end; procedure TDlgCoeAgentMain.OnAppNotify(aSender: TThdProcessWatch; pEnt: PPwEnt; aKind: TProcessWatchKind); begin case aKind of pwkUnknown : {$IFDEF DEBUG} ASSERT(false) {$ENDIF}; pwkInit, pwkExecute : begin if TgAppList_.IndexOf(pEnt.sPName) = -1 then exit; if InjectModule(pEnt.dwPid, sDllPath_, @bIsWow64_) then begin TTgTrace.T('InjectModule() .. PName="%s"', [pEnt.sPName]); end else begin // {$IFDEF WIN64} // var sExe32: String := CutFileExt(GetRunExePath) + '32.exe'; // if FileExists(sExe32) then // ExecutePath(sExe32, Format('-hook %d', [pEnt.dwPid])); // {$ENDIF} TTgTrace.T('Fail .. InjectModule() .. PName="%s"', [pEnt.sPName]); end; end; pwkTerminated : ; end; end; procedure TDlgCoeAgentMain.SetSharedData(aActive: Boolean); var sDir: String; begin bActive_ := aActive; with SharedData_.Data^ do begin bActive := bActive_; llRcvWnd := Handle; sDir := GetRunExePathDir; StrCopy(sTaskDir, PChar(sDir)); StrCopy(sLogPath, PChar(sDir + LOG_FILE)); end; end; function TDlgCoeAgentMain.StartHookWatch: Boolean; begin Result := false; if ThdAppMon_ = nil then begin try EjectModuleFromPath(sDllPath_); DeleteFile(GetRunExePathDir + LOG_FILE); ThdAppMon_ := TThdProcessWatch.Create; ThdAppMon_.OnProcessWatchNotify := OnAppNotify; ThdAppMon_.StartThread; SetSharedData(true); Result := true; except on E: Exception do ETgException.TraceException(Self, E, 'Fail .. StartHookWatch()'); end; end; end; function TDlgCoeAgentMain.StopHookWatch: Boolean; begin Result := false; if ThdAppMon_ <> nil then begin try SetSharedData(false); {$IFDEF WIN64} // var sExe32: String := CutFileExt(GetRunExePath) + '32.exe'; // if FileExists(sExe32) then // ExecutePath(sExe32, '-clearhook'); {$ENDIF} ThdAppMon_.OnProcessWatchNotify := nil; FreeAndNil(ThdAppMon_); EjectModuleFromPath(sDllPath_); Sleep(500); EjectModuleFromPath(sDllPath_); bActive_ := false; Result := true; except on E: Exception do ETgException.TraceException(Self, E, 'Fail .. StopHookWatch()'); end; end; end; procedure TDlgCoeAgentMain.btnScrSecuClick(Sender: TObject); begin if not bActive_ then begin if not StartHookWatch then // if not LoadHookDLL then begin MessageBox(Handle, PChar('DLLÀ» ·ÎµåÇÏ´Â Áß ½ÇÆÐÇß½À´Ï´Ù.'), PChar(Caption), MB_ICONWARNING or MB_OK); exit; end; MessageBox(Handle, PChar('DRM Á¦¾î°¡ ½ÃÀÛ µÇ¾ú½À´Ï´Ù.'), PChar(Caption), MB_ICONINFORMATION or MB_OK); end else begin StopHookWatch; // FreeHookDLL; MessageBox(Handle, PChar('DRM Á¦¾î°¡ ÁßÁö µÇ¾ú½À´Ï´Ù.'), PChar(Caption), MB_ICONINFORMATION or MB_OK); end; if bActive_ then begin btnScrSecu.Caption := 'DRM Á¦¾î ÁßÁö'; end else begin btnScrSecu.Caption := 'DRM Á¦¾î ½ÃÀÛ'; end; Application.ProcessMessages; end; procedure TDlgCoeAgentMain.miConnectClick(Sender: TObject); var dlg: TDlgConnectInfo; begin Guard(dlg, TDlgConnectInfo.Create(Self, Client_)); if dlg.ShowModal = mrOk then begin end; end; procedure TDlgCoeAgentMain.miConnectionClick(Sender: TObject); begin miConnect.Checked := Client_.Connected; miConnect.Enabled := not miConnect.Checked; miDisconnect.Enabled := not miConnect.Enabled; end; procedure TDlgCoeAgentMain.miDisconnectClick(Sender: TObject); begin if Client_.Connected then begin if MessageBox(Handle, PChar('Á¢¼Ó ÇØÁ¦ ÇϽðڽÀ´Ï±î?'), PChar(Caption), MB_ICONQUESTION or MB_YESNO) = IDNO then exit; Client_.Disconnect; end; end; procedure TDlgCoeAgentMain.process_WM_NOTI_CREATE_IMAGE_FILE(var msg: TMessage); begin if SharedData_.IsAvailable then begin Log(SharedData_.Data.sImgPath); end; end; procedure TDlgCoeAgentMain.process_WM_SYSCOMMAND(var msg: TWMSysCommand); begin if msg.CmdType = SC_CLOSE then begin {$IFNDEF DEBUG} miExit.Click; exit; {$ENDIF} end; Inherited; end; procedure TDlgCoeAgentMain.RDP1Click(Sender: TObject); var dlg: TDlgSelRdpDest; dlgLink: TDlgRdpLink; begin Guard(dlg, TDlgSelRdpDest.Create(Self, Client_)); if dlg.ShowModal = mrOk then begin dlgLink := TDlgRdpLink.Create(Self, Client_, dlg.SelDestIp); dlgLink.Show; end; end; procedure TDlgCoeAgentMain.process_WM_RDPSECU_LOG(var msg: TMessage); begin Log(PChar(msg.LParam)); end; procedure TDlgCoeAgentMain.tInitTimer(Sender: TObject); var ini: TIniFile; sEmpNo: String; begin tInit.Enabled := false; Guard(ini, TIniFile.Create(CutFileExt(GetRunExePath) + '.ini')); if ini.ReadBool('TCheckBox', 'chAutoConn', false) then begin sEmpNo := ini.ReadString('TEdit', 'edEmpNo', ''); if sEmpNo = '' then exit; Client_.EmpNo := sEmpNo; if not Client_.Connect(ini.ReadString('TEdit', 'edIp', ''), ini.ReadInteger('TEdit', 'edPort', 0)) then begin ini.WriteBool('TCheckBox', 'chAutoConn', false); end; end; end; procedure TDlgCoeAgentMain.tStatusTimer(Sender: TObject); begin if Client_.Connected then lbTime.Caption := ConvSecBetweenToProgTime(Client_.ConnDT, Now); case MgRdp_.State of rssNone : begin lbStatus.Caption := Format('RDPº¸¾È ¹ÌÀû¿ë (%d)', [MgRdp_.RdpPort]); lbStatus.Font.Color := clGray; end; rssPrevent : begin lbStatus.Caption := Format('RDPº¸¾È Àû¿ë (%d)', [MgRdp_.RdpPort]); lbStatus.Font.Color := clBlue; end; rssOpenRdp : begin lbStatus.Caption := Format('RDPº¸¾È ¿¬°á´ë±â (%d)', [MgRdp_.RdpPort]); lbStatus.Font.Color := clGreen; end; end; Application.ProcessMessages; end; procedure TDlgCoeAgentMain.process_WM_CRMCLIENT_NOTIFICATION(var msg: TMessage); var Rcv: IRcvPacket; Send: ISendPacket; begin case msg.WParam of NOTI_RCV_TEST : begin Rcv := IRcvpacket(msg.LParam); Log('Server Send : ' + Rcv.S['Msg']); end; NOTI_CLIENT_CONN : begin tStatus.Enabled := true; Log('¼­¹ö¿¡ Á¢¼Ó µÇ¾ú½À´Ï´Ù.'); MgRdp_.State := rssRqPrevent; end; NOTI_CLIENT_DISCONN : begin tStatus.Enabled := false; Log('¼­¹ö¿Í Á¢¼ÓÀÌ ÇØÁ¦ µÇ¾ú½À´Ï´Ù.'); lbTime.Caption := 'ÁغñµÊ'; lbStatus.Caption := 'RDPº¸¾È ¹ÌÀû¿ë'; lbStatus.Font.Color := clBlack; MgRdp_.State := rssNone; end; REQ_RDP_OPEN : begin Rcv := IRcvpacket(msg.LParam); Log('%s(%s) ¿¡¼­ RDP ¿¬°áÀ» ¿äûÇß½À´Ï´Ù.', [Rcv.S['RqIP'], Rcv.S['ComName']]); Send := TTgPacket.Create(Rcv); if MgRdp_.OpenSafeRdp then begin Send.I['Port'] := MgRdp_.RdpPort; end else begin Log('RDPº¸¾È ¿¬°áÀ» ½ÇÆÐÇß½À´Ï´Ù...'); Send.Result := 11; Send.ResultMsg := 'RDPº¸¾È ¿¬°áÀ» ÇÏ´Â Áß ½ÇÆÐÇß½À´Ï´Ù.'; end; Client_.SendPacket(Send); end; REQ_RDP_CLOSE : begin MgRdp_.CloseSafeRdp; end; end; Application.ProcessMessages; end; procedure TDlgCoeAgentMain.miExitClick(Sender: TObject); begin if MessageBox(Handle, PChar('Á¾·áÇϽðڽÀ´Ï±î?'), PChar(Caption), MB_ICONQUESTION or MB_YESNO) = IDNO then exit; Close; end; procedure TDlgCoeAgentMain.process_WM_COPYDATA(var msg: TMessage); var dwData: DWORD; pCpData: PCopyDataStruct; O: ISuperObject; begin dwData := 0; pCpData := PCopyDataStruct(msg.LParam); try dwData := pCpData.dwData; case dwData of RSCMD_CAPUTRE_DATA : begin O := SO(Copy(PChar(pCpData.lpData), 1, pCpData.cbData)); var pBuf: TBytes := DecodeBase64(O.S['Data']); var ms: TMemoryStream := TMemoryStream.Create; ms.Write(pBuf[0], Length(pBuf)); var dlg: TDlgImgView := TDlgImgView.Create(Self, ms, O.S['Path'], O.I['PID'], Handle); if dlg.LoadFromStream(ms) then dlg.Show else dlg.Free; end; end; except on E: Exception do ETgException.TraceException(Self, E, Format('Fail .. process_WM_COPYDATA(), dwData=%d', [dwData])); end; end; end.