4-13-指针、劫持、递归、多线程

学习4-13期视频笔记

1、指针

       int num = 10;
       printf("%p", &num);//可以通过两种方式(直接、间接)修改num值,间接赋值有两种方式(直接内存修改、使用指针进行修改)
       *(&num) = 3;//通过指针,从内存中更改变量的值
       printf("\n%d", num);//此时输出的num值位3
 
       int num = 10;
       int *p = #
       printf("%p", &num);//显示的地址位num内存中的地址,存储的数据是10
       printf("\n%p", &p);//显示的p值内存中地址,存储的数据是num的地址

 

2、劫持自己(劫持system函数)

(1)安装DetoursExpress30.msi,

(2)使用vs开发人员命令提示进行编译(cd到安装目录下,nmake)

nmake执行报错:

第一次报错:C:\Program Files (x86)\Microsoft Research\Detours Express3.0\lib.X86\detours.pdb使用了旧格式,提示要删除

第二次报错error c2220: .\detours.cpp(156):error C2220: 警告被视为错误 - 没有生成“object”文件

修改./src/makefile,

将CFLAGS=/W4 /WX /Zi /MTd /Gy /Gm- /Zl /Od/DDETOURS_BITS=$(DETOURS_BITS)

改为CFLAGS=/W3 /Zi /MTd /Gy /Gm- /Zl /Od /DDETOURS_BITS=$(DETOURS_BITS)

修改./sample/common.mak

同样删除/WX 修改/W4为/W3

重新编译

(3)复制include下三个头文件与lib.x86下的俩个lib文件到vs项目的文件夹下

(4)编程实现

#include<stdio.h>
#include<stdlib.h>
#include<Windows.h>
#include"detours.h"//包含头文件,必须在包含windows.h
#pragmacomment(lib,"detours.lib")//包含库文件
 
int(*poldsystem)(const char * _Command)=system;//存放老的函数代码
intnewsystem(const char * _Command)
{
       printf("%s", _Command);
     poldsystem(_Command);
}
void hook()
{
       DetourRestoreAfterWith();//恢复之前的状态,避免重复劫持
       DetourTransactionBegin();//开始劫持
       DetourUpdateThread(GetCurrentThread());//刷新当前的线程
       DetourAttach((void**)&poldsystem,newsystem);//劫持
       DetourTransactionCommit();//立刻生效
}
 
void main()
{
       system("notepad");//打开notepad文件
       hook();//劫持system函数,使其实现printf功能
       system("notepad");//打印字符串notepad
       system("pause");//打印字符串pause
     getchar();
}


执行时须改Debug模式为Release模式

 

3、劫持他人

使用vs创建一个mfc程序,作为靶子

添加俩个按钮,添加事件,使之打开notepad程序

编辑程序,在release模式下生成dll文件

 {   _DCRTIMPint__cdeclsystem( _In_opt_z_ charconst*_Command );}//system定义
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<Windows.h>
#include"detours.h"//包含头文件,必须在包含windows.h
#pragmacomment(lib,"detours.lib")//包含库文件
int(*poldsystem)(constchar * _Command) = system;//存放老的函数代码
//书写格式仿照system函数的定义,去掉宏,保留剩余部分
//{   _DCRTIMP int __cdeclsystem( _In_opt_z_ charconst*_Command );}//system定义
 
intnewsystem(const char * _Command)
{
       MessageBoxA(0, "hello","world", 0);
       return 1;
}
void hook()
{
       DetourRestoreAfterWith();//恢复之前的状态,避免重复劫持
       DetourTransactionBegin();//开始劫持
       DetourUpdateThread(GetCurrentThread());//刷新当前的线程
       DetourAttach((void**)&poldsystem,newsystem);//劫持
       DetourTransactionCommit();//立刻生效
}
_declspec(dllexport)void go()
{
       hook();
       getchar();
}
通过dllnject.exe程序,将dll注射到目标程序中
 


4、劫持360(劫持CreateProcess函数)

拷贝DetoursExpress30include下三个头文件与lib.x86下的俩个lib文件到vs项目的文件夹下

 

查看CreateProcessW函数的定义

WINBASEAPI
BOOL
WINAPI
CreateProcessW(
         _In_opt_ LPCWSTRlpApplicationName,  //执行程序的名称
         _Inout_opt_ LPWSTRlpCommandLine,  //命令行
         _In_opt_ LPSECURITY_ATTRIBUTESlpProcessAttributes,//进程安全
         _In_opt_ LPSECURITY_ATTRIBUTESlpThreadAttributes,//进程主线程的安全
         _In_ BOOLbInheritHandles, ///附加参数
         _In_ DWORDdwCreationFlags,  //创建参数
         _In_opt_ LPVOIDlpEnvironment,  //环境变量的指针
         _In_opt_ LPCWSTRlpCurrentDirectory,  //进程当前的路径
         _In_ LPSTARTUPINFOWlpStartupInfo,  //进程启动的附加信息
         _Out_ LPPROCESS_INFORMATIONlpProcessInformation  //进程的标识符,也叫令牌环
         );


 

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<Windows.h>
#include"detours.h"//包含头文件,必须在包含windows.h
#pragmacomment(lib,"detours.lib")//包含库文件
 
BOOL(WINAPI*poldcreateprocess)(
       LPCWSTR lpApplicationName,
       LPWSTR lpCommandLine,
       LPSECURITY_ATTRIBUTESlpProcessAttributes,
       LPSECURITY_ATTRIBUTES lpThreadAttributes,
       BOOL bInheritHandles,
       DWORD dwCreationFlags,
       LPVOID lpEnvironment,
       LPCWSTR lpCurrentDirectory,
       LPSTARTUPINFOW lpStartupInfo,
       LPPROCESS_INFORMATIONlpProcessInformation
       ) = CreateProcessW;
 
BOOLNEWCreateProcessW(
       LPCWSTR lpApplicationName,
       LPWSTR lpCommandLine,
       LPSECURITY_ATTRIBUTESlpProcessAttributes,
       LPSECURITY_ATTRIBUTES lpThreadAttributes,
       BOOL bInheritHandles,
       DWORD dwCreationFlags,
       LPVOID lpEnvironment,
       LPCWSTR lpCurrentDirectory,
       LPSTARTUPINFOW lpStartupInfo,
       LPPROCESS_INFORMATIONlpProcessInformation)
{
       wchar_t *p1;
       wchar_t *p2;
       p1 = wcsstr(lpApplicationName,L"360");
       p2 = wcsstr(lpCommandLine,L"360");
       if (p1 != NULL || p2 != NULL)
       {
              MessageBoxA(0, "360","不允许运行", 0);
              return 0;
       }
       else
       {
              MessageBoxA(0, "不是360","可以运行", 0);
              poldcreateprocessw(lpApplicationName,
                     lpCommandLine,
                     lpProcessAttributes,
                     lpThreadAttributes,
                     bInheritHandles,
                     dwCreationFlags,
                     lpEnvironment,
                     lpCurrentDirectory,
                     lpStartupInfo,
                     lpProcessInformation);
              return 1;
       }
       return 0;
}
 
void hook()
{
       DetourRestoreAfterWith();//恢复之前的状态,避免重复劫持
       DetourTransactionBegin();//开始劫持
       DetourUpdateThread(GetCurrentThread());//刷新当前的线程
       DetourAttach((void**)&poldcreateprocess,NEWCreateProcessW);//劫持
       DetourTransactionCommit();//立刻生效
}
void unhook() //取消劫持
{
       DetourTransactionBegin();//开始劫持
       DetourUpdateThread(GetCurrentThread());//刷新当前的线程
       DetourDetach((void**)&poldcreateprocess,NEWCreateProcessW);//反劫持
       DetourTransactionCommit();//立刻生效
}
_declspec(dllexport)void go ()
{
       hook();
       getchar();
}


注入到其他进程中,可实现劫持360,(win10实验未成功)

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<Windows.h>
#include"detours.h"//包含头文件,必须在包含windows.h
#pragmacomment(lib,"detours.lib")//包含库文件
 
