Background
Break News
How to add local font to Tailwind Css and NextJS? - Tutorial Design Pattern? - Blockchain Technology, How to create own Bitcoin virtual currency - Zustand mordern management state - Design Pattern - Flyweight Pattern? - Docker Full training Topic

[Tutorial] "DLL injection and DLL injection example" (1/2)

Friday, 7 August 2015
|
Read: Completed in minutes

[Tutorial] "DLL injection and DLL injection example" (1/2)

DLL Injection is a technique used for running code within the address space of another process by forcing it to load a "dynamic link library injection".







DLL injection is often used by external programs to influence the behavior of another program in a way its authors did not anticipate or intend

A program used to inject arbitrary code into arbitrary processes is called a DLL injector
Using SetWindowHookEx API for DLL injection
 
"DLL injection and DLL injection example" Webzone Tech Tips Zidane




Remarks:

            SetWindowsHookEx can be used to inject a DLL into another process. A 32-bit DLL cannot be injected into a 64-bit process, and a 64-bit DLL cannot be injected into a 32-bit process. If an application requires the use of hooks in other processes, it is required that a 32-bit application call SetWindowsHookEx to inject a 32-bit DLL into 32-bit processes, and a 64-bit application call SetWindowsHookEx to inject a 64-bit DLL into 64-bit processes. The 32-bit and 64-bit DLLs must have different names.

          Because hooks run in the context of an application, they must match the "bitness" of the application. If a 32-bit application installs a global hook on 64-bit Windows, the 32-bit hook is injected into each 32-bit process (the usual security boundaries apply). In a 64-bit process, the threads are still marked as "hooked." However, because a 32-bit application must run the hook code, the system executes the hook in the hooking app's context; specifically, on the thread that called SetWindowsHookEx. This means that the hooking application must continue to pump messages or it might block the normal functioning of the 64-bit processes.

            If a 64-bit application installs a global hook on 64-bit Windows, the 64-bit hook is injected into each 64-bit process, while all 32-bit processes use a callback to the hooking application.
To hook all applications on the desktop of a 64-bit Windows installation, install a 32-bit global hook and a 64-bit global hook, each from appropriate processes, and be sure to keep pumping messages in the hooking application to avoid blocking normal functioning. If you already have a 32-bit global hooking application and it doesn't need to run in each application's context, you may not need to create a 64-bit version.
Here is my code show you how to Inject DLL To Any Process
You can download my projects from here



Demo here:


"DLL injection and DLL injection example" Webzone Tech Tips Zidane

Create My dll same here

DLL injection and DLL injection example





