Author Topic: Help me!!!  (Read 7345 times)

Offline ngochuan1st

  • Jr. Member
  • *
  • Posts: 30
Help me!!!
« on: September 16, 2012, 09:38:06 AM »
Hello all, I don't know how to Set Up The KeyBoard Hook by SetWindowsHookExA in nasm... :(

Offline Bryant Keller

  • Forum Moderator
  • Full Member
  • *****
  • Posts: 360
  • Country: us
    • About Bryant Keller
Re: Help me!!!
« Reply #1 on: September 16, 2012, 07:50:20 PM »
First, don't cross post. Having multiple topics with the same question doesn't improve your chances of getting your question answered, it just makes more work for the moderators who have to clean up after you.

As for the code, SetWindowsHookExA is pretty straight forward. I was going to refer you to Iczelion's site, but it seems it's no longer online (pitty). IIRC it should be something like this:

Code: [Select]
;; Translates to the C expression:
;;   SetWindowsHookA(WH_MOUSE,SomeStdCallProcedure,GetModuleHandle(NULL), NULL);
;;

   xor eax, eax ; eax = 0
   push eax
   push eax
   call GetModuleHandleA
   push eax ; eax = hInstance
   push dword SomeStdCallProcedure
   push dword 7 ; WH_MOUSE
   call SetWindowsHookExA

;; At this point, EAX contains your hooked handle. Save it for use with UnhookWindowsHookExA

About Bryant Keller
bkeller@about.me

Offline ngochuan1st

  • Jr. Member
  • *
  • Posts: 30
Re: Help me!!!
« Reply #2 on: September 17, 2012, 04:44:00 AM »
This is full code, that's not work!!!
i'm using NASM and ALINK (on Windows 7 32bit Professional)
; nasm -fobj dllHook.asm
; alink -oPE -dll dllHook.obj win32.lib
and
; nasm -fobj Call.asm
; alink -oPE Call.obj


dllHook.asm (dllHook.dll)
Code: [Select]
global dllstart
export dllstart
global KeyBoardHook
export KeyBoardHook
global SetHook
export SetHook
global hHook

;--------------------------------------------
%include 'win32.inc'
[extern GetModuleHandleA]
[extern SetWindowsHookExA]
[extern CallNextHookEx]
[extern MessageBoxA]

segment data public use32 class=DATA

Module db 'dllHook',0
lpText db 'Key A is Pressed',0
lpCaption db 'Key A',0
lpErrorC db 'Error',0
lpErrorT db 'Cannot Set Hook',0
hHook dd 0
hHandle dd 0

segment .code use32
;DLL entry point - do nothing, but flag success
;This is a STDCALL entrypoint, so remove 3 params from stack on return
..start:
dllstart:
 mov eax,1
 ret 12

;exported procedure
KeyBoardHook:
%define nCode ebp+8
%define wParam ebp+0Ch
%define lParam ebp+10h
;--------------------------------------
mov eax, [nCode]
cmp eax, 0
je .reCallNextHookEx
cmp eax, HC_ACTION
jne .reCallNextHookEx
xor eax,eax
mov eax, [wParam]
cmp eax, 41h ; if wParam= 'A' then show messagebox
je .MsgBox
jmp .reCallNextHookEx

.MsgBox:
push 0
push dword lpCaption
push dword lpText
push 0
call MessageBoxA
jmp .RetProcHook

.reCallNextHookEx:
push dword [lParam]
push dword [wParam]
push dword [nCode]
push dword [hHook]
call CallNextHookEx
;-----------------------------------------
.RetProcHook:
ret

SetHook:
push dword Module
call GetModuleHandleA
cmp eax, 0
je .notHook
mov [hHandle], eax
push 0
push dword [hHandle]
push dword KeyBoardHook
push WH_KEYBOARD
call SetWindowsHookExA
cmp eax,0
je .notHook
mov [hHook], eax
jmp .RetProcSet
.notHook:
push byte 0
push dword lpErrorC
push dword lpErrorT
push byte 0
call MessageBoxA
.RetProcSet:
ret
;-----------------------------

