Advertisement
Guest User

Untitled

a guest
Aug 3rd, 2012
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.25 KB | None | 0 0
  1. {
  2. Класс отправки клавиш. Использует драйвер r0kedrv
  3. }
  4.  
  5. unit SendKeys;
  6.  
  7. interface
  8.  
  9. uses
  10. SysUtils, Classes, Windows, WinSvc, Forms, Functions;
  11.  
  12. const
  13. SC_MANAGER_ALL_ACCESS = $F003F;
  14. SERVICE_KERNEL_DRIVER = $00000001;
  15. SERVICE_DEMAND_START = $00000003;
  16. SERVICE_ERROR_IGNORE = $00000000;
  17.  
  18. DRIVER = 'ring0keyboard'; // Имя файла драйвера (без *.sys) а заодно и регистрируемой службы
  19. DEVICE_FILE = '\\\\.\\r0kedrv';
  20.  
  21. IOCTL_KB_PS2_WRITE = 1;
  22.  
  23. ERR_CANT_START = 'Can''t start keyboard emulator driver';
  24. ERR_ALREADY_EXIST = 1073;
  25.  
  26. type
  27. TByteArray = array of byte;
  28.  
  29. type
  30. TKey = record
  31. key: string;
  32. code: integer;
  33. end;
  34.  
  35. type
  36. TsendKeys = class(TObject)
  37. private
  38. { Private declarations }
  39. SCManagerHandle: THandle;
  40. ServiceHandle: THandle;
  41. function StartService(): Boolean;
  42. procedure StopService();
  43. protected
  44. { Protected declarations }
  45. public
  46. { Public declarations }
  47. waiting: integer;
  48. constructor Create();
  49. destructor Destroy();
  50. function SendCodes(codes: TByteArray): Boolean;
  51. function SendKeys(keys: string): Boolean;
  52. end;
  53.  
  54. implementation
  55.  
  56. { TKeySend }
  57.  
  58. function SKey(key: string; code: integer): TKey;
  59. begin
  60. Result.key := key;
  61. Result.code := code;
  62. end;
  63.  
  64. function StringToPWide(sStr: string): PWideChar;
  65. var
  66. w: WideString;
  67. begin
  68. w := sStr;
  69. Result := PWideChar(w);
  70. end;
  71.  
  72. function ScanCode(key: string; down: Boolean): integer;
  73. var
  74. keys: array [0 .. 69] of TKey;
  75. i: integer;
  76. begin
  77. keys[0] := SKey('1', 2);
  78. keys[10] := SKey('-', 12);
  79. keys[20] := SKey('O', 24);
  80. keys[1] := SKey('2', 3);
  81. keys[11] := SKey('=', 13);
  82. keys[21] := SKey('P', 25);
  83. keys[2] := SKey('3', 4);
  84. keys[12] := SKey('Q', 16);
  85. keys[22] := SKey('[', 26);
  86. keys[3] := SKey('4', 5);
  87. keys[13] := SKey('W', 17);
  88. keys[23] := SKey(']', 27);
  89. keys[4] := SKey('5', 6);
  90. keys[14] := SKey('E', 18);
  91. keys[24] := SKey('A', 30);
  92. keys[5] := SKey('6', 7);
  93. keys[15] := SKey('R', 19);
  94. keys[25] := SKey('S', 31);
  95. keys[6] := SKey('7', 8);
  96. keys[16] := SKey('T', 20);
  97. keys[26] := SKey('D', 32);
  98. keys[7] := SKey('8', 9);
  99. keys[17] := SKey('Y', 21);
  100. keys[27] := SKey('F', 33);
  101. keys[8] := SKey('9', 10);
  102. keys[18] := SKey('U', 22);
  103. keys[28] := SKey('G', 34);
  104. keys[9] := SKey('0', 11);
  105. keys[19] := SKey('I', 23);
  106. keys[29] := SKey('H', 35);
  107.  
  108. keys[30] := SKey('J', 36);
  109. keys[40] := SKey('V', 47);
  110. keys[31] := SKey('K', 37);
  111. keys[41] := SKey('B', 48);
  112. keys[32] := SKey('L', 38);
  113. keys[42] := SKey('N', 49);
  114. keys[33] := SKey(';', 39);
  115. keys[43] := SKey('M', 50);
  116. keys[34] := SKey('''', 40);
  117. keys[44] := SKey(',', 51);
  118. keys[35] := SKey('`', 41);
  119. keys[45] := SKey('.', 52);
  120. keys[36] := SKey('\', 43);
  121. keys[46] := SKey('/', 53);
  122. keys[37] := SKey('Z', 44);
  123. keys[47] := SKey(' ', 57);
  124. keys[38] := SKey('X', 45);
  125. keys[48] := SKey('SPACE', 57);
  126. keys[39] := SKey('C', 46);
  127.  
  128. keys[49] := SKey('ESC', 1);
  129. keys[59] := SKey('SCROLLLOCK', 70);
  130. keys[50] := SKey('BACKSPACE', 14);
  131. keys[60] := SKey('F1', 59);
  132. keys[51] := SKey('TAB', 15);
  133. keys[61] := SKey('F2', 60);
  134. keys[52] := SKey('ENTER', 28);
  135. keys[62] := SKey('F3', 61);
  136. keys[53] := SKey('RETURN', 28);
  137. keys[63] := SKey('F4', 62);
  138. keys[54] := SKey('CTRL', 29);
  139. keys[64] := SKey('F5', 63);
  140. keys[55] := SKey('SHIFT', 42);
  141. keys[65] := SKey('F6', 64);
  142. keys[56] := SKey('ALT', 56);
  143. keys[66] := SKey('F7', 65);
  144. keys[57] := SKey('CAPS', 58);
  145. keys[67] := SKey('F8', 66);
  146. keys[58] := SKey('NUMLOCK', 58);
  147. keys[68] := SKey('F9', 67);
  148.  
  149. keys[69] := SKey('F10', 68);
  150.  
  151. for i := 0 to 69 do
  152. if keys[i].key = key then
  153. break;
  154.  
  155. Result := keys[i].code;
  156.  
  157. end;
  158.  
  159. constructor TsendKeys.Create;
  160. begin
  161. AddToLog('Driver loading ...');
  162. inherited;
  163. if not StartService() then begin
  164. AddToLog(ERR_CANT_START + ':' + FloatToStr(GetLastError()));
  165. StopService();
  166. raise Exception.Create(ERR_CANT_START);
  167. Application.Terminate();
  168. end;
  169. AddToLog('Driver loaded');
  170. Sleep(5000);
  171. Self.SendKeys('n;t;TAB');
  172. end;
  173.  
  174. destructor TsendKeys.Destroy;
  175. begin
  176. AddToLog('Driver unloading');
  177. StopService();
  178. AddToLog('Driver unloaded');
  179. inherited;
  180. end;
  181.  
  182. function TsendKeys.SendCodes(codes: TByteArray): Boolean;
  183. var
  184. h: THandle;
  185. tmp:Boolean;
  186. ret:Cardinal;
  187. begin
  188.  
  189. h := CreateFile(StringToPWide(DEVICE_FILE), GENERIC_READ + GENERIC_WRITE, 0,
  190. nil, OPEN_EXISTING, 0, 0);
  191. if h = INVALID_HANDLE_VALUE then
  192. Result := False
  193. else
  194. begin
  195. ret:=0;
  196. tmp := DeviceIoControl(h,
  197. IOCTL_KB_PS2_WRITE,nil,0,
  198. @codes,Length(codes) * sizeof(codes),
  199. ret, nil);
  200. Result := not tmp;
  201. if not Result then AddToLog('Can''t send data');
  202.  
  203. CloseHandle(h);
  204. end;
  205. end;
  206.  
  207. function TsendKeys.SendKeys(keys: string): Boolean;
  208. var
  209. send: array of string;
  210. lastchar: char;
  211. i, k: integer;
  212. code: TByteArray;
  213. sc:string;
  214. begin
  215. SetLength(send, 1);
  216. send[0] := '';
  217. k := 0;
  218. lastchar := chr(0);
  219.  
  220. for i := 1 to Length(keys) do
  221. begin
  222. if (keys[i] = ';') and (lastchar <> '\') then
  223. begin
  224. k := k + 1;
  225. SetLength(send, Length(send) + 1);
  226. send[k] := '';
  227. end
  228. else if (keys[i] <> '\') or ((keys[i] = '\') and (lastchar = '\')) then
  229. send[k] := send[k] + keys[i];
  230. end;
  231.  
  232. sc:='';
  233. for i:=0 to Length(send)-1 do
  234. sc:=sc+send[i]+',';
  235. sc[Length(sc)]:=']';
  236. AddToLog('Sending keys : '+keys[i]);
  237. AddToLog('Translated to scan-codes array : '+sc);
  238.  
  239. Result := True;
  240. for i := 0 to Length(send) do
  241. begin
  242. SetLength(code, 1);
  243. code[0] := ScanCode(send[i], False);
  244. Result := Result and SendCodes(code);
  245. if not Result then
  246. break;
  247.  
  248. Sleep(waiting);
  249.  
  250. SetLength(code, 1);
  251. code[0] := ScanCode(send[i], True);
  252. Result := Result and SendCodes(code);
  253. if not Result then
  254. break;
  255.  
  256. Sleep(waiting);
  257. end;
  258. end;
  259.  
  260. function TsendKeys.StartService: Boolean;
  261. var
  262. path: string;
  263. pw:PWideChar;
  264. f:file of THandle;
  265. begin
  266. SCManagerHandle := OpenSCManager(nil, nil, SC_MANAGER_ALL_ACCESS);
  267. if SCManagerHandle = 0 then begin
  268. Result := False;
  269. AddToLog('Can''t connect to service manager');
  270. end
  271. else
  272. begin
  273. path := ExtractFilePath(ParamStr(0)) + '\' + DRIVER + '.sys';
  274. ServiceHandle := CreateService(SCManagerHandle, StringToPWide(DRIVER),
  275. StringToPWide('ring 0 keyboard emulator'), SC_MANAGER_ALL_ACCESS,
  276. SERVICE_KERNEL_DRIVER, SERVICE_DEMAND_START, SERVICE_ERROR_IGNORE,
  277. StringToPWide(path), nil, nil, nil, nil, nil);
  278. if ServiceHandle = 0 then begin
  279. Result := False;
  280. AddToLog('Can''t connect ro service');
  281. end
  282. else
  283. Result := WinSvc.StartService(ServiceHandle, 0, pw);
  284. end;
  285.  
  286. AssignFile(f,'handles');
  287. Rewrite(f);
  288. Write(f,SCManagerHandle,ServiceHandle);
  289. CloseFile(f);
  290. end;
  291.  
  292. procedure TsendKeys.StopService;
  293. var
  294. s: TServiceStatus;
  295. begin
  296. ControlService(ServiceHandle, SERVICE_CONTROL_STOP, s);
  297. DeleteService(ServiceHandle);
  298. CloseServiceHandle(ServiceHandle);
  299. CloseServiceHandle(SCManagerHandle);
  300. end;
  301.  
  302. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement