Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1205 lines
32 KiB

  1. #include <conio.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <windows.h>
  5. #include <lmcons.h>
  6. #include <userenv.h>
  7. #include <ras.h>
  8. #include <raserror.h>
  9. #include <process.h>
  10. #include <netmon.h>
  11. #include <ncdefine.h>
  12. #include <ncmem.h>
  13. #include <diag.h>
  14. #include <winsock.h>
  15. #include <devguid.h>
  16. #include "rsniffclnt.h"
  17. #include "rasdiag.h"
  18. SERVICE_STATUS_HANDLE g_hSs=NULL;
  19. SOCKET g_sock;
  20. HANDLE g_hTerminateEvent=NULL;
  21. #define RSNIFF_SERVICE_NAME TEXT("RSNIFF")
  22. #define RSNIFF_DISPLAY_NAME TEXT("RAS REMOTE SNIFF AGENT")
  23. #define INSTALL_PATH TEXT("C:\\RSNIFF")
  24. BOOL
  25. WriteLogEntry(HANDLE hFile, DWORD dwCallID, WCHAR *szClientName, WCHAR *pszTextMsg);
  26. BOOL
  27. CreateNewService(void);
  28. BOOL
  29. UninstallService(void);
  30. void
  31. RSniffServiceMain(DWORD argc, LPTSTR *argv);
  32. void
  33. BeSniffServer(SOCKET s,PRASDIAGCAPTURE pInterfaces, DWORD dwInterfaces);
  34. DWORD
  35. WINAPI
  36. RSniffSvcHandler(DWORD dwControl,DWORD dwEventType,LPVOID lpEventData,LPVOID lpContext);
  37. BOOL
  38. SetupListen(SOCKET *ps);
  39. BOOL
  40. Init(PRASDIAGCAPTURE *ppInterfaces, DWORD *pdwInterfaces, SOCKET *ps);
  41. BOOL
  42. Init(PRASDIAGCAPTURE *ppInterfaces, DWORD *pdwInterfaces, SOCKET *ps)
  43. {
  44. PRASDIAGCAPTURE pInterfaces=NULL;
  45. DWORD dwInterfaces=0;
  46. HRESULT hr;
  47. SOCKET s;
  48. *ppInterfaces = NULL;
  49. *pdwInterfaces = 0;
  50. hr = CoInitialize(NULL);
  51. if(!SUCCEEDED(hr)) {
  52. return FALSE;
  53. }
  54. // Create a termination event
  55. if((g_hTerminateEvent = CreateEvent(NULL, TRUE, FALSE, NULL))
  56. == NULL) return FALSE;
  57. if(InitWinsock() == FALSE) {
  58. wprintf(L"could not init winsock\n");
  59. return FALSE;
  60. }
  61. if(IdentifyInterfaces(&pInterfaces, &dwInterfaces) == FALSE) {
  62. wprintf(L"Could not ID interfaces\n");
  63. return FALSE;
  64. }
  65. if(SetupListen(&s) == FALSE) {
  66. wprintf(L"Could not listen!\n");
  67. return FALSE;
  68. }
  69. *ps = s;
  70. *ppInterfaces = pInterfaces;
  71. *pdwInterfaces = dwInterfaces;
  72. return TRUE;
  73. }
  74. int
  75. __cdecl
  76. wmain(int argc, TCHAR **argv)
  77. {
  78. SERVICE_TABLE_ENTRY ste[] = {
  79. RSNIFF_SERVICE_NAME, RSniffServiceMain,
  80. NULL, NULL
  81. };
  82. if (argc>1) {
  83. if(lstrcmpi(argv[1], L"-i") == 0) {
  84. wprintf(L"Installing RSNIFF. Result = %s\n", CreateNewService() ? L"Done" : L"Failed!!");
  85. return 0;
  86. }
  87. if(lstrcmpi(argv[1], L"-u") == 0) {
  88. wprintf(L"Uninstalling RSNIFF. Result = %s\n", UninstallService() ? L"Done" : L"Failed!!");
  89. return 0;
  90. }
  91. if(lstrcmpi(argv[1], L"-?") == 0 || lstrcmpi(argv[1], L"/?") == 0) {
  92. wprintf(L"\n"
  93. L"------------------------------------------------\n"
  94. L"RSNIFF - RAS REMOTE SNIFFING AGENT SERVICE (%d.%d)\n"
  95. L"------------------------------------------------\n"
  96. L"-i Install RSNIFF service\n"
  97. L"-u Uninstall RSNIFF service\n"
  98. L"-? This menu\n"
  99. L"/? This menu\n"
  100. L"------------------------------------------------\n", RASDIAG_MAJOR_VERSION, RASDIAG_MINOR_VERSION
  101. );
  102. return 0;
  103. }
  104. return 0;
  105. }
  106. if(StartServiceCtrlDispatcher(ste) == FALSE)
  107. {
  108. return 0;
  109. }
  110. return 1;
  111. }
  112. DWORD
  113. WINAPI
  114. RSniffSvcHandler(DWORD dwControl,DWORD dwEventType,LPVOID lpEventData,LPVOID lpContext)
  115. {
  116. SERVICE_STATUS ss;
  117. switch(dwControl)
  118. {
  119. case SERVICE_CONTROL_STOP:
  120. ss.dwServiceType = SERVICE_WIN32;
  121. ss.dwCurrentState = SERVICE_STOP_PENDING;
  122. ss.dwControlsAccepted = SERVICE_CONTROL_INTERROGATE;
  123. ss.dwWin32ExitCode = 0;
  124. ss.dwServiceSpecificExitCode = 0;
  125. ss.dwCheckPoint = 1;
  126. ss.dwWaitHint = 60*1000;
  127. SetServiceStatus(g_hSs, &ss);
  128. //
  129. // set event that causes service to exit
  130. //
  131. SetEvent(g_hTerminateEvent);
  132. closesocket(g_sock);
  133. return NO_ERROR;
  134. break;
  135. }
  136. return ERROR_CALL_NOT_IMPLEMENTED;
  137. }
  138. void
  139. RSniffServiceMain(DWORD argc, LPTSTR *argv)
  140. {
  141. SERVICE_STATUS_HANDLE hSs;
  142. SERVICE_STATUS ss;
  143. PRASDIAGCAPTURE pInterfaces=NULL;
  144. DWORD dwInterfaces=0;
  145. SOCKET s;
  146. ZeroMemory(&ss, sizeof(SERVICE_STATUS));
  147. if((g_hSs=hSs=RegisterServiceCtrlHandlerEx(RSNIFF_SERVICE_NAME, RSniffSvcHandler,0))
  148. == 0) return;
  149. ss.dwServiceType = SERVICE_WIN32;
  150. ss.dwCurrentState = SERVICE_START_PENDING;
  151. ss.dwControlsAccepted = SERVICE_ACCEPT_STOP;
  152. ss.dwWin32ExitCode = 0;
  153. ss.dwServiceSpecificExitCode = 0;
  154. ss.dwCheckPoint = 0;
  155. ss.dwWaitHint = 0;
  156. if(Init(&pInterfaces, &dwInterfaces, &s)==TRUE) {
  157. ss.dwCurrentState = SERVICE_RUNNING;
  158. SetServiceStatus (hSs, &ss);
  159. //
  160. // Report start event to eventlog
  161. //
  162. //MyReportEvent(BEACONMSG_STARTED, EVENTLOG_INFORMATION_TYPE, NULL, 0);
  163. //
  164. // Execute the service - run until term event is signalled
  165. //
  166. BeSniffServer(s,pInterfaces,dwInterfaces);
  167. //
  168. // Unload winsock
  169. //
  170. WSACleanup();
  171. //
  172. // Report halt to event log
  173. //
  174. //MyReportEvent(BEACONMSG_STOPPED, EVENTLOG_INFORMATION_TYPE, NULL, 0);
  175. } else {
  176. //
  177. // Report the init failure
  178. //
  179. //MyReportEvent(BEACONMSG_STOPPED, EVENTLOG_INFORMATION_TYPE, NULL, 0);
  180. }
  181. ss.dwServiceType = SERVICE_WIN32;
  182. ss.dwCurrentState = SERVICE_STOPPED;
  183. ss.dwControlsAccepted = 0;
  184. ss.dwWin32ExitCode = 0;
  185. ss.dwServiceSpecificExitCode = 0;
  186. ss.dwCheckPoint = 0;
  187. ss.dwWaitHint = 0;
  188. //
  189. // Tell service controller that service is halted.
  190. //
  191. SetServiceStatus(g_hSs, &ss);
  192. //DBGPRINTF(D, TEXT("Beacon service is halting\n"));
  193. return;
  194. }
  195. BOOL
  196. SetupListen(SOCKET *ps)
  197. {
  198. SOCKADDR_IN ServSockAddr = { AF_INET };
  199. SOCKET s;
  200. ServSockAddr.sin_port = htons(TCP_SERV_PORT);
  201. ServSockAddr.sin_addr.s_addr = INADDR_ANY;
  202. ServSockAddr.sin_family = AF_INET;
  203. wprintf(L"Create listen socket...\n");
  204. if((g_sock=s=socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET)
  205. {
  206. wprintf(L"Create listen socket failed! (%d)", WSAGetLastError());
  207. return FALSE;
  208. }
  209. //
  210. // Bind an address to the socket
  211. //
  212. wprintf(L"Bind listen socket...\n");
  213. if (bind(s, (const struct sockaddr *) &ServSockAddr,
  214. sizeof(SOCKADDR_IN)) == SOCKET_ERROR)
  215. {
  216. wprintf(L"Bind failed...\n");
  217. //
  218. // Close the socket
  219. //
  220. closesocket(s);
  221. return FALSE;
  222. }
  223. //
  224. // Attempt to listen
  225. //
  226. wprintf(L"Listen...\n");
  227. if(listen(s, 1) == SOCKET_ERROR)
  228. {
  229. wprintf(L"Could not listen...\n");
  230. //
  231. // Close the socket
  232. //
  233. closesocket(s);
  234. return FALSE;
  235. }
  236. *ps = s;
  237. return TRUE;
  238. }
  239. void
  240. BeSniffServer(SOCKET s,PRASDIAGCAPTURE pInt, DWORD dwIntCount)
  241. {
  242. HANDLE hLog;
  243. WCHAR szLogFileName[MAX_PATH+1];
  244. SYSTEMTIME st;
  245. DWORD dwCallerID=0;
  246. GetLocalTime(&st);
  247. wsprintf(szLogFileName, L"%s\\%04d%02d%02d%02d%02d-RSNIFFLOG.TXT",
  248. INSTALL_PATH,
  249. st.wYear,
  250. st.wMonth,
  251. st.wDay,
  252. st.wHour,
  253. st.wMinute,
  254. st.wSecond);
  255. hLog = CreateFile(szLogFileName,
  256. GENERIC_READ|GENERIC_WRITE,
  257. FILE_SHARE_READ,
  258. NULL,
  259. CREATE_ALWAYS,
  260. 0,NULL);
  261. if(hLog == INVALID_HANDLE_VALUE)
  262. hLog = NULL;
  263. WriteLogEntry(hLog, 0, L"RSNIFFSVC", L"Service Started");
  264. while(1)
  265. {
  266. REMOTECAPTURE rc;
  267. REMOTECAPTURE_V5 rc_v5;
  268. SOCKADDR_IN PeerSockAddr;
  269. SOCKET NewSock=0;
  270. BYTE x;
  271. int iSize = sizeof(SOCKADDR_IN);
  272. DWORD i,dwStatus;
  273. SYSTEMTIME st;
  274. WCHAR wcRootDir[MAX_PATH+1];
  275. // should always be signalled except when we're going to quit.
  276. if(WaitForSingleObject(g_hTerminateEvent, 0) != WAIT_TIMEOUT) {
  277. CloseHandle(hLog);
  278. return;
  279. }
  280. wprintf(L"Waiting for connection\n");
  281. if((NewSock = accept(s, (struct sockaddr *) &PeerSockAddr,
  282. &iSize)) == INVALID_SOCKET)
  283. {
  284. wprintf(L"accept() failed\n");
  285. continue;
  286. }
  287. // Increment the caller count
  288. dwCallerID++;
  289. GetLocalTime(&st);
  290. // Receive least common denominator...
  291. if(RecvBuffer(NewSock, (LPBYTE)&rc_v5, sizeof(REMOTECAPTURE_V5)) == FALSE)
  292. {
  293. wprintf(L"Recv invalid REMOTECAPTURE (rc.dwVer=%d)\n", rc.dwVer);
  294. closesocket(NewSock);
  295. continue;
  296. }
  297. // Convert v5 -> v7 structure
  298. if(rc_v5.dwVer == sizeof(REMOTECAPTURE_V5))
  299. {
  300. REMOTECAPTURE rc_new;
  301. PREMOTECAPTURE_V5 pRc5=(PREMOTECAPTURE_V5)&rc_v5;
  302. ZeroMemory(&rc_new, sizeof(REMOTECAPTURE));
  303. rc_new.dwVer = sizeof(REMOTECAPTURE);
  304. mbstowcs(rc_new.szMachine, pRc5->szMachine, lstrlenA(pRc5->szMachine)+1);
  305. WriteLogEntry(hLog, dwCallerID, rc_new.szMachine, L"Version 5 client");
  306. // All pre v7 clients will want to do sniff.
  307. rc_new.dwOpt1 = RSNIFF_OPT1_DOSNIFF;
  308. memcpy(&rc,&rc_new,sizeof(REMOTECAPTURE));
  309. } else // Convert v6 -> v7 structure
  310. if(rc_v5.dwVer == sizeof(REMOTECAPTURE_V6)) {
  311. REMOTECAPTURE rc_new;
  312. PREMOTECAPTURE_V5 pRc5=(PREMOTECAPTURE_V5)&rc_v5;
  313. // copy portion of struct received.
  314. memcpy(&rc_new, pRc5, sizeof(REMOTECAPTURE_V5));
  315. WriteLogEntry(hLog, dwCallerID, rc_new.szMachine, L"Version 6 client");
  316. // Receive the remaining part of the v6 structure...
  317. if(RecvBuffer(NewSock, (LPBYTE)&rc_new + sizeof(REMOTECAPTURE_V5), (sizeof(REMOTECAPTURE_V6) - sizeof(REMOTECAPTURE_V5))) == FALSE)
  318. {
  319. WriteLogEntry(hLog, dwCallerID, rc_new.szMachine, L"Recv err!");
  320. wprintf(L"Recv invalid REMOTECAPTURE (rc.dwVer=%d)\n", rc.dwVer);
  321. closesocket(NewSock);
  322. continue;
  323. }
  324. // Covert structure (since copied from V6, just change version number
  325. rc_new.dwVer = sizeof(REMOTECAPTURE);
  326. // All pre v7 clients will want to do sniff.
  327. rc_new.dwOpt1 = RSNIFF_OPT1_DOSNIFF;
  328. // copy structure
  329. memcpy(&rc,&rc_new,sizeof(REMOTECAPTURE));
  330. } else if(rc_v5.dwVer == sizeof(REMOTECAPTURE)) {
  331. REMOTECAPTURE rc_new;
  332. PREMOTECAPTURE_V5 pRc5=(PREMOTECAPTURE_V5)&rc_v5;
  333. // copy portion of struct received.
  334. memcpy(&rc_new, pRc5, sizeof(REMOTECAPTURE_V5));
  335. WriteLogEntry(hLog, dwCallerID, rc_new.szMachine, L"Version 7 client");
  336. // Receive the remaining part of the v6 structure...
  337. if(RecvBuffer(NewSock, (LPBYTE)&rc_new + sizeof(REMOTECAPTURE_V5), (sizeof(REMOTECAPTURE) - sizeof(REMOTECAPTURE_V5))) == FALSE)
  338. {
  339. WriteLogEntry(hLog, dwCallerID, rc_new.szMachine, L"Recv err!");
  340. wprintf(L"Recv invalid REMOTECAPTURE (rc.dwVer=%d)\n", rc.dwVer);
  341. closesocket(NewSock);
  342. continue;
  343. }
  344. // valid v7 structure - read remaining bytes; no conversion necessary
  345. // copy structure
  346. memcpy(&rc,&rc_new,sizeof(REMOTECAPTURE));
  347. } else {
  348. // no idea what this is...
  349. WriteLogEntry(hLog, dwCallerID, L"UNKNOWN CLIENT!", NULL);
  350. wprintf(L"Recv invalid REMOTECAPTURE version (rc.dwVer=%d)\n", rc.dwVer);
  351. closesocket(NewSock);
  352. continue;
  353. }
  354. wsprintf(wcRootDir, L"%s\\%04d%02d%02d%02d%02d%02d-%s-SID_%03d",
  355. INSTALL_PATH,
  356. st.wYear,
  357. st.wMonth,
  358. st.wDay,
  359. st.wHour,
  360. st.wMinute,
  361. st.wSecond,
  362. rc.szMachine,
  363. dwCallerID);
  364. CreateDirectory(wcRootDir,NULL);
  365. if(rc.dwOpt1 & RSNIFF_OPT1_DOSNIFF)
  366. {
  367. WriteLogEntry(hLog, dwCallerID, rc.szMachine, L"Request for local network sniff");
  368. wprintf(L"Sniffing traffic for caller: %s\n", rc.szMachine);
  369. // start sniffing
  370. for(i=0;i<dwIntCount;i++)
  371. {
  372. // Initialize this interface...
  373. if(InitIDelaydC(pInt[i].hBlob, &pInt[i].pIDelaydC) == TRUE)
  374. {
  375. if(pInt[i].pIDelaydC)
  376. {
  377. CHAR szCaptureFile[MAX_PATH+1];
  378. dwStatus = pInt[i].pIDelaydC->Start(szCaptureFile);
  379. if(dwStatus != NMERR_SUCCESS)
  380. {
  381. wprintf(L"pIDelaydC->Start() FAILED! Start rtn %x\n", dwStatus);
  382. pInt[i].pIDelaydC->Disconnect();
  383. } else {
  384. // convert mbs to wide
  385. mbstowcs(pInt[i].szCaptureFileName, szCaptureFile, lstrlenA(szCaptureFile)+1);
  386. }
  387. }
  388. } else {
  389. wprintf(L"InitIDelaydC() failed\n");
  390. }
  391. }
  392. }
  393. // Get pre-connect routing table info
  394. if(rc.dwOpt1 & RSNIFF_OPT1_GETSRVROUTINGINFO)
  395. {
  396. WCHAR szRoutingInfo[MAX_PATH+1];
  397. WriteLogEntry(hLog, dwCallerID, rc.szMachine, L"Request for local routing table");
  398. wsprintf(szRoutingInfo, L"route print > %s\\ROUTINGINFO.TXT", wcRootDir);
  399. _wsystem(szRoutingInfo);
  400. wsprintf(szRoutingInfo, L"ipconfig >> %s\\ROUTINGINFO.TXT", wcRootDir);
  401. _wsystem(szRoutingInfo);
  402. }
  403. // wait until remote closes socket
  404. RecvBuffer(NewSock, (LPBYTE)&x, 1);
  405. closesocket(NewSock);
  406. // Get the routing table info
  407. if(rc.dwOpt1 & RSNIFF_OPT1_GETSRVROUTINGINFO)
  408. {
  409. WCHAR szRoutingInfo[MAX_PATH+1];
  410. wsprintf(szRoutingInfo, L"route print >> %s\\ROUTINGINFO.TXT", wcRootDir);
  411. _wsystem(szRoutingInfo);
  412. wsprintf(szRoutingInfo, L"ipconfig >> %s\\ROUTINGINFO.TXT", wcRootDir);
  413. _wsystem(szRoutingInfo);
  414. }
  415. if(rc.dwOpt1 & RSNIFF_OPT1_DOSNIFF)
  416. {
  417. wprintf(L"Sniffing complete\n");
  418. // Stop sniffing...
  419. for(i=0;i<dwIntCount;i++)
  420. {
  421. dwStatus = pInt[i].pIDelaydC->Stop(&pInt[i].stats);
  422. if(dwStatus != NMERR_SUCCESS)
  423. {
  424. wprintf(L"pIDelaydC->Stop() rtn %x\n", dwStatus);
  425. pInt[i].pIDelaydC->Disconnect();
  426. }
  427. if(pInt[i].stats.TotalBytesCaptured)
  428. {
  429. WCHAR szNewFileName[MAX_PATH+1];
  430. WCHAR *pChar=NULL;
  431. WCHAR *pFn=NULL;
  432. pFn = pInt[i].szCaptureFileName + lstrlenW(pInt[i].szCaptureFileName) + 1;
  433. // Get the filename
  434. while(*pFn != '\\' && pFn!=pInt[i].szCaptureFileName) pFn--;
  435. pFn++;
  436. wprintf(L"%d LAN bytes captured (%s)\n", pInt[i].stats.TotalBytesCaptured, pInt[i].szCaptureFileName);
  437. wsprintf(szNewFileName,L"%s\\%s__%s_SID_%d.CAP", wcRootDir, pFn, rc.szMachine, dwCallerID);
  438. if((pChar=wcsstr(szNewFileName, L"."))
  439. != NULL) {
  440. *pChar='_';
  441. }
  442. wprintf(L"Saving %s\n", szNewFileName);
  443. MoveFile(pInt[i].szCaptureFileName, szNewFileName);
  444. } else {
  445. wprintf(L"%d bytes captured (%s)\n", pInt[i].stats.TotalBytesCaptured, pInt[i].szCaptureFileName);
  446. }
  447. // Disconnect
  448. if(pInt[i].pIDelaydC)
  449. {
  450. pInt[i].pIDelaydC->Disconnect();
  451. }
  452. }
  453. WriteLogEntry(hLog, dwCallerID, rc.szMachine, L"Client processing complete.");
  454. wprintf(L"Sniffing complete\n");
  455. }
  456. }
  457. }
  458. void
  459. Listen(PRASDIAGCAPTURE pInt, DWORD dwIntCount)
  460. {
  461. SOCKADDR_IN ServSockAddr = { AF_INET };
  462. SOCKET s;
  463. ServSockAddr.sin_port = htons(TCP_SERV_PORT);
  464. ServSockAddr.sin_addr.s_addr = INADDR_ANY;
  465. ServSockAddr.sin_family = AF_INET;
  466. wprintf(L"Create listen socket...\n");
  467. if((s=socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET)
  468. {
  469. wprintf(L"Create listen socket failed! (%d)", WSAGetLastError());
  470. }
  471. //
  472. // Bind an address to the socket
  473. //
  474. wprintf(L"Bind listen socket...\n");
  475. if (bind(s, (const struct sockaddr *) &ServSockAddr,
  476. sizeof(SOCKADDR_IN)) == SOCKET_ERROR)
  477. {
  478. wprintf(L"Bind failed...\n");
  479. //
  480. // Close the socket
  481. //
  482. closesocket(s);
  483. return;
  484. }
  485. //
  486. // Attempt to listen
  487. //
  488. wprintf(L"Listen...\n");
  489. if(listen(s, 1) == SOCKET_ERROR)
  490. {
  491. wprintf(L"Could not listen...\n");
  492. //
  493. // Close the socket
  494. //
  495. closesocket(s);
  496. return;
  497. }
  498. while(1)
  499. {
  500. REMOTECAPTURE rc;
  501. SOCKADDR_IN PeerSockAddr;
  502. SOCKET NewSock=0;
  503. BYTE x;
  504. int iSize = sizeof(SOCKADDR_IN);
  505. DWORD i,dwStatus;
  506. wprintf(L"Waiting for connection\n");
  507. if((NewSock = accept(s, (struct sockaddr *) &PeerSockAddr,
  508. &iSize)) == INVALID_SOCKET)
  509. {
  510. wprintf(L"accept() failed\n");
  511. continue;
  512. }
  513. if(RecvBuffer(NewSock, (LPBYTE)&rc, sizeof(REMOTECAPTURE)) == FALSE)
  514. {
  515. wprintf(L"Recv invalid REMOTECAPTURE (rc.dwVer=%d)\n", rc.dwVer);
  516. closesocket(NewSock);
  517. continue;
  518. }
  519. wprintf(L"Sniffing traffic for caller: %s\n", rc.szMachine);
  520. // start sniffing
  521. for(i=0;i<dwIntCount;i++)
  522. {
  523. // Initialize this interface...
  524. if(InitIDelaydC(pInt[i].hBlob, &pInt[i].pIDelaydC) == TRUE)
  525. {
  526. if(pInt[i].pIDelaydC)
  527. {
  528. CHAR szCaptureName[MAX_PATH+1];
  529. dwStatus = pInt[i].pIDelaydC->Start(szCaptureName);
  530. if(dwStatus != NMERR_SUCCESS)
  531. {
  532. wprintf(L"pIDelaydC->Start() FAILED! Start rtn %x\n", dwStatus);
  533. pInt[i].pIDelaydC->Disconnect();
  534. } else {
  535. mbstowcs(pInt[i].szCaptureFileName, szCaptureName, lstrlenA(szCaptureName)+1);
  536. }
  537. }
  538. } else {
  539. wprintf(L"InitIDelaydC() failed\n");
  540. }
  541. }
  542. // wait until remote closes socket
  543. RecvBuffer(NewSock, (LPBYTE)&x, 1);
  544. closesocket(NewSock);
  545. wprintf(L"Sniffing complete\n");
  546. // Stop sniffing...
  547. for(i=0;i<dwIntCount;i++)
  548. {
  549. dwStatus = pInt[i].pIDelaydC->Stop(&pInt[i].stats);
  550. if(dwStatus != NMERR_SUCCESS)
  551. {
  552. wprintf(L"pIDelaydC->Stop() rtn %x\n", dwStatus);
  553. pInt[i].pIDelaydC->Disconnect();
  554. }
  555. if(pInt[i].stats.TotalBytesCaptured)
  556. {
  557. WCHAR szNewFileName[MAX_PATH+1];
  558. WCHAR *pChar=NULL;
  559. wprintf(L"%d LAN bytes captured (%s)\n", pInt[i].stats.TotalBytesCaptured, pInt[i].szCaptureFileName);
  560. wsprintf(szNewFileName, L"%s__%s.CAP", pInt[i].szCaptureFileName, rc.szMachine);
  561. if((pChar=wcsstr(szNewFileName, L"."))
  562. != NULL) {
  563. *pChar='_';
  564. }
  565. wprintf(L"Saving %s\n", szNewFileName);
  566. MoveFile(pInt[i].szCaptureFileName, szNewFileName);
  567. } else {
  568. wprintf(L"%d bytes captured (%s)\n", pInt[i].stats.TotalBytesCaptured, pInt[i].szCaptureFileName);
  569. }
  570. // Disconnect
  571. if(pInt[i].pIDelaydC)
  572. {
  573. pInt[i].pIDelaydC->Disconnect();
  574. }
  575. }
  576. wprintf(L"Sniffing complete\n");
  577. }
  578. }
  579. BOOL
  580. IdentifyInterfaces(PRASDIAGCAPTURE *hLAN, DWORD *pdwLanCount)
  581. {
  582. DWORD dwStatus;
  583. HBLOB hBlob;
  584. BLOB_TABLE *pTable=NULL;
  585. DWORD i;
  586. BOOL bRas=FALSE;
  587. DWORD dwLanCount=0;
  588. PRASDIAGCAPTURE hLanAr=NULL;
  589. *hLAN = NULL;
  590. *pdwLanCount = 0;
  591. if((hLanAr = (PRASDIAGCAPTURE)LocalAlloc(LMEM_ZEROINIT, sizeof(RASDIAGCAPTURE) * MAX_LAN_CAPTURE_COUNT))
  592. == NULL) return FALSE;
  593. wprintf(L"Identifying Network Interfaces\n");
  594. // Create blob for blob table
  595. dwStatus = CreateBlob(&hBlob);
  596. if(dwStatus != NMERR_SUCCESS) {
  597. wprintf(L"Blob not created!\n");
  598. return FALSE;
  599. }
  600. // Get the blob table
  601. dwStatus = GetNPPBlobTable(hBlob, &pTable);
  602. if(dwStatus != NMERR_SUCCESS) {
  603. wprintf(L"GetNPPBlobTable failed (%d)!\n",dwStatus);
  604. DestroyBlob(hBlob);
  605. return FALSE;
  606. }
  607. wprintf(L"Interface Count: %d\n",pTable->dwNumBlobs);
  608. for(i=0;i<pTable->dwNumBlobs && (dwLanCount < MAX_LAN_CAPTURE_COUNT);i++)
  609. {
  610. NETWORKINFO ni;
  611. dwStatus = GetNetworkInfoFromBlob(pTable->hBlobs[i], &ni);
  612. if(dwStatus == NMERR_SUCCESS)
  613. {
  614. wprintf(L"----------------------------------------\n");
  615. wprintf(L"%d. LinkSpeed: %d (%d)\n", i,ni.LinkSpeed, ni.MacType );
  616. if(NMERR_SUCCESS != GetBoolFromBlob( pTable->hBlobs[i],
  617. OWNER_NPP,
  618. CATEGORY_LOCATION, //CATEGORY_NETWORKINFO,
  619. TAG_RAS,
  620. &bRas))
  621. {
  622. DestroyBlob(hBlob);
  623. return FALSE;
  624. }
  625. if(bRas)
  626. {
  627. wprintf(L"Interface %d: %s\n", i, TAG_RAS);
  628. hLanAr[dwLanCount].hBlob=pTable->hBlobs[i];
  629. hLanAr[dwLanCount++].bWan=TRUE;
  630. } else {
  631. const char *pString=NULL;
  632. if(GetStringFromBlob(pTable->hBlobs[i],
  633. OWNER_NPP,
  634. CATEGORY_LOCATION,
  635. TAG_MACADDRESS,
  636. &pString)
  637. == NMERR_SUCCESS)
  638. {
  639. //wprintf(L"Interface %d: %s (MAC: %s)\n", i, "LAN", pString);
  640. } else {
  641. //wprintf(L"Interface %d: %s (UNKNOWN)\n", i, "LAN");
  642. }
  643. // use this interface only if the mac type
  644. // is one of the following:
  645. if(ni.MacType == MAC_TYPE_ETHERNET ||
  646. ni.MacType == MAC_TYPE_TOKENRING ||
  647. ni.MacType == MAC_TYPE_ATM)
  648. if(pString)
  649. {
  650. if((hLanAr[dwLanCount].pszMacAddr = (WCHAR*)LocalAlloc(LMEM_ZEROINIT, sizeof(WCHAR) * (lstrlenA(pString) + 1)))
  651. != NULL)
  652. mbstowcs(hLanAr[dwLanCount].pszMacAddr, pString, lstrlenA(pString));
  653. }
  654. hLanAr[dwLanCount++].hBlob = pTable->hBlobs[i];
  655. }
  656. } else {
  657. wprintf(L"GetNetworkInfoFromBlob rtn %d\n", dwStatus);
  658. DestroyBlob(hBlob);
  659. return FALSE;
  660. }
  661. }
  662. wprintf(L"----------------------------------------\n");
  663. DestroyBlob(hBlob);
  664. if(dwLanCount)
  665. {
  666. *pdwLanCount = dwLanCount;
  667. *hLAN = hLanAr;
  668. }
  669. return TRUE;
  670. }
  671. BOOL
  672. InitWinsock(void)
  673. {
  674. WSADATA WSAData;
  675. WORD WSAVerReq = MAKEWORD(2,0);
  676. if (WSAStartup(WSAVerReq, &WSAData)) {
  677. return FALSE;
  678. } else
  679. return TRUE;
  680. }
  681. PSOCKCB
  682. TcpConnectRoutine(LPSTR pAddr)
  683. {
  684. SOCKADDR_IN DstAddrIP = { AF_INET };
  685. HOSTENT *pHost;
  686. ULONG uAddr;
  687. PSOCKCB pSock;
  688. BOOL bResult;
  689. ULONG ulHostAddr=0;
  690. //
  691. // Resolve name
  692. //
  693. if ((ulHostAddr = inet_addr(pAddr)) == -1L)
  694. {
  695. return NULL;
  696. }
  697. uAddr = ntohl(ulHostAddr);
  698. wprintf(L"Connecting to remote server sniffing agent (ADDR: %d.%d.%d.%d)\n",
  699. ((struct in_addr *) &uAddr)->S_un.S_un_b.s_b4,
  700. ((struct in_addr *) &uAddr)->S_un.S_un_b.s_b3,
  701. ((struct in_addr *) &uAddr)->S_un.S_un_b.s_b2,
  702. ((struct in_addr *) &uAddr)->S_un.S_un_b.s_b1);
  703. DstAddrIP.sin_port = htons(TCP_SERV_PORT);
  704. DstAddrIP.sin_addr.s_addr = htonl(uAddr);
  705. //
  706. // Create socket
  707. //
  708. pSock = CreateSocket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  709. if(pSock == NULL)
  710. return NULL;
  711. //
  712. // Connect the socket
  713. //
  714. if(ConnectSock(pSock, (struct sockaddr *)&DstAddrIP, sizeof(SOCKADDR_IN))
  715. != TRUE)
  716. {
  717. wprintf(L"Could not connect to remote server sniffing agent!\n");
  718. closesocket(pSock->s);
  719. LocalFree(pSock);
  720. return NULL;
  721. }
  722. return pSock;
  723. }
  724. BOOL
  725. ConnectSock(PSOCKCB pSock, SOCKADDR* pDstAddr, int size)
  726. {
  727. int err;
  728. //
  729. // Connect the socket
  730. //
  731. err = connect(pSock->s, pDstAddr, size);
  732. if(err==SOCKET_ERROR)
  733. {
  734. return FALSE;
  735. }
  736. return TRUE;
  737. }
  738. PSOCKCB
  739. CreateSocket(int Af, int Type, int Proto)
  740. {
  741. PSOCKCB pSock;
  742. if((pSock = (PSOCKCB)LocalAlloc(LMEM_ZEROINIT, sizeof(SOCKCB)))
  743. == NULL)
  744. return NULL;
  745. //
  746. // Create a socket
  747. //
  748. if((pSock->s = socket(Af, Type, Proto))
  749. == INVALID_SOCKET)
  750. {
  751. LocalFree(pSock);
  752. return NULL;
  753. }
  754. return pSock;
  755. }
  756. BOOL
  757. SendStartSniffPacket(PSOCKCB pSock)
  758. {
  759. REMOTECAPTURE rc;
  760. DWORD dwSize = MAX_COMPUTERNAME_LENGTH+1;
  761. ZeroMemory(&rc, sizeof(REMOTECAPTURE));
  762. rc.dwVer = sizeof(REMOTECAPTURE);
  763. GetComputerName(rc.szMachine, &dwSize);
  764. return SendBuffer(pSock->s, (LPBYTE)&rc, sizeof(REMOTECAPTURE));
  765. }
  766. BOOL
  767. SendBuffer(SOCKET s, LPBYTE pBuffer, ULONG uSize)
  768. {
  769. ULONG err,uSent, uBytesSent=0;
  770. while(uBytesSent != uSize)
  771. {
  772. uSent = send(s, (const char *)(pBuffer+uBytesSent), uSize-uBytesSent, 0);
  773. switch(uSent)
  774. {
  775. case SOCKET_ERROR:
  776. err = WSAGetLastError();
  777. return FALSE;
  778. break;
  779. case 0:
  780. return FALSE;
  781. default:
  782. uBytesSent+=uSent;
  783. break;
  784. }
  785. }
  786. return TRUE;
  787. }
  788. BOOL
  789. RecvBuffer(SOCKET s, LPBYTE pBuffer, ULONG uSize)
  790. {
  791. ULONG err=0,uRecv=0, uBytesRecv=0;
  792. while(uBytesRecv != uSize)
  793. {
  794. wprintf(L"Recv...\n");
  795. uRecv = recv(s, (char *)pBuffer+uBytesRecv, uSize-uBytesRecv, 0);
  796. switch(uRecv)
  797. {
  798. case SOCKET_ERROR:
  799. err=WSAGetLastError();
  800. wprintf(L"err=%d\n",err);
  801. return FALSE;
  802. break;
  803. case 0:
  804. wprintf(L"Socket gracefully closed by peer\n");
  805. return FALSE;
  806. default:
  807. uBytesRecv += uRecv;
  808. wprintf(L"rtn %d bytes recv\n", uBytesRecv);
  809. break;
  810. }
  811. }
  812. wprintf(L"RecvBuffer complete\n");
  813. return TRUE;
  814. }
  815. BOOL
  816. InitIDelaydC(HBLOB hBlob, IDelaydC **ppIDelaydC)
  817. {
  818. DWORD dwStatus;
  819. const char *sterrLSID;
  820. IDelaydC *pIDelaydC=NULL;
  821. wprintf(L"hBlob: %p\n", hBlob);
  822. if(NMERR_SUCCESS != GetStringFromBlob(hBlob,OWNER_NPP,CATEGORY_LOCATION,TAG_CLASSID,&sterrLSID))
  823. {
  824. wprintf(L"GetStringFromBlob failed\n");
  825. return FALSE;
  826. }
  827. wprintf(L"ClassID: %s\n",sterrLSID);
  828. dwStatus = CreateNPPInterface(hBlob,IID_IDelaydC,(void**)&pIDelaydC);
  829. if(dwStatus != NMERR_SUCCESS)
  830. {
  831. wprintf(L"CreateNPPInterface rtn %x (%p)\n", dwStatus, pIDelaydC);
  832. return FALSE;
  833. }
  834. dwStatus = pIDelaydC->Connect(hBlob,NULL,NULL,NULL);
  835. if(dwStatus != NMERR_SUCCESS)
  836. {
  837. wprintf(L"Connect rtn %x\n", dwStatus);
  838. return FALSE;
  839. }
  840. *ppIDelaydC = pIDelaydC;
  841. return TRUE;
  842. }
  843. BOOL
  844. CreateNewService(void)
  845. {
  846. SC_HANDLE schService, schSCManager;
  847. WCHAR szPath[MAX_PATH+1];
  848. LPCTSTR lpszBinaryPathName = szPath;
  849. wsprintf(szPath, L"%s\\RSNIFF.EXE", INSTALL_PATH);
  850. if((schSCManager= OpenSCManager(NULL,NULL,SC_MANAGER_CREATE_SERVICE))
  851. == NULL) return FALSE;
  852. if((schService = CreateService(
  853. schSCManager, // SCManager database
  854. RSNIFF_SERVICE_NAME, // name of service
  855. RSNIFF_DISPLAY_NAME, // service name to display
  856. SERVICE_ALL_ACCESS, // desired access
  857. SERVICE_WIN32_OWN_PROCESS | SERVICE_INTERACTIVE_PROCESS, // service type
  858. SERVICE_AUTO_START, // start type
  859. SERVICE_ERROR_NORMAL, // error control type
  860. lpszBinaryPathName, // service's binary
  861. NULL, // no load ordering group
  862. NULL, // no tag identifier
  863. NULL, // no dependencies
  864. NULL, // LocalSystem account
  865. NULL)) // no password
  866. == NULL)
  867. {
  868. WCHAR szErrTxt[MAX_PATH+1];
  869. wsprintf(szErrTxt, L"Could not install Beacon service. Err=%d", GetLastError());
  870. wprintf(L"%s\n", szErrTxt);
  871. CloseServiceHandle(schSCManager);
  872. return FALSE;
  873. }
  874. CloseServiceHandle(schService);
  875. CloseServiceHandle(schSCManager);
  876. return TRUE;
  877. }
  878. BOOL
  879. UninstallService(void)
  880. {
  881. SC_HANDLE schService, schSCManager;
  882. if((schSCManager=OpenSCManager(NULL,NULL,SC_MANAGER_CREATE_SERVICE))
  883. == NULL) return FALSE;
  884. if((schService = OpenService(schSCManager,RSNIFF_SERVICE_NAME, SERVICE_ALL_ACCESS))
  885. == NULL) {
  886. WCHAR szErrTxt[MAX_PATH+1];
  887. wsprintf(szErrTxt, L"Could not uninstall Beacon service. Err=%d", GetLastError());
  888. wprintf(L"%s\n", szErrTxt);
  889. CloseServiceHandle(schSCManager);
  890. return FALSE;
  891. }
  892. DeleteService(schService);
  893. CloseServiceHandle(schService);
  894. CloseServiceHandle(schSCManager);
  895. return TRUE;
  896. }
  897. BOOL
  898. WriteLogEntry(HANDLE hFile, DWORD dwCallID, WCHAR *szClientName, WCHAR *pszTextMsg)
  899. {
  900. WCHAR *pBuff;
  901. SYSTEMTIME st;
  902. DWORD dwBytesWritten;
  903. if(hFile == NULL) return FALSE;
  904. if(pszTextMsg)
  905. pBuff = new WCHAR[(MAX_COMPUTERNAME_LENGTH + lstrlenW(pszTextMsg) + 1 + 100)];
  906. else
  907. pBuff = new WCHAR[(MAX_COMPUTERNAME_LENGTH + 1 + 100)]; // no text msg
  908. // got buffer?
  909. if(pBuff == NULL) return FALSE;
  910. GetLocalTime(&st);
  911. wsprintf(pBuff, L"%04d%02d%02d%02d%02d%02d %05d %-15.15s %s\r\n",
  912. st.wYear,
  913. st.wMonth,
  914. st.wDay,
  915. st.wHour,
  916. st.wMinute,
  917. st.wSecond,
  918. dwCallID,
  919. szClientName,
  920. pszTextMsg ? pszTextMsg : L"");
  921. return WriteFile(hFile, pBuff, lstrlenW(pBuff) * sizeof(WCHAR), &dwBytesWritten, NULL);
  922. }