Call.asm (Call function in dllHook.dll)

Code: [Select]

extern SetHook
import SetHook dllHook.dll
extern ExitProcess
import ExitProcess kernel32.dll
extern _getch
import _getch msvcrt.dll

segment .data USE32

lpText db 'Hello',0
lpCaption db 'Welcome',0

..start:
;-------------------------------
call [SetHook]
call [_getch]
;-------------------------------
push dword 0
call [ExitProcess]
;-------------------------------
« Last Edit: September 17, 2012, 05:45:47 AM by ngochuan1st »

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2359
  • Country: us
Re: Help me!!!
« Reply #3 on: September 17, 2012, 08:45:58 AM »
I can't test this (not running Windows), but according to the bizzare theory that "code is code", these minor changes might possibly help...

Code: [Select]
global dllstart
export dllstart
global KeyBoardHook
export KeyBoardHook
global SetHook
export SetHook
global hHook

;--------------------------------------------
%include 'win32.inc'
[extern GetModuleHandleA]
[extern SetWindowsHookExA]
[extern CallNextHookEx]
[extern MessageBoxA]

segment data public use32 class=DATA

Module db 'dllHook',0
lpText db 'Key A is Pressed',0
lpCaption db 'Key A',0
lpErrorC db 'Error',0
lpErrorT db 'Cannot Set Hook',0
hHook dd 0
hHandle dd 0

segment .code use32
;DLL entry point - do nothing, but flag success
;This is a STDCALL entrypoint, so remove 3 params from stack on return
..start:
dllstart:
 mov eax,1
 ret 12

;exported procedure
KeyBoardHook:
%define nCode ebp+8
%define wParam ebp+0Ch
%define lParam ebp+10h
;--------------------------------------
; in order for above to make sense...
push ebp
mov ebp, esp
mov eax, [nCode]
cmp eax, 0
je .reCallNextHookEx
cmp eax, HC_ACTION
jne .reCallNextHookEx
xor eax,eax
mov eax, [wParam]
cmp eax, 41h ; if wParam= 'A' then show messagebox
je .MsgBox
jmp .reCallNextHookEx

.MsgBox:
push 0
push dword lpCaption
push dword lpText
push 0
call MessageBoxA
jmp .RetProcHook

.reCallNextHookEx:
push dword [lParam]
push dword [wParam]
push dword [nCode]
push dword [hHook]
call CallNextHookEx
;-----------------------------------------
.RetProcHook:
; unwind stack frame
leave
; and probably...
ret 12

SetHook:
push dword Module
call GetModuleHandleA
cmp eax, 0
je .notHook
mov [hHandle], eax
push 0
push dword [hHandle]
push dword KeyBoardHook
push WH_KEYBOARD
call SetWindowsHookExA
cmp eax,0
je .notHook
mov [hHook], eax
jmp .RetProcSet
.notHook:
push byte 0
push dword lpErrorC
push dword lpErrorT
push byte 0
call MessageBoxA
.RetProcSet:
ret
;-----------------------------

Bryant sez:
Quote
I was going to refer you to Iczelion's site, but it seems it's no longer online (pitty).
Ouch! Bummer! What are we gonna do?

Best,
Frank


Offline Bryant Keller

  • Forum Moderator
  • Full Member
  • *****
  • Posts: 360
  • Country: us
    • About Bryant Keller
Re: Help me!!!
« Reply #4 on: September 17, 2012, 09:31:54 AM »
I wasn't able to get your code to work, so I rewrote it. I broke this up into several files and ported a NASMX build.bat file. Since the windows system that I have access to uses NASMX, I used the tools available with it (nasm.exe, gorc.exe, golink.exe). If you look into the build.bat file, you'll notice this computer used the default NASM installation location (C:\Program Files\nasm) and if you have NASM installed elsewhere, that will need to be changed.
« Last Edit: September 17, 2012, 09:34:50 AM by Bryant Keller »

