zoukankan      html  css  js  c++  java
  • windows、linux劫持技术

    windows系统以下能够利用detours劫持

    realse  模式劫持,调试的程序不能够

    函数劫持能够实现的效果。

    函数的劫持原理

    我们怎样实现-detours

     

    detours是微软亚洲研究院出品的信息安全产品。主要用于劫持。

     

    detours依据函数指针改变函数的行为,

    拦截不论什么函数,即使操作系统函数。

    1.安装detours

    2.构建库文件-nmake编译


    3.包括头文件还有库文件

    #include <detours.h>

    #pragma comment(lib, "detours.lib")

    4.

    定义旧函数指针指向原来的函数

    static int (WINAPI *OLD_MessageBoxW)(HWND hWnd, LPCSTR lpText, LPCSTR lpaptioin, UINT uType) = MessageBoxW;

     

    定义新的函数

    int WINAPI  NEW_MessgeBox(HWND hWnd, LPCSTR lpText, LPCSTR lpCaptioin, UINT uType)

    {

    //又一次定义函数的行为

    //为空能够禁止函数使用

    //加上if else 能够限制函数的调用

    //加上对话框能够限制同一或者不允许

    if (IDYES == MessageBoxW(NULL, lpCommandLine, L"拦截成功!", MB_YESNO) )

        return 1;

    else

        return FALSE;

       

        return ret;

    }

     

     

    5.

     

    開始拦截

    void Hook()

    {

    DetourRestoreAfterWith();//恢复原来状态

    DetourTransactionBegin();//拦截開始

    DetourUpdateThread(GetCurrentThread());//刷新当前线程

    //这里能够连续多次调用DetourAttach,表明HOOK多个函数

    DetourAttach( (void **)&OLD_MessageBox, NEW_MessageBox );//实现函数拦截

    DetourTransactionCommit();//拦截生效

    }

     

    取消拦截

    void UnHook()

    {

    DetourTransactionBegin();//拦截開始

    DetourUpdateThread(GetCurrentThread());//刷新当前线程

    //这里能够连续多次调用DetourDetach,,表明撤销多个函数HOOK

    DetourDetach( (void **)&OLD_MessageBox, NEW_MessageBox );//实现函数拦截

    DetourTransactionCommit();//拦截生效

    }

     

    6.改动自己,直接挂接函数就可以

    改动外部程序

    须要作为模块注射,须要导出声明

    __declspec(dllexport)


    劫持system函数

    #include<stdio.h>
    #include<stdlib.h>
    #include<Windows.h>
    #include<string.h>
    
    #include"detours.h"
    #pragma comment(lib, "detours.lib")
    
    //劫持自己
    
    static int (*poldsystem)( const char * _Command)=system;//存储函数指针地址
    
    int  newsystem(const char * _Command)
    {
    	//tasklist
    	printf("%s", _Command); //禁止你干活	
    	return 0;
    }
    int  newsystemA(const char * _Command)
    {
    	//tasklist   过滤
    	char *p = strstr(_Command, "tasklist");
    	if (p == NULL)
    	{		
    		poldsystem(_Command);
    	}
    	else
    	{
    		printf("%s禁止运行", _Command);//找到
    		return 0;
    	}
    	return 0;
    }
    
    //開始拦截
    void Hook()
    {
    	DetourRestoreAfterWith();//恢复原来状态
    	DetourTransactionBegin();//拦截開始
    	DetourUpdateThread(GetCurrentThread());//刷新当前线程
    	//这里能够连续多次调用DetourAttach。表明HOOK多个函数
    	DetourAttach((void **)&poldsystem, newsystemA);//实现函数拦截
    	DetourTransactionCommit();//拦截生效
    }
    void main()
    {
    	system("calc");
    	Hook();
    	system("calc");
    	system("tasklist");
    	getchar();
    }
    

    编写成dll文件,注入到其它的程序中,从而可以实现劫持其它应用程序,达到过滤的效果。假设交了保护费,就行不去劫持你的程序。实现猥琐的技术。

    #include<stdio.h>
    #include<stdlib.h>
    #include<Windows.h>
    #include<string.h>
    
    #include"detours.h"
    #pragma comment(lib, "detours.lib")
    
    
    static int(*poldsystem)(const char * _Command) = system;//存储函数指针地址
    
    int  newsystem(const char * _Command)
    {
    	//tasklist
    	printf("%s", _Command); //禁止你干活
    	return 0;
    }
    //開始拦截
    void Hook()
    {
    	DetourRestoreAfterWith();//恢复原来状态
    	DetourTransactionBegin();//拦截開始
    	DetourUpdateThread(GetCurrentThread());//刷新当前线程
    	//这里能够连续多次调用DetourAttach,表明HOOK多个函数
    	DetourAttach((void **)&poldsystem, newsystem);//实现函数拦截
    	DetourTransactionCommit();//拦截生效
    }
    //导出函数,能够载入的时候调用
    _declspec(dllexport) void  go()
    {
    	MessageBoxA(0, "1", "2", 0);
    	Hook();
    }
    

    CreateProcessW函数是用来创建进程的。


    #include<stdio.h>
    #include<stdlib.h>
    #include<Windows.h>
    
    void main1()
    {
    	//system("calc");
    	//ShellExecuteA(0, "open", "calc", 0, 0, 1);
    	STARTUPINFO si = { sizeof(si) }; //启动信息
    	PROCESS_INFORMATION pi;//保存了进程的信息
    	si.dwFlags = STARTF_USESHOWWINDOW; //表示显示窗体
    	si.wShowWindow = 1; //1表示显示创建的进程的窗体 
    	wchar_t cmdline[] = L"c://program files//internet explorer//iexplore.exe";
    	CreateProcessW(NULL, cmdline, NULL, NULL, 0, CREATE_NEW_CONSOLE, NULL, NULL, &si, &pi);//创建进程	
    }
    

    在Windows平台下能够使用挂钩(Hook)技术,将系统中的鼠标、键盘等事件拦截下来。以加入实现自己的功能。相同的。在Linux系统中也有类似的技术。都能够起到挂钩(Hook)拦截的作用。能够实现拦截的功能。拦截技术的实现是通过环境变量LD_PRELOAD设置优先被载入器载入的动态库(下面简称拦截动态库)。这里应该设置LD_PRELOAD=“xxx.so”


    样例:
    /* 文件名称:verifypasswd.c */
    /* 这是一段推断用户口令的程序。当中使用到了标准C函数strcmp*/
     
    #include <stdio.h>
    #include <string.h>
     
    int main(int argc, char **argv)
    {
    	char passwd[] = "password";
    	 
    	if (argc < 2) {
    		printf("usage: %s <password>
    ", argv[0]);
    		return;
    	}
    	 
    	if (!strcmp(passwd, argv[1])) {
    		printf("Correct Password!
    ");
    		return;
    	}
    	 
    	printf("Invalid Password!
    ");
    }

    编译程序:

    $ gcc -o verifypasswd verifypasswd.c

    測试一下程序:(得到正确结果)

    $ ./verifypasswd asdf

    Invalid Password!

    在上面这段程序中。我们使用了strcmp函数来推断两个字符串是否相等。以下。我们使用一个动态函数库来重载strcmp函数:

    #include <stdio.h>
    int strcmp(const char *s1, const char *s2)
    {
            printf("hack function invoked. s1=<%s> s2=<%s>
    ", s1, s2);
            /* 永远返回0,表示两个字符串相等 */
            return 0;
    }

    编译程序:

    $ gcc -shared -o hack.so hack.c


    设置LD_PRELOAD变量:(使我们重写过的strcmp函数的hack.so成为优先加载链接库)

    $ export LD_PRELOAD="./hack.so"

    再次执行程序:

    $ ./verifypasswd  asdf

    hack function invoked. s1=<password> s2=<asdf>

    Correct Password!



  • 相关阅读:
    Django REST framework 1
    爬虫基本原理
    QueryDict对象
    Django组件ModelForm
    MongoDB
    Algorithm
    BOM
    CSS
    Vue
    AliPay
  • 原文地址:https://www.cnblogs.com/gavanwanggw/p/6902605.html
Copyright © 2011-2022 走看看