"DLL injection and DLL injection example" Webzone Tech Tips Zidane



  // dllmain.cpp : Defines the entry point for the DLL application.
  /* ****************************************
   * Author: Zidane (huuvi168@gmail.com)
   * Last modified: 2015-06-08
   * ****************************************/
  #include "stdafx.h"
  #include "dll.h"
  
  #define PARAM_ONE 1000
  
  const UINT WM_HOOK_WRITE = RegisterWindowMessage(L"WM_HOOK_WRITE");
  const UINT WM_HOOKEX = RegisterWindowMessage(L"WM_HOOKEX_RK");
  
  #define pCW ((CWPSTRUCT*)lParam)
  
  WNDPROC OldWndProc = NULL;
  LRESULT CALLBACK NewWndProc(HWND,UINT,WPARAM,LPARAM);
  BOOL bHooked = 0;
  HWND ghWnd;
  
  HHOOK hHookKeyBoard;
  HINSTANCE hModuleDll;
  
  
  BOOL APIENTRY DllMain( HMODULE hModule,
                         DWORD  ul_reason_for_call,
                         LPVOID lpReserved
                      )
  {
      switch (ul_reason_for_call)
      {
          case DLL_PROCESS_ATTACH:
               hModuleDll = (HINSTANCE) hModule;
               DisableThreadLibraryCalls(hModuleDll); 
    
               break;
          case DLL_THREAD_ATTACH:
          case DLL_THREAD_DETACH:
          case DLL_PROCESS_DETACH:
               break;
      }
      return TRUE;
  
  }
  
  
  // hàm HookPro phải có CALLBACK, không có CALLBACK sẽ bị lỗi not response
  LRESULT CALLBACK HookProc (int nCode, WPARAM wParam, LPARAM lParam)
  {
      HHOOK hHook = (HHOOK)pCW->wParam;
  
      if(nCode<0)
          return CallNextHookEx(hHook, nCode, wParam,lParam);
  
      HWND hVLWnd = pCW->hwnd;
   
      if((pCW->message == WM_HOOKEX) && pCW->lParam)
      {
          UnhookWindowsHookEx(hHookKeyBoard);
          if (bHooked)
              return CallNextHookEx(hHook, nCode, wParam,lParam);
        
          TCHAR lib_name[MAX_PATH];
          GetModuleFileName(hModuleDll, lib_name, MAX_PATH);
          if(!LoadLibrary(lib_name))
              return CallNextHookEx(hHook, nCode, wParam,lParam);
          OldWndProc = (WNDPROC)SetWindowLong(hVLWnd, GWL_WNDPROC, (LONG)NewWndProc);
    
          if(OldWndProc == NULL) {
               FreeLibrary(hModuleDll);
          }
          else
          {
              bHooked = TRUE;
          }
      }
      else if(pCW->message == WM_HOOKEX) 
      {
          UnhookWindowsHookEx(hHookKeyBoard);
          if (!bHooked)
              return CallNextHookEx(hHook, nCode, wParam,lParam);
          if(!SetWindowLong(hVLWnd, GWL_WNDPROC, (LONG)OldWndProc))
              return CallNextHookEx(hHook, nCode, wParam,lParam);
          FreeLibrary(hModuleDll);
          bHooked = FALSE;
      } 
  }
  
  LRESULT CALLBACK NewWndProc(HWND hWnd, UINT uMsg, 
                              WPARAM wParam, LPARAM lParam)
  {  
      if (uMsg == WM_HOOK_WRITE)
      { 
   
          switch (wParam) // Received from Application Demo
          {   
              case PARAM_ONE:
                   MessageBox(hWnd, L"received message 
                      succeed from dll!",L"learn-tech-tips",0);
                   break;
           }
       }
       return CallWindowProc(OldWndProc, hWnd, uMsg, wParam, lParam);
  }
  
  int InjectDll(HWND hWnd)
  {  
      if (!IsWindow(hWnd))
           return 0;
      ghWnd = hWnd;
   
      hHookKeyBoard = SetWindowsHookEx(WH_CALLWNDPROC, HookProc, hModuleDll,  
                                       GetWindowThreadProcessId(hWnd,NULL));
   
      MessageBox(hWnd, L"InjectDll succeed!",L"learn-tech-tips",0);
      if(hHookKeyBoard == NULL)
      {
          return 0;
      }
      else
      {
          SendMessage(hWnd, WM_HOOKEX, WPARAM(hHookKeyBoard), 1);
  
      }
      return 1;
  }
  
  int UnmapDll(HWND hWnd)
  { 
      if (!IsWindow(hWnd))
          return 0;
      HHOOK hHook = SetWindowsHookEx(WH_CALLWNDPROC,
                  (HOOKPROC)HookProc, hModuleDll, 
              GetWindowThreadProcessId(hWnd,NULL));
     
      if(hHook==NULL)
          return 0;
      SendMessage(hWnd, WM_HOOKEX, (WPARAM)hHook, 0);
      return 1;
  }
  



  // dll.h
  /* ****************************************
   * Author: Zidane (huuvi168@gmail.com)
   * Last modified: 2015-06-08
   * ****************************************/
  #if _MSC_VER > 1000
  #pragmaonce
  #endif
  
  #if !defined INJECT_EX__H
  #define INJECT_EX__H
  
  #ifdef INJECT_EX_EXPORTS
  #define HOOKDLL_API extern "C" __declspec(dllexport)
  #else
  #define HOOKDLL_API extern"C"__declspec(dllimport)
  #endif
  
  HOOKDLL_API int InjectDll(HWND hWnd);
  HOOKDLL_API int UnmapDll(HWND hWnd);
  
  #endif
  

Now create new project with application
Add -> Reference

"DLL injection and DLL injection example" Webzone Tech Tips Zidane

"DLL injection and DLL injection example" Webzone Tech Tips Zidane

/* ****************************************
   * Author: Zidane (huuvi168@gmail.com)
   * Last modified: 2015-06-08
   * ****************************************/
  
  void CAppDlg::OnBnClickedButtonSendmsg()
  {
      // TODO: Add your control notification handler code here 
    
      CString sWindowName = L"Hex";
      m_hVLWin = ::FindWindow(NULL, sWindowName);
  
      if (!m_hVLWin)
          return;
   
      if (InjectDll(m_hVLWin))
          SetWindowTextW(L"Inject " + sWindowName + L" Succeed! 
                                          Ready to call function ...");
  }
  
  void CAppDlg::OnBnClickedButtonExec()
  {
      // TODO: Add your control notification handler code here
      ::SendMessage(m_hVLWin, WM_HOOK_WRITE, PARAM_ONE, 1 );
  }
  

Any feedback on dll injection. Leave your comment, we ca discuss about it!

View more: https://learn-tech-tips.blogspot.com/2015/10/dll-injection-and-exmaple-2.html

Are you interested in topic DLL injection and DLL injection example from Webzone Tech Tips? If you have any thoughts or questions, please share them in the comment section below. I would love to hear from you and chat about it

Webzone Tech Tips Zidane


🙇🏼🙇🏼 We Appreciate Your Comments and Suggestions - Webzone, all things Tech Tips web development
Popular Webzone Tech Tips topic maybe you will be like it - by Webzone Tech Tips - Zidane
As a student, I found Blogspot very useful when I joined in 2014. I have been a developer for years . To give back and share what I learned, I started Webzone, a blog with tech tips. You can also search for tech tips zidane on Google and find my helpful posts. Love you all,

I am glad you visited my blog. I hope you find it useful for learning tech tips and webzone tricks. If you have any technical issues, feel free to browse my posts and see if they can help you solve them. You can also leave a comment or contact me if you need more assistance. Here is my blog address: https://learn-tech-tips.blogspot.com.

My blog where I share my passion for web development, webzone design, and tech tips. You will find tutorials on how to build websites from scratch, using hot trends frameworks like nestjs, nextjs, cakephp, devops, docker, and more. You will also learn how to fix common bugs on development, like a mini stackoverflow. Plus, you will discover how to easily learn programming languages such as PHP (CAKEPHP, LARAVEL), C#, C++, Web(HTML, CSS, javascript), and other useful things like Office (Excel, Photoshop). I hope you enjoy my blog and find it helpful for your projects. :)

Thanks and Best Regards!
Follow me on Tiktok @learntechtips and send me a direct message. I will be happy to chat with you.
Webzone - Zidane (huuvi168@gmail.com)
I'm developer, I like code, I like to learn new technology and want to be friend with people for learn each other
I'm a developer who loves coding, learning new technologies, and making friends with people who share the same passion. I have been a full stack developer since 2015, with more than years of experience in web development.
Copyright @2022(November) Version 1.0.0 - By Webzone, all things Tech Tips for Web Development Zidane
https://learn-tech-tips.blogspot.com