SECURITY/REVERSING

์–ด์…ˆ๋ธ”๋ฆฌ ์ฝ”๋“œ ๋ถ„์„ 4 - example 4 :: ํ•จ์ˆ˜

\b\t 2020. 7. 15. 15:58

* ์ด ๊ฒŒ์‹œ๊ธ€์€ AT&T ๋ฌธ๋ฒ•์— ๋”ฐ๋ผ ์ž‘์„ฑ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.

(AT&T ์™€ Intel ์ฐจ์ด ์ฐธ๊ณ -

2020.07.08 - [SECURITY/REVERSING] - REVERSING ์‹œ์ž‘ํ•˜๊ธฐ - ๊ธฐ์ดˆ ๊ฐœ๋… 4 :: ์–ด์…ˆ๋ธ”๋ฆฌ ์–ธ์–ด )

 

REVERSING ์‹œ์ž‘ํ•˜๊ธฐ - ๊ธฐ์ดˆ ๊ฐœ๋… 4 :: ์–ด์…ˆ๋ธ”๋ฆฌ ์–ธ์–ด

์–ด์…ˆ๋ธ”๋ฆฌ ์–ธ์–ด : ์ž์—ฐ์–ด์™€ ๊ธฐ๊ณ„์–ด ์‚ฌ์ด๋กœ, CPU ์˜ ๋ช…๋ น์–ด๋“ค์„ ์˜์–ด์˜ ์•ฝ์ž์ธ ๊ธฐํ˜ธ๋กœ ํ‘œ๊ธฐํ•œ ๊ฒƒ์ด๋‹ค. ์ด ๊ธฐํ˜ธ๋“ค์€ CPU ์˜ ๋ช…๋ น์–ด(๊ธฐ๊ณ„์–ด) ์™€ ์ผ๋Œ€์ผ ๋Œ€์‘ํ•œ๋‹ค. ์ปดํ“จํ„ฐ ๊ตฌ์กฐ์— ๋”ฐ๋ผ ์‚ฌ์šฉํ•˜๋Š” ๊ธฐ๊ณ„์–ด

iforint.tistory.com

 

.file	"example4.c"
.section	.rodata
.LC0:
.string	"result : %d\n"
.text
.globl	function
.type	function, @function
function:
.LFB0:
.cfi_startproc
pushq	%rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
movq	%rsp, %rbp
.cfi_def_cfa_register 6
subq	$16, %rsp
movl	%edi, -4(%rbp)
movl	%esi, -8(%rbp)
movl	-4(%rbp), %eax
imull	-8(%rbp), %eax
movl	%eax, %esi
movl	$.LC0, %edi
movl	$0, %eax
call	printf
leave
.cfi_def_cfa 7, 8
ret
.cfi_endproc
.LFE0:
.size	function, .-function
.globl	main
.type	main, @function
main:
.LFB1:
.cfi_startproc
pushq	%rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
movq	%rsp, %rbp
.cfi_def_cfa_register 6
subq	$16, %rsp
movl	$50, -8(%rbp)
movl	$60, -4(%rbp)
movl	-4(%rbp), %edx
movl	-8(%rbp), %eax
movl	%edx, %esi
movl	%eax, %edi
call	function
movl	$0, %eax
leave
.cfi_def_cfa 7, 8
ret
.cfi_endproc
.LFE1:
.size	main, .-main
.ident	"GCC: (Ubuntu 4.8.4-2ubuntu1~14.04.1) 4.8.4"
.section	.note.GNU-stack,"",@progbits

 

์ด๋ฒˆ์—๋Š” ์ƒˆ๋กœ์šด ํ•จ์ˆ˜ function ์ด ๋“ฑ์žฅํ•œ๋‹ค.

function ์˜ ๋‚ด์šฉ์ด ํ—ท๊ฐˆ๋ ค์„œ ๋‘ ๊ฐ€์ง€๋ฅผ ๋งŒ๋“ค๊ณ  ๋น„๊ตํ•ด๋ณด์•˜๋‹ค.

 

์ฒซ ๋ฒˆ์งธ๋Š” a = a*b 

 

#include <stdio.h>

int function(int a, int b) {
    a = a * b;
    printf("result : %d\n", a);

    return 0;
}


int main() {
    int a = 50, b = 60;
    function(a, b);
    return 0;
}

 

 

a ๊ฐ€ ์žˆ๋˜ 8(%ebp) ์— ์ €์žฅ๋˜๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

 

 

๋‘ ๋ฒˆ์งธ๋Š” printf ์—์„œ a*b  

#include <stdio.h>

int function(int a, int b) {
    // int a2 = a, b2 = b;
    printf("result : %d\n", a*b);

    return 0;
}


int main() {
    int a = 50, b = 60;
    function(a, b);
    return 0;
}

 

 

 

์ฃผ์–ด์ง„ ์˜ˆ์ œ ์ฝ”๋“œ์˜ ๋‚ด์šฉ์ด ๋‚˜์˜จ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

 

 

imull -8(%rbp), %eax

 

์ด ๋ถ€๋ถ„์ด ํ—ท๊ฐˆ๋ฆฐ ๊ฒƒ์ด์—ˆ๋Š”๋ฐ,

eax ์— ์—ฐ์‚ฐ ๊ฒฐ๊ณผ๋ฅผ ์ €์žฅ ํ•œ๋‹ค๋Š” ๊ฒƒ์€ eax ์Šคํƒ์— push ํ•˜๋Š” ๊ฒƒ์ด๋ฏ€๋กœ a ์— ๋Œ€์ž…ํ•˜๋Š” ๊ฒŒ ์•„๋‹˜์„ ์•Œ๊ฒŒ ๋˜์—ˆ๋‹ค.

 

 

๋Œ“๊ธ€์ˆ˜0