SetWindowsHookEx

lordofthunder Messages postés 30 Date d'inscription lundi 20 février 2006 Statut Membre Dernière intervention 6 septembre 2007 - 3 août 2006 à 19:43
mortalino Messages postés 6786 Date d'inscription vendredi 16 décembre 2005 Statut Membre Dernière intervention 21 décembre 2011 - 9 août 2006 à 17:03
bonjour je voudrais savoir si il est possible , en utilisant l'api SetWindowsHookEx et l'interception WH_KEYBOARD d'enregistrer chaque frappe au clavier dasn un fichier (j'aimerais savoir comment obtenir une chaine(qui sera donc une touche du clavier) afin d'enregistrer chaque "lettre")
En clair je voudrais faire un keylooger grace à cet api est ce possible?
merci
A voir également:

13 réponses

lordofthunder Messages postés 30 Date d'inscription lundi 20 février 2006 Statut Membre Dernière intervention 6 septembre 2007
3 août 2006 à 19:59
enfin setwindowshookex n'est pas un API mais vous m'avez compris, c'est une fonctiona  declarer me semble t'il
0
PCPT Messages postés 13272 Date d'inscription lundi 13 décembre 2004 Statut Membre Dernière intervention 3 février 2018 47
4 août 2006 à 11:39
salut,
si si, c'est bien une API....

voici le principe de base
place un richtextbox scroll both, et une txtbox multiligne scroll verticale sur Form1

' = -=-=-=-=
' Form1.frm
' =-=-=-=-=
'
'
Option Explicit 
'
'
Private Sub Form_Load() 
    Me.Width = 12915 
    Me.Height = 9720 

    With RichTextBox1 
        .Top = 120 
        .Left = 120 
        .Width = 12615 
        .Height = 4335 
        .Text = "" 
        .Locked = True 
    End With 

    With Text1 
        .Top = 4560 
        .Left = 120 
        .Width = 12615 
        .Height = 4695 
        .Text = "" 
        .Locked = True 
    End With 

    hHook = SetWindowsHookEx(WH_KEYBOARD, AddressOf KeyboardProc, App.hInstance, App.ThreadID) 
End Sub 
'
'
Private Sub Form_Unload(Cancel As Integer) 
    'remove the windows-hook
    UnhookWindowsHookEx hHook 
End Sub 

' =-=-=-=-=-=
' Module1.bas
' =-=-=-=-=-=
'
'
Option Explicit 
Public Const WH_KEYBOARD = 2 
Public Const VK_SHIFT = &H10 
Declare Function CallNextHookEx Lib "user32" (ByVal hHook As Long, ByVal ncode As Long, ByVal wParam As Long, lParam As Any) As Long 
Declare Function GetKeyState Lib "user32" (ByVal nVirtKey As Long) As Integer 
Declare Function SetWindowsHookEx Lib "user32" Alias "SetWindowsHookExA" (ByVal idHook As Long, ByVal lpfn As Long, ByVal hmod As Long, ByVal dwThreadId As Long) As Long 
Declare Function UnhookWindowsHookEx Lib "user32" (ByVal hHook As Long) As Long 
Public hHook As Long 
'
'
Public Function KeyboardProc(ByVal idHook As Long, ByVal wParam As Long, ByVal lParam As Long) As Long 
    'if idHook is less than zero, no further processing is required
    If idHook < 0 Then 
        'call the next hook
        KeyboardProc = CallNextHookEx(hHook, idHook, wParam, ByVal lParam) 
    Else 
        Form1.Text1.Text = Form1.Text1.Text & wParam & " " 
        Form1.RichTextBox1.Text = Form1.RichTextBox1.Text & Chr$(wParam) & " " 

        'call the next hook
        KeyboardProc = CallNextHookEx(hHook, idHook, wParam, ByVal lParam) 
    End If 
End Function

<small> Coloration
syntaxique automatique [AFCK] </small>
       

++
PCPT   [AFCK]

<hr size ="2" width="100%" />Prenez un instant pour répondre à ce sondage svp
0
lordofthunder Messages postés 30 Date d'inscription lundi 20 février 2006 Statut Membre Dernière intervention 6 septembre 2007
4 août 2006 à 16:22
il s'avere que je ne me suis jamais servi d'un richtextbox scroll both et apres documentation j'ai l'impresiopn qu'il faut creer le control soi meme.
a quoi sert-il? comment le place t-on sur la feuille?
0
mortalino Messages postés 6786 Date d'inscription vendredi 16 décembre 2005 Statut Membre Dernière intervention 21 décembre 2011 18
4 août 2006 à 23:07
Salut,

La RichTextBox (aussi employé RTB) est un composant à rajouter. Dans VB6, là où tu as tes contrôles, fait un click droit sur une zone libre,
ensuite fait ajouter composants... et coche Microsoft Rich TextBox 6.0
Ensuite place le sur ta Form. Sélectionne le et dans la fenêtre des propriétés, tu as Scroll. Met cette propriété à rtfBoth
Après place aussi sur ta Form un TextBox. Sélectionne le et dans la fenêtre des propriétés, tu as Scroll. Met cette propriété à Vertical

Pour info Scroll est le diminutif de ScrollBar (Barre de défilement, donc verticale, horizontale, les deux ou aucunes) qui se place dans l'élément
en question.

Après, merci à PCPT pour son code "tout fait", t'as plus qu'à faire un copier / coller & à tester tout ça !

@++

   Mortalino
Le mystérieux chevalier, "Provençal, le Gaulois"
0

Vous n’avez pas trouvé la réponse que vous recherchez ?

Posez votre question
mortalino Messages postés 6786 Date d'inscription vendredi 16 décembre 2005 Statut Membre Dernière intervention 21 décembre 2011 18
4 août 2006 à 23:19
PCPT,

je viens de tester, et dans la RTB, j'ai 4 fois le même caractère (id° dans le TextBox).

Public Function KeyboardProc(ByVal idHook As Long, ByVal wParam As Long, ByVal lParam As Long) As Long 
    'if idHook is less than zero, no further processing is required
    If idHook < 1</gras>
Then 
        'call the next hook
        KeyboardProc = CallNextHookEx(hHook, idHook, wParam, ByVal lParam) 
    Else 
        Form1.Text1.Text = Form1.Text1.Text & wParam & " " 
        Form1.RichTextBox1.Text = Form1.RichTextBox1.Text & Chr$(wParam) & " " 

        'call the next hook
        KeyboardProc = CallNextHookEx(hHook, idHook, wParam, ByVal lParam) 
    End If 
End Function

J'ai mis cette valeur à 1, je n'ai plus que 2 caractères (au lieu d'un). Une soluce ?
merci  ;)

@++

   Mortalino
Le mystérieux chevalier, "Provençal, le Gaulois"
0
lordofthunder Messages postés 30 Date d'inscription lundi 20 février 2006 Statut Membre Dernière intervention 6 septembre 2007
5 août 2006 à 01:00
Est-ce que la rtb a seulement plus de fonctionnalités que la textbox, ou est-ce un contrôle totalement different?
0
mortalino Messages postés 6786 Date d'inscription vendredi 16 décembre 2005 Statut Membre Dernière intervention 21 décembre 2011 18
5 août 2006 à 01:18
Rich Text veut dire 'Texte enrichi'




Le contrôle





RichTextBox


permet d'afficher, d'entrer et de
manipuler du texte mis en forme.
Il effectue les mêmes tâches que le contrôle




TextBox



,
mais il peut également afficher
des polices, des couleurs et des liens, charger
du texte et des images incorporées à partir
d'un fichier, ainsi que rechercher
des caractères spécifiques.





Mais là, ça vaut le coup de faire l'essaie, ouvre un nouveau projet, insère cette rich textbox et un textbox (avec les bonnes
valeurs à Scroll), colle les codes Form_Load et Unload, ensuite insère un module, et colle l'autre parti.
T'as plus qu'à faire F5 et à tester directement.
Après, ça c'est un exemple, à toi de l'adapter.

Pour PCPT :
c'est bien "If idHook < 0" qu'il faut mettre.
j'ai fait des recherches, il y a peu de choses sur cet API (pas mal en delphi) mais le peu qui
a est comme tu as mis.  ;)
Faut-il absoluement trier les caractères ou il y a une méthode pour ne récupérer q'un seul caractère ?

Merci bien 

@++

   Mortalino
Le mystérieux chevalier, "Provençal, le Gaulois"
0
PCPT Messages postés 13272 Date d'inscription lundi 13 décembre 2004 Statut Membre Dernière intervention 3 février 2018 47
9 août 2006 à 15:06
salut,
(salut Mortalino)

on teste idhook car c'est un des retours de l'api.
si zéro, çà ne nous intéresse pas.

pour "4" résultats, ce n'est pas une erreur :

j'appuis sur "A"

état de la touche
    ....avant la pression
    ....au keydown
    ....au keyup
    ....après

de mémoire c'est çà....

(ou avant, down, press, up)

on doit pouvoir trier avec And &HF0000000 sur wParam

lordofthunder n'hésite pas à donner suite

++
PCPT   [AFCK]
<hr size="2" width="100%" />Prenez un instant pour répondre à ce sondage svp
0
mortalino Messages postés 6786 Date d'inscription vendredi 16 décembre 2005 Statut Membre Dernière intervention 21 décembre 2011 18
9 août 2006 à 15:49
AAAAAAAArrrrrgh !

Moi, petit newb en API, j'ai testé ça :

Public Function KeyboardProc(ByVal idHook As Long, ByVal wParam As Long, ByVal lParam As Long) As Long 
    'if idHook is less than zero, no further processing is required
    If idHook < 0 Then 
        'call the next hook
        KeyboardProc =  CallNextHookEx(hHook, idHook, wParam And &HF0000000 , ByVal lParam) 
    Else 
        Form1.Text1.Text  = Form1.Text1.Text & wParam & " " 
        Form1.RichTextBox1.Text =  Form1.RichTextBox1.Text & Chr$(wParam) & " " 

        'call the next hook
        KeyboardProc = CallNextHookEx(hHook, idHook, wParam And &HF0000000 , ByVal lParam) 
    End If 
End Function

ça ne change rien, puis j'ai testé ça : (attention à la casse!!!!!!!!!!!!!!!!!)

Public Function KeyboardProc(ByVal idHook As Long, ByVal wParam As Long, ByVal lParam As Long) As Long 
    'if idHook is less than zero, no further processing is required
    If idHook < 0 Then 
        'call the next hook
        KeyboardProc  = CallNextHookEx(hHook, idHook, &HF0000000, ByVal lParam) 
    Else 
        Form1.Text1.Text = Form1.Text1.Text & wParam & " " 
        Form1.RichTextBox1.Text = Form1.RichTextBox1.Text & Chr$(wParam) & " " 

        'call the next hook
        KeyboardProc = CallNextHookEx(hHook, idHook, &HF0000000, ByVal lParam) 
    End If 
End Function

VB s'est carrément fermé, il a pas du apprécier ma manip !!

Rha ces jeunes, ils testent des trucs sans savoir...

@++

   Mortalino
Le mystérieux chevalier, "Provençal, le Gaulois"
0
PCPT Messages postés 13272 Date d'inscription lundi 13 décembre 2004 Statut Membre Dernière intervention 3 février 2018 47
9 août 2006 à 16:12
rhalala qu'est-ce que tu nous fais là Mortalino

c'est vrai que j'aurais pu préciser...

Public Function KeyboardProc(ByVal idHook As Long, ByVal wParam As Long, ByVal lParam As Long) As Long 
    'if idHook is less than zero, no further processing is required
    If idHook < 0 Then 
        'call the next hook
        KeyboardProc =  CallNextHookEx(hHook, idHook, wParam, ByVal lParam) 
    Else 
         If (GetKeyState(wParam) And &HF0000000) Then 
            Form1.Text1.Text  = Form1.Text1.Text & wParam & " " 
            Form1.RichTextBox1.Text =  Form1.RichTextBox1.Text & Chr$(wParam) & " " 
        End If  
        'call the next hook
        KeyboardProc  = CallNextHookEx(hHook, idHook, wParam, ByVal lParam) 
    End If 
End Function

là on a juste le down et up
on ne peut pas faire moins puisque le principe est justement de récupérer la vraie frappe qui peut très bien être (dans l'ordre) :

Shift - down
A - down
A - up
Shift - up

si on n'avait pas ces 2 éléments (down up) çà serait assez dur de savoir si la touche saisie est ~ sous l'effet ~ d'une touche telle shift/ctrl/alt etc...
++
<hr size="2" width="100%" />Prenez un instant pour répondre à ce sondage svp
0
mortalino Messages postés 6786 Date d'inscription vendredi 16 décembre 2005 Statut Membre Dernière intervention 21 décembre 2011 18
9 août 2006 à 16:26
J'ai trouvé moins mais ce ne doit pas être conventionnel !!
Ne rigole surtout pas (en attendant, ça marche...)

Public Function KeyboardProc(ByVal idHook As Long, ByVal wParam As Long, ByVal lParam As Long) As Long 
    'if idHook is less than zero, no further processing is required
    If idHook < 1 Then 
        'call the next hook
        KeyboardProc =  CallNextHookEx(hHook, idHook, wParam, ByVal lParam) 
    Else 
         If (GetKeyState(wParam) And &HF0000000) Then 
            Form1.Text1.Text  = Form1.Text1.Text & wParam & " " 
            Form1.RichTextBox1.Text =  Form1.RichTextBox1.Text & Chr$(wParam) & " " 
        End If  
        'call the next hook
        KeyboardProc  = CallNextHookEx(hHook, idHook, wParam, ByVal lParam) 
    End If 
End Function

on en revient à la valeur 1, qui n'a pourtant rien avoir.
mais j'ai bien un seul caractère

@++

   Mortalino
Le mystérieux chevalier, "Provençal, le Gaulois"
0
PCPT Messages postés 13272 Date d'inscription lundi 13 décembre 2004 Statut Membre Dernière intervention 3 février 2018 47
9 août 2006 à 16:48
il y a de l'idée mais ce n'est pas encore çà ;)

· idHook
Specifies the type of hook procedure to be installed. This parameter can be one of the following values:
WH_CALLWNDPROC
 Installs a hook procedure that monitors messages before the system sends them to the destination window procedure. For more information, see the CallWndProc hook procedure.
WH_CALLWNDPROCRET
 Installs a hook procedure that monitors messages after they have been processed by the destination window procedure. For more information, see the CallWndRetProc hook procedure.
WH_CBT
 Installs a hook procedure that receives notifications useful to a computer-based training (CBT) application. For more information, see the CBTProc hook procedure.
WH_DEBUG
 Installs a hook procedure useful for debugging other hook procedures. For more information, see the DebugProc hook procedure.
WH_GETMESSAGE
 Installs a hook procedure that monitors messages posted to a message queue. For more information, see the GetMsgProc hook procedure.
WH_JOURNALPLAYBACK
 Installs a hook procedure that posts messages previously recorded by a WH_JOURNALRECORD hook procedure. For more information, see the JournalPlaybackProc hook procedure.
WH_JOURNALRECORD
 Installs a hook procedure that records input messages posted to the system message queue. This hook is useful for recording macros. For more information, see the JournalRecordProc hook procedure.
WH_KEYBOARD
 Installs a hook procedure that monitors keystroke messages. For more information, see the KeyboardProc hook procedure.
WH_MOUSE
 Installs a hook procedure that monitors mouse messages. For more information, see the MouseProc hook procedure.
WH_MSGFILTER
 Installs a hook procedure that monitors messages generated as a result of an input event in a dialog box, message box, menu, or scroll bar. For more information, see the MessageProc hook procedure.
WH_SHELL
 Installs a hook procedure that receives notifications useful to shell applications. For more information, see the ShellProc hook procedure.
WH_SYSMSGFILTER
 Installs a hook procedure that monitors messages generated as a result of an input event in a dialog box, message box, menu, or scroll bar. The hook procedure monitors these messages for all applications in the system. For more information, see the SysMsgProc hook procedure.

la bonne valeur serait Private Const WH_KEYBOARD = 2
(comme indiqué dans les déclarations, mais non utilisé )

++
<hr size="2" width="100%" />Prenez un instant pour répondre à ce sondage svp
0
mortalino Messages postés 6786 Date d'inscription vendredi 16 décembre 2005 Statut Membre Dernière intervention 21 décembre 2011 18
9 août 2006 à 17:03
Bien vu, c'est parfait !
Un seul caractère s'affiche.

Bon courage avec ta boite mail. (elle doit-être blindée)

@++

   Mortalino
Le mystérieux chevalier, "Provençal, le Gaulois"
0
Rejoignez-nous