Bonjour tout le monde,

Je m’intéresse actuellement au fonctionnement des hooks sous Windows et plus particulièrement aux hooks sur les fonctions manipulant les socket.
Le but de mon code est de faire un sniffer réseau pour internet explorer.
Le principe serait donc d'avoir une DLL qui hook les fonctions et un programme qui injecte cette DLL dans iexplore.exe.
Voici le code :

La DLL :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include "stdafx.h"
 
#include <cstdio>
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include "detours.h"
 
#pragma comment(lib,"detours.lib")
#pragma comment(lib,"ws2_32.lib")
 
int (WINAPI *pSend)(SOCKET s, const char* buf, int len, int flags) = send;
int WINAPI MySend(SOCKET s, const char* buf, int len, int flags);
int (WINAPI *pRecv)(SOCKET s, char* buf, int len, int flags) = recv;
int WINAPI MyRecv(SOCKET s, char* buf, int len, int flags);
 
FILE* pSendLogFile;
FILE* pRecvLogFile;
 
int WINAPI MySend(SOCKET s, const char* buf, int len, int flags)
{
    fopen_s(&pSendLogFile, "C:\\SendLog.txt", "a+");
    fprintf(pSendLogFile, "%s\n", buf);
    fclose(pSendLogFile);
    return pSend(s, buf, len, flags);
}
 
int WINAPI MyRecv(SOCKET s, char* buf, int len, int flags)
{
    fopen_s(&pRecvLogFile, "C:\\RecvLog.txt", "a+");
    fprintf(pRecvLogFile, "%s\n", buf);
    fclose(pRecvLogFile);
    return pRecv(s, buf, len, flags);
}
 
extern "C" __declspec(dllexport) void dummy(void){
    return;
}
 
BOOL WINAPI DllMain(HINSTANCE hinst, DWORD dwReason, LPVOID reserved)
{
    if (DetourIsHelperProcess()) {
        return TRUE;
    }
 
    if (dwReason == DLL_PROCESS_ATTACH) {
        //DetourRestoreAfterWith();
 
        DetourTransactionBegin();
        DetourUpdateThread(GetCurrentThread());
        DetourAttach(&(PVOID&)pSend, MySend);
        DetourTransactionCommit();
 
        DetourTransactionBegin();
        DetourUpdateThread(GetCurrentThread());
        DetourAttach(&(PVOID&)pRecv, MyRecv);
        DetourTransactionCommit();
    }
    else if (dwReason == DLL_PROCESS_DETACH) {
        DetourTransactionBegin();
        DetourUpdateThread(GetCurrentThread());
        DetourDetach(&(PVOID&)pSend, MySend);
        DetourTransactionCommit();
 
        DetourTransactionBegin();
        DetourUpdateThread(GetCurrentThread());
        DetourDetach(&(PVOID&)pRecv, MyRecv);
        DetourTransactionCommit();
    }
    return TRUE;
}
Le programme qui injecte la DLL :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
// injection.cpp*: définit le point d'entrée pour l'application console.
//
 
#include "stdafx.h"
#include "windows.h"
#include "detours.h"
 
#pragma comment(lib,"detours.lib")
 
int _tmain(int argc, _TCHAR* argv[])
{
    STARTUPINFO si;
    PROCESS_INFORMATION pi;
 
    ZeroMemory(&si, sizeof(si));
    ZeroMemory(&pi, sizeof(pi));
    si.cb = sizeof(si);
    si.dwFlags = STARTF_USESHOWWINDOW;
    si.wShowWindow = SW_SHOW;
 
    if(!DetourCreateProcessWithDllEx(L"C:\\Program Files\\Internet Explorer\\iexplore.exe", 
                                        NULL, NULL, NULL, TRUE, 
                                        CREATE_DEFAULT_ERROR_MODE | CREATE_SUSPENDED,
                                        NULL, NULL, &si, &pi, 
                                        "dll2.dll", NULL))
		printf("failed\n");
    else
		printf("success\n");
 
    ResumeThread(pi.hThread);
 
    WaitForSingleObject(pi.hProcess, INFINITE);
 
    CloseHandle(&si);
    CloseHandle(&pi);
 
	return TRUE;
}
Tout compile bien, cependant, quand je lance le programme pour injecter la DLL, ce message d'erreur apparaît :


Je ne comprend pas trop a quoi correspond cette erreur.
Je pense que cela proviens de ma DLL, car mon programme parvient a injecter la DLL.