BOOL(WINAPI*poldCreateProcessW)(
       LPCWSTR lpApplicationName,
       LPWSTR lpCommandLine,
       LPSECURITY_ATTRIBUTESlpProcessAttributes,
       LPSECURITY_ATTRIBUTES lpThreadAttributes,
       BOOL bInheritHandles,
       DWORD dwCreationFlags,
       LPVOID lpEnvironment,
       LPCWSTR lpCurrentDirectory,
       LPSTARTUPINFOW lpStartupInfo,
       LPPROCESS_INFORMATIONlpProcessInformation
       ) = CreateProcessW;
 
BOOLNEWCreateProcessW(
       LPCWSTR lpApplicationName,
       LPWSTR lpCommandLine,
       LPSECURITY_ATTRIBUTESlpProcessAttributes,
       LPSECURITY_ATTRIBUTES lpThreadAttributes,
       BOOL bInheritHandles,
       DWORD dwCreationFlags,
       LPVOID lpEnvironment,
       LPCWSTR lpCurrentDirectory,
       LPSTARTUPINFOW lpStartupInfo,
       LPPROCESS_INFORMATIONlpProcessInformation)
{
       printf("abc");
       return 1;
}
 
void hook()
{
       DetourRestoreAfterWith();//恢复之前的状态,避免重复劫持
       DetourTransactionBegin();//开始劫持
       DetourUpdateThread(GetCurrentThread());//刷新当前的线程
       DetourAttach((void**)&poldCreateProcessW,NEWCreateProcessW);//劫持
       DetourTransactionCommit();//立刻生效
}
void main()
{      //直接使用CreateProcessW打开notepad
       STARTUPINFO si = { sizeof(si) };//创建结构体初始化第一步
       si.dwFlags = STARTF_USESHOWWINDOW;//选择窗口模式
       si.wShowWindow = 1;//显示窗口
       PROCESS_INFORMATION pi;
       wchar_t cmdline[100] =L"notepad";
       CreateProcessW(NULL, cmdline, NULL, NULL,0, CREATE_NEW_CONSOLE, NULL, NULL, &si, &pi);
 
       hook();
 
       CreateProcessW(NULL, cmdline, NULL, NULL,0, CREATE_NEW_CONSOLE, NULL, NULL, &si, &pi);
       system("pause");
}


说明劫持createprocessw函数成功

5、递归

使用递归查看数组是否递增

#include<stdio.h>
#include<stdlib.h>
 
int isit(inta[10], int i)
{
       if (i==1)
       {
              return 1;
       }
       else if (i == 2)
       {
              return a[1] > a[0];
       }
       else
       {
              return isit(a, i - 1) &&a[i - 1] > a[i - 2];
       }
 
 
}
void main()
{
       int a[10] = { 1,2,3,4,5,6,7,8,9,0 };
       int flag = isit(a, 10);
       if (flag)
       {
              printf("递增");
       }
       else
       {
              printf("不是递增");
       }
}


 

5050-n!

#include<stdio.h>
#include<stdlib.h>
int sub(int n)
{
       if (n == 0)
       {
              return 5050;
       }
       else
       {
              return sub(n - 1) - n;
       }
}
void main()
{
       printf("%d", sub(99));
       system("pause");
}


 

6、多线程

#include<stdlib.h>
#include<Windows.h>
//typedefunsigned long       DWORD;
//#defineWINAPI      __stdcall  标准的呼叫
//typedef voidfar            *LPVOID;
DWORD WINAPImymsg(LPVOID lp)
{
       MessageBoxA(0, "hello","world", 0);
 
}
void main()
{
       HANDLE hthread;
       DWORD threadid;//保存线程编号
       for (int i = 0; i < 5; i++)
       {
              hthread = CreateThread(  //异步执行(同时全部打开)
                     NULL,//安全属性
                     NULL, //堆栈大小
                     mymsg, //线程的入口点
                     NULL, //函数的入口点
                     0, //立即执行
                     &threadid); //保存线程的id   
              //WaitForSingleObject(hthread,INFINITE); //等待,实现同步执行(依次打开)
              //CloseHandle(hthread);  //关闭线程
       }
       system("pause");
}


 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值