About Bryant Keller
bkeller@about.me

Offline Bryant Keller

  • Forum Moderator
  • Full Member
  • *****
  • Posts: 360
  • Country: us
    • About Bryant Keller
Re: Help me!!!
« Reply #5 on: September 17, 2012, 09:34:11 AM »
Bryant sez:
Quote
I was going to refer you to Iczelion's site, but it seems it's no longer online (pitty).
Ouch! Bummer! What are we gonna do?

You could try:
http://www.asmcommunity.net/book/tutorials/iczelion/

Though it's not yet finished being updated..
« Last Edit: September 17, 2012, 09:36:04 AM by Bryant Keller »

About Bryant Keller
bkeller@about.me

Offline ngochuan1st

  • Jr. Member
  • *
  • Posts: 30
Re: Help me!!!
« Reply #6 on: September 17, 2012, 10:26:59 AM »
i'm hooked in masm32, but i want to hook in nasm.
Thanks Frank Kotler, i'm trying...

Offline ngochuan1st

  • Jr. Member
  • *
  • Posts: 30
Re: Help me!!!
« Reply #7 on: September 17, 2012, 11:50:21 AM »
i think this code have a problem in SetHook function...

Offline Bryant Keller

  • Forum Moderator
  • Full Member
  • *****
  • Posts: 360
  • Country: us
    • About Bryant Keller
Re: Help me!!!
« Reply #8 on: September 18, 2012, 12:46:06 AM »
i'm hooked in masm32, but i want to hook in nasm.

Check out the attachment I posted. It was written for NASM, it just uses golink instead of alink (and gorc for the call.asm test program's dialog).

About Bryant Keller
bkeller@about.me

Offline ngochuan1st

  • Jr. Member
  • *
  • Posts: 30
Re: Help me!!!
« Reply #9 on: September 18, 2012, 01:07:03 AM »
i'm checked but it's not work !!! :(

Offline Bryant Keller

  • Forum Moderator
  • Full Member
  • *****
  • Posts: 360
  • Country: us
    • About Bryant Keller
Re: Help me!!!
« Reply #10 on: September 18, 2012, 01:26:36 AM »
Could you give me a little more information? What errors are you getting?

About Bryant Keller
bkeller@about.me

Offline ngochuan1st

  • Jr. Member
  • *
  • Posts: 30
Re: Help me!!!
« Reply #11 on: September 18, 2012, 01:34:53 AM »
it's not show a messagebox when i press 'A'.

Offline Bryant Keller

  • Forum Moderator
  • Full Member
  • *****
  • Posts: 360
  • Country: us
    • About Bryant Keller
Re: Help me!!!
« Reply #12 on: September 18, 2012, 01:40:21 AM »
This code should show a dialog that, when in focus, it shows a message box every time you hit a key. I run the build.bat script and tested it again, it seems to be working on this XP/32 system. This zip file contains the binaries. Try running them and see if you get the results I describe, if so, then it's a problem in how you're building the project, if not then it seems there is something on your system preventing it from working correctly.  :-\

About Bryant Keller
bkeller@about.me

Offline ngochuan1st

  • Jr. Member
  • *
  • Posts: 30
Re: Help me!!!
« Reply #13 on: September 18, 2012, 01:55:50 AM »
my computer not have Golink.exe???

Offline Bryant Keller

  • Forum Moderator
  • Full Member
  • *****
  • Posts: 360
  • Country: us
    • About Bryant Keller
Re: Help me!!!
« Reply #14 on: September 18, 2012, 01:59:31 AM »
Both GoRC and GoLINK can be obtained from Jeremy Gordon's Site. You should put them in the same directory that nasm.exe is in.

About Bryant Keller
bkeller@about.me