Results 1 to 6 of 6

Thread: รบกวนสอบถาม เกี่ยวกับ Code Dll injection หน่อยครับ

  1. #1
    Senior Member
    Join Date
    Jan 2008
    Location
    Some where on the earth.
    Posts
    106


    Code:
    //EXE.CPP
    
    #include <windows.h>
    #include  <windowsx.h>
    #include <iostream>
    #include  <cstdio>
    #include <string>
    
    using namespace std;
    
    #define  Filename "C:\\Program Files\\Internet Explorer\\iexplore.exe"
    #define  DLLTOINJECT "C:\\DLL.dll"
    
    //@@@@@@@@@@@
    CONTEXT  OriginalContext;
    char OriginalCodePage[4096];
    DWORD sizeofCP=0;
    VOID*  mySec;
    
    //return values:
    //
    //    if  0    ->    successful
    //    if -1    ->    (0xFFFFFFFF)  WriteProcessMemory returned FALSE
    //    else    ->    Amount of  bytes written + 1 
    //                (to get exact amount of bytes  written, you must decrement return value by one!)
    //
    
    DWORD  RestoreOriginalCodePage( HANDLE hProcess, HANDLE hThread, DWORD *outSize  )
    {
        BOOL B;
        DWORD written;
        CONTEXT Context;
    
        if(outSize)  *outSize = sizeofCP; //Just for user's info
    
        Context.ContextFlags  = CONTEXT_FULL;
        GetThreadContext( hThread, &Context);
    
        B  = WriteProcessMemory( hProcess, mySec, OriginalCodePage, sizeofCP,  &written );
    
        if(!B)
            return -1;
    
        if(written!=sizeofCP)
            return  written+1;
    
        B=SetThreadContext( hThread, (CONST  CONTEXT*)&OriginalContext);
        if(!B)
            return -1;
    
        return  0;
    }
    
    
    BOOL InjectDLL(HANDLE hProcess, HANDLE hThread,  VOID* hModuleBase, char *DllName)
    {//You must have debug access to  hProcess (required for ReadProcessMemory() & WriteProcessMemory)
    
        FARPROC  LoadLibProc = GetProcAddress(GetModuleHandle("KERNEL32.dll"),  "LoadLibraryA");
        FARPROC LastErrProc =  GetProcAddress(GetModuleHandle("KERNEL32.dll"), "GetLastError");
        if(!LoadLibProc  || !LastErrProc) return FALSE;
    
        ////////////////////////////////
        
        char  CodePage[4096] = 
        
        {    0xB8, 00, 00, 00, 00,    // mov  EAX,  0h | Pointer to LoadLibraryA() (DWORD)
            0xBB, 00, 00,  00, 00,    // mov EBX,  0h | DLLName to inject (DWORD)
            0x53,                    //  push EBX
            0xFF, 0xD0,                // call EAX
            0x5b,                    //  pop EBX
            0xcc                    // INT 3h
        };
        int  nob=15;
        
            char *DLLName;
            DWORD *EAX, *EBX;
    
            DLLName  = (char*)((DWORD)CodePage + nob);
            EAX = (DWORD*)( CodePage  +  1);
            EBX = (DWORD*) ( CodePage +  6);
    
            strcpy(  DLLName, DllName );
            *EAX = (DWORD)LoadLibProc;
            *EBX  = nob; // need to do this: *EBX = *EBX + (Section)
        ////////////////////////////
            sizeofCP  = strlen(DllName) + nob +1;
    
        IMAGE_DOS_HEADER        DOShdr;
        IMAGE_NT_HEADERS        *pNThdr,  NThdr;
        IMAGE_SECTION_HEADER    SecHdr, *pSecHdr;
        IMAGE_DATA_DIRECTORY    DataDir,  *pDataDir; //@@@@@@@@
        DWORD dwD, dwD2, read, written;
        CONTEXT  Context;
        BOOL B;
    
        Context.ContextFlags =  CONTEXT_FULL;//CONTROL;
        OriginalContext.ContextFlags =  CONTEXT_FULL;//CONTROL;
        if(!GetThreadContext( hThread,  &OriginalContext))
        {
            dwD = GetLastError();
            return  FALSE;
        }
            
        // Check to see if we have valid  Headers:
        //
        /////////Get DOS hdr
        B =  ReadProcessMemory(hProcess, hModuleBase, &DOShdr, sizeof(DOShdr),  &read);
        if( (!B) || (read!=sizeof(DOShdr)) ) return FALSE;
        if(  DOShdr.e_magic != IMAGE_DOS_SIGNATURE ) //Check for `MZ
            return  FALSE;
        
        //Get NT header
        B = ReadProcessMemory(  hProcess, 
            (VOID*)((DWORD)hModuleBase +  (DWORD)DOShdr.e_lfanew), &NThdr, sizeof(NThdr), &read);
        if(  (!B) || (read!=sizeof(NThdr)) ) return FALSE;
        if(  NThdr.Signature != IMAGE_NT_SIGNATURE ) //Check for `PE\0\0
            return  0;
    
        // Valid EXE header!
        // Look for a usable writable  code page:
        //
    
        /////
            //
            if(  (dwD=NThdr.FileHeader.NumberOfSections) < 1 ) 
                return  FALSE;//Section table: (after optional header)
            
            pSecHdr  = (IMAGE_SECTION_HEADER*)
                ( 
                ((DWORD)hModuleBase  + (DWORD)DOShdr.e_lfanew) + 
                  (DWORD)sizeof(NThdr.FileHeader) + 
                  (DWORD)NThdr.FileHeader.SizeOfOptionalHeader + 4
                  );//@@@@@@@@@@@@@
    
            B=FALSE;
            dwD2 =  (DWORD)GetModuleHandle(0);
            
            for( dwD2=0;  dwD2<dwD; dwD2++ )
            {//iterate sections to look for a  writable part of memory and NOT .idata
                if(  !ReadProcessMemory( hProcess, pSecHdr, &SecHdr, sizeof(SecHdr),  &read) )
                    return FALSE;
                if(read!=sizeof(SecHdr))  return FALSE;
                
                if( 
                    (SecHdr.Characteristics  & IMAGE_SCN_MEM_WRITE) //writable section
                    &&
                    (  strcmpi((const char*)SecHdr.Name, ".idata")!=NULL ) //not .idata  (import data)
                    )
                {
                    B  = TRUE;
                    break;//OK!!
                }
                pSecHdr++;
            }
    
            if(!B)
                return  FALSE; //couldn't find usable code  page!
            //
        /////
            //Found a  section:  (SecHdr.VirtualAddress + (DWORD)hModuleBase)
            mySec =  (VOID*)(SecHdr.VirtualAddress + (DWORD)hModuleBase);
    
            *EBX  = *EBX + (DWORD)mySec;
            
            if(!ReadProcessMemory(  hProcess, mySec,
                OriginalCodePage, sizeofCP, &read) )
                return  FALSE;
            if(read != sizeofCP)
                return FALSE;
            
            //Now  starts the mega part! (If an error occurs here, god knows what might  happen!
            B = WriteProcessMemory( hProcess, mySec,
                CodePage,  sizeofCP, &written);
            
            if( (written!=0)  && (written!=sizeofCP) )  //Uh oh!, System crash might occur  now!
            {//****EMERGENCY**** ****EMERGENCY**** ****EMERGENCY****  ****EMERGENCY****
                WriteProcessMemory( hProcess, mySec,  OriginalCodePage, sizeofCP, &written);
                // Try to save  what you can, and return back to memory
                            
                return  FALSE; //might not have worked, so, big s***!
            }
    
            if((!B)  || (written!=sizeofCP))
                return FALSE;
            
            //Ok,  injected successfully, 
            //You MUST call function  RestoreOriginalCodePage() function upon the following breakpoint!
            Context  = OriginalContext;
            Context.Eip = (DWORD)mySec;
            B =  SetThreadContext(hThread, &Context);
            if(!B) return  FALSE;
    
    
        return 1;
    }
    
    int Create()
    {
        BOOL  B=FALSE, BREAK1=FALSE, BREAK2=FALSE;
        STARTUPINFO            sInfo;
        PROCESS_INFORMATION  pInfo;
        DEBUG_EVENT            dEvent;
        DWORD                ret;
        
        ZeroMemory((VOID*)&sInfo,  sizeof(sInfo));
        B = CreateProcess(Filename, 0, 0, 0, FALSE,  DEBUG_ONLY_THIS_PROCESS,
            0, 0, &sInfo, &pInfo);
        if(!B)  return FALSE;
        
        //-----
        ///    ////  
        /////  We need 3 things, ProcessHandle, ThreadHandle, and BaseOfImage
        ////  
        ///
        //-----
    
    
        HANDLE                PHandle=pInfo.hProcess,  THandle=pInfo.hThread;
        VOID *                BaseOfImage;
        //char                DLLTOINJECT[]  = "d:\\VCPrj\\Inject\\DLL\\Release\\DLL.dll";
        //^^#defined above..
    
        while(1)
        {
            if(  !(B = WaitForDebugEvent(&dEvent, INFINITE)) )
                return  -1;
            
            if(dEvent.dwDebugEventCode==CREATE_PROCESS_DEBUG_EVENT)
            {
                BaseOfImage  = dEvent.u.CreateProcessInfo.lpBaseOfImage;
            }
    
            if(dEvent.dwDebugEventCode==EXIT_PROCESS_DEBUG_EVENT)
                break;
    
            if(dEvent.dwDebugEventCode==EXCEPTION_DEBUG_EVENT)//Check  for breakpoint
            {
                if(dEvent.u.Exception.ExceptionRecord.ExceptionCode==EXCEPTION_BREAKPOINT)
                {//It  is a break point;
                    if(BREAK1==FALSE)
                    {//First  Breakpoint occured
                        B = InjectDLL(  pInfo.hProcess, pInfo.hThread, BaseOfImage, 
                            DLLTOINJECT);
    
                        BREAK1  = TRUE;
                        if(!B)
                            return  0;
                    }else if(BREAK2==FALSE)
                    {
                        ret  = RestoreOriginalCodePage( PHandle, THandle, 0);
                        BREAK2=TRUE;
                    }
                }else
                {
                    ContinueDebugEvent(  dEvent.dwProcessId, dEvent.dwThreadId, DBG_EXCEPTION_NOT_HANDLED);
                    continue;
                }
    
            }//end  if
    
            ContinueDebugEvent( dEvent.dwProcessId,  dEvent.dwThreadId, DBG_CONTINUE);
        }//end while()
    }//end  function Create()
    
    
    int Open()
    {
    
        return 0;
    }
    
    
    int  APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE prev, LPSTR lpCmdLine,  int nShowCmd)
    {
        Create();
        Open();
        return 0;
    }
    เนื่องจาก ว่า งงๆ กับ บรรทัด ว่า เมื่อเรา Openprocess ปรกติ ด้วย Flag "DEBUG_ONLY_THIS_PROCESS" (ในที่นี้ เค้าใช้เปิด process ของ IE) คำถามคือ EXCEPTION_BREAKPOINT ในตอนแรกสุดนั้นมันมาจากไหน หรอครับ? หรือว่าใน IE มันมี 0xCC อยู่ข้างในอยู่แล้ว??
    Code:
            if(dEvent.dwDebugEventCode==EXCEPTION_DEBUG_EVENT)//Check  for breakpoint
            {
                if(dEvent.u.Exception.ExceptionRecord.ExceptionCode==EXCEPTION_BREAKPOINT)
                {//It  is a break point;
                    if(BREAK1==FALSE)
                    {//First  Breakpoint occured
    
                                      ..................
    <div align="center">"Hacking isn’t about helping the security industry, which leeches from Hackers."</div>

    <div align="center">http://img397.imageshack.us/img397/159/cwhbannerzl0.gif</div>

  2. #2


    เนื่องจากว่า งงๆ กับ บรรทัด ว่า เมื่อเรา Openprocess ปรกติ ด้วย Flag "DEBUG_ONLY_THIS_PROCESS" (ในที่นี้ เค้าใช้เปิด process ของ IE) คำถามคือ EXCEPTION_BREAKPOINT ในตอนแรกสุดนั้นมันมาจากไหนหรอครับ? หรือว่าใน IE มันมี 0xCC อยู่ข้างในอยู่แล้ว??[/b]
    EXCEPTION_BREAKPOINT ได้มาจาก

    Code:
    #include <windows.h>
    source code ส่วน include ของ windows.h

    Code:
    #include <windef.h>
    #include <types.h>
    #include <winbase.h>
    #include <wingdi.h>
    #include <winuser.h>
    #include <winreg.h>
    #include <shellapi.h>
    #if !defined(WINCEMACRO) && !defined(WIN32_NO_OLE)
    #include [list=1]
    #endif
    
    #include <imm.h>
    
    #include <tchar.h>
    #include <excpt.h>
    จะเห็นว่า include อีกหลายไฟล์ ให้ดูที่ winbase.h ซึ่งไฟล์นี้จะเก็บค่าคงที่ต่างๆ เอาไว้

    source code winbase.h
    ftp://ztchs.p.lodz.pl/dydaktyka/WDInf/Pro...clude/winbase.h

    เอาเป็นว่าไล่ดูแล้วกันจะมีการ define EXCEPTION_BREAKPOINT ไว้อยู่

    [code]#define STATUS_BREAKPOINT 0x80000003
    #define EXCEPTION_BREAKPOINT

  3. #3
    Senior Member
    Join Date
    Jan 2008
    Location
    Some where on the earth.
    Posts
    106


    - -"

    ง่าาา จริงๆคืออยากจะำถามว่า ไอ้ EXCEPTION_BREAKPOINT ที่เป็น event ของ Debug Event ครั้งแรกสุดทึ่จะทำให้ program มัน ทำการ inject dll อ่ะ มันเกิดมาจากไหน?? เพราะ งงๆ ว่า ไอ้เจ้า IE มันจะทิ้งค่า breakpoint เอาไว้ใน program มันเองทำไม - -"
    <div align="center">"Hacking isn’t about helping the security industry, which leeches from Hackers."</div>

    <div align="center">http://img397.imageshack.us/img397/159/cwhbannerzl0.gif</div>

  4. #4
    Senior Member
    Join Date
    Oct 2006
    Location
    thailand
    Posts
    182


    Code:
            
    if(dEvent.dwDebugEventCode==EXCEPTION_DEBUG_EVENT)//Check  for breakpoint
            {
                if(dEvent.u.Exception.ExceptionRecord.ExceptionCode==EXCEPTION_BREAKPOINT)
                {//It  is a break point;
                    if(BREAK1==FALSE)
                    {//First  Breakpoint occured
                      ....
    Debug Event เกินตอน CreateProcess และ Set CreationFlag เป็น DEBUG_ONLY_THIS_PROCESS ลองอ่านดูบางส่วนที่ตัดมาจากMSDN

    Code:
    DEBUG_ONLY_THIS_PROCESS
    0x00000002
    The calling thread starts and debugs the new process. It can receive all related debug events using the WaitForDebugEvent function.





    ผมส่งสัยเพิ่มเติมคือ
    Function Open() ใช้ทำอะไรครับ?

    และ โค้ดนี้ลอง compile และ run ดูหรือยังครับ :-)

    This post has been edited by pspn.n: Aug 17 2008, 12:59 PM

  5. #5
    Senior Member
    Join Date
    Jul 2004
    Location
    Thailand
    Posts
    211


    ลองศึกษา

    Tutorial 28: Win32 Debug API part 1
    http://win32assembly.online.fr/tut28.html
    Tutorial 29: Win32 Debug API part 2
    http://win32assembly.online.fr/tut29.html
    Tutorial 30: Win32 Debug API part 3
    http://win32assembly.online.fr/tut30.html

    แถม code-injection aka. fwb+, exemple: [C/C++]
    http://www.codeproject.com/KB/threads/winspy.aspx


  6. #6
    Senior Member
    Join Date
    Jan 2008
    Location
    Some where on the earth.
    Posts
    106


    ตอบคุณ pspn.n
    ไอ้เรื่อง function open นี่ผมก็ไม่แน่ใจว่าเค้ามีไว้ทำไมเหมือนกัน - -a (พอดีเรื่องนี้่ยังอ่อนหัดอยู่อีกมาก)

    ตอบคุณ neoclassic
    ส่วนเรื่อง debug event กระจ่าแล้วครับ ขอบคุณมากๆที่ชี้แนะ ^^
    <div align="center">"Hacking isn’t about helping the security industry, which leeches from Hackers."</div>

    <div align="center">http://img397.imageshack.us/img397/159/cwhbannerzl0.gif</div>

Similar Threads

  1. Replies: 2
    Last Post: 12-04-2010, 01:44 PM
  2. Replies: 6
    Last Post: 16-05-2009, 01:55 PM
  3. Example Code Injection
    By Edkung_ in forum Code Injection
    Replies: 0
    Last Post: 05-09-2008, 01:14 AM
  4. เกี่ยวกับ javascript injection
    By mixmatrix in forum Hacking, Exploit Articles/Tutorial/Techniques
    Replies: 0
    Last Post: 07-07-2007, 07:08 PM
  5. Replies: 1
    Last Post: 16-05-2007, 07:18 PM

Members who have read this thread : 0

Actions : (View-Readers)

There are no names to display.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •