1 / 12

함수 호출 구조 보고서

함수 호출 구조 보고서. 2010.11.28 시스템제어 박정욱. 다음의 소스를 이용해 메모리의 stack 영역에 함수의 호출 구조에 대해서 조사해 보겠다. void test(int, int); int main() { int A = 0x12345678; int B = 0xABCDEFBA; printf(&quot;[%08X] : main() address<br>&quot;, main); test(A, B); return 0; } void test2() { printf(&quot;test2<br>&quot;); }

maura
Download Presentation

함수 호출 구조 보고서

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 함수 호출 구조 보고서 2010.11.28 시스템제어 박정욱

  2. 다음의 소스를 이용해 메모리의 stack영역에 함수의 호출 구조에 대해서 조사해 보겠다. void test(int, int); int main() { int A = 0x12345678; int B = 0xABCDEFBA; printf("[%08X] : main() address\n", main); test(A, B); return 0; } void test2() { printf("test2\n"); } void test(int a, int b) { int C= 0x11223344; int *p= &C; p = p+2; *p = (int)test2; PrintHexaNAscii(&p, 150); }

  3. 프로그램 이 시작되면 main함수가 되고 assembly에서 main함수의 지역 변수가 들어갈 영역을 할당한다. Stack에서 main함수의 영역은 EBP(0012FF80)에서 ESP(0012FF2C)까지 이다.

  4. 변수 A, B를 선언하고 하고 A, B의 값을 초기화 한다. A B ebp-4에(0012FF7C) 변수 A를선언, 값을 초기화 하고 ebp-8에(0012FF78) 변수 B를 선언, 값을 초기화 한다.

  5. main 함수 내에서 test함수가 호출되면 인자 B, A의순서대로 Stack에 저장 후 Call명령으로 함수 본체로 점프 한다. A B call 명령은 (1)PUSH eip명령으로 A5(004012A5)를 test return address (00FF1220)에 초기화 후, (2)JUMP test명령으로 test함수의 주소 (00401330)로 점프 한다..

  6. test 함수의 몸체로 넘어오게 되면 초기 main함수의 stack영역을 할당 할 때와 같이 test함수도 stack영역을 할당 받는다. return address 004012A5다음 주소에 EBP가 초기화 된다.

  7. test의 지역 변수 c가 ebp-4(0012FF18)에 선언되고 11223344로 초기화 된다. 지역변수 *P는 ebp-8(0012FF14)에 선언되고 변수 c의 주소로 초기화 된다. *P c

  8. C의주소를 가지고 있던 P에 +2의 연산을 해 8을 증가 시켜 test return adderss의 주소를 보게 만들었다. 그 후 *P가 가리키는 test return address의 주소를 test2함수의주소로 초기화 시킨다. P의값을 test return address를 가리키게 만든다. test return address의 값을 test2의 주소로 초기화 시킨다.

  9. 다음 PrintHexaView함수가호출되고 PrintHexaView의 몸체로 가서 프로그램을 실행 후 다시 test함수로 return한다. test 함수 종료 시 ebp의 값을 test함수의 초기 ebp의 주소 0012FF1C로 옮긴 후 pop명령으로 return address가 있는 주소로 이동하고 리턴하게 된다. 변환 전 test return address 변환 후 test return address 이때 test return address을 임의적으로 test2의 주소로 바꿔놓아 test2의 함수로 점프 하게 된다.

  10. Test2함수 역시 초기화 작업을 거친다. test2함수 내부에 printf 함수를 호출하게 되면 004222A0번의 주소에 test2 문자열을 초기화 시키고 printf함수호출을 통해 test2가 초기화 되어있는 주소를 출력 한다.

  11. test 함수의 종료 시 esp는 test2함수의 ebp(0012FF20)의 주소로 가고, Pop명령으로 통해 값을 ebp에 복사 후 0012FF24주소로 가서 return한다. 주소0012FF24에 초기화 되어있는 값으로 return하게 되고, return하는 주소에는 실행 불가능한 명령이 있어 메모리 오류로 프로그램이 종료 되게 된다.

  12. Main: esp Test: *p Test C Test: ebp Test return address Main: B Test의 인자 B,A Main: A Main: ebp

More Related