SECURITY/REVERSING

์–ด์…ˆ๋ธ”๋ฆฌ ์ฝ”๋“œ ๋ถ„์„ 3 - example 3 :: ์กฐ๊ฑด๋ฌธ

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

 

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

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

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

 

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

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

iforint.tistory.com

 

.file	"example3.c"
.section	.rodata
.LC0:
.string	"a is 10"
.LC1:
.string	"b is 10"
.LC2:
.string	"b is 20"
.LC3:
.string	"a=b"
.LC4:
.string	"a!=b"
.text
.globl	main
.type	main, @function
main:
.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	$10, -8(%rbp)
movl	$20, -4(%rbp)
cmpl	$10, -8(%rbp)
jne	.L2
movl	$.LC0, %edi
call	puts
.L2:
cmpl	$10, -4(%rbp)
jne	.L3
movl	$.LC1, %edi
call	puts
jmp	.L4
.L3:
cmpl	$20, -4(%rbp)
jne	.L4
movl	$.LC2, %edi
call	puts
.L4:
movl	-8(%rbp), %eax
cmpl	-4(%rbp), %eax
jne	.L5
movl	$.LC3, %edi
call	puts
jmp	.L6
.L5:
movl	$.LC4, %edi
call	puts
.L6:
movl	$0, %eax
leave
.cfi_def_cfa 7, 8
ret
.cfi_endproc
.LFE0:
.size	main, .-main
.ident	"GCC: (Ubuntu 4.8.4-2ubuntu1~14.04.1) 4.8.4"
.section	.note.GNU-stack,"",@progbits

.LC0:
.string "a is 10"
.LC1:
.string "b is 10"
.LC2:
.string "b is 20"
.LC3:
.string "a=b"
.LC4:
.string "a!=b"

 

LC0, LC1, LC2, LC3, LC4 ์— ๋ฌธ์ž์—ด ํ• ๋‹น

 

 

movl $10, -8(%rbp) : -8(%rbp) ์— 10 ์ €์žฅ
movl $20, -4(%rbp) : -4(%rbp) ์— 20 ์ €์žฅ
cmpl $10, -8(%rbp) : -8(%rbp) ์˜ ๊ฐ’(10) ๊ณผ 10 ๋น„๊ต

์ฆ‰, ( -8(%rbp) - 10 )

 

jne .L2 : ๋น„๊ต ๊ฒฐ๊ณผ๊ฐ€ ๋‹ค๋ฅด๋ฉด L2 ๋กœ ์ ํ”„.

์ฆ‰, ZF ๊ฐ€ 0 ์ด์–ด์•ผ ์ ํ”„


movl $.LC0, %edi
call puts

 


.L2:


cmpl $10, -4(%rbp) : -4(%rbp) ์˜ ๊ฐ’ (20) ๊ณผ 10 ๋น„๊ต

jne .L3 : ๋น„๊ต ๊ฒฐ๊ณผ๊ฐ€ ๋‹ค๋ฅด๋ฉด L3 ๋กœ ์ ํ”„

movl $.LC1, %edi
call puts

 

jmp .L4 : L4 ๋กœ ์ ํ”„


.L3:
cmpl $20, -4(%rbp) : -4(%rbp) ์˜ ๊ฐ’ (20) ๊ณผ 20 ๋น„๊ต

jne .L4 : ๋น„๊ต ๊ฒฐ๊ณผ๊ฐ€ ๋‹ค๋ฅด๋ฉด L4 ๋กœ ์ ํ”„

movl $.LC2, %edi
call puts


.L4:
movl -8(%rbp), %eax
cmpl -4(%rbp), %eax

jne .L5
movl $.LC3, %edi
call puts
jmp .L6


.L5:
movl $.LC4, %edi
call puts


.L6:
movl $0, %eax

 

#include <stdio.h>

int main(){
    int a = 10;
    int b = 20;
    
    if (a == 10)
    	puts("a is 10");
    else if (b == 10)
    	puts("b is 10");
    else if (b == 20)
        puts("b is 20");
        
    if (a == b) puts("a=b");
    else puts("a!=b");
    
    return 0;
}  		

์ด์ƒํ•˜๋‹ค...

 

gcc ๋กœ ์–ด์…ˆ๋ธ”๋ฆฌ ์ฝ”๋“œ๋ฅผ ์ƒ์„ฑํ•œ ๊ฒฐ๊ณผ, ์ฒซ๋ฒˆ์งธ ๋ถ„๊ธฐ์—์„œ L3 ๋กœ ์ ํ”„ํ•œ๋‹ค. 

๊ทธ๋ž˜์„œ ์กฐ๊ฑด๋ฌธ์˜ ์ˆœ์„œ๊ฐ€ L3 L4 ๊ฐ€ ์•„๋‹ˆ๋ผ L4 L3 ๊ฐ€ ๋œ ๊ฒƒ์ธ๊ฑฐ ๊ฐ™์€๋ฐ.. 

๊ทธ๋Ÿผ ์˜ˆ์ œ์˜ ์–ด์…ˆ๋ธ”๋ฆฌ ์ฝ”๋“œ์™€ ๋‹ค๋ฅด๋‹ค.

 

 

 

์–ด์…ˆ๋ธ”๋ฆฌ ์ฝ”๋“œ์—์„œ ์กฐ๊ฑด๋ฌธ์€ cmp ์™€ ๋Œ€์‘๋˜๋Š”๋ฐ, JMP ๋Š” else (if) ์™€ ๋Œ€์‘๋˜๋Š” ๊ฒƒ์œผ๋กœ ์ƒ๊ฐํ•˜๋ฉด ๋  ๊ฒƒ ๊ฐ™๋‹ค.

์ง€๊ธˆ L1 ์—์„œ ๋งˆ์ง€๋ง‰์— jmp ๊ฐ€ ์žˆ๋Š” ๊ฑธ๋กœ ๋ณด์•„ ์ดํ›„ ๋‹จ๊ณ„๋ฅผ ์ฝ”๋“œ๋ฅผ ๋”ฐ๋ผ ์ˆœ์ฐจ์ ์œผ๋กœ ์ง„ํ–‰๋˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ else (if) ๋ฅผ ํ†ตํ•ด์„œ ์ ํ”„ํ•ด๊ฐ€๋Š” ๊ฒฝ๋กœ๊ฐ€ ์žˆ์Œ์„ ์•Œ๊ฒŒ ๋˜์—ˆ๋‹ค. 

๊ทธ๋Ÿฌ๋‹ˆ๊นŒ if(a==10) ๋’ค์—๋Š” else if ๊ฐ€ ์˜ค์ง€ ์•Š์•„์•ผ ํ•œ๋‹ค๋Š” ๋œป์ด ๋œ๋‹ค.

 

 

๊ทธ๋ ‡๊ฒŒ ์ƒ๊ฐํ•˜๊ณ  ์ฝ”๋“œ๋ฅผ ์ˆ˜์ •ํ•˜๋‹ˆ, ๋‹ค์Œ๊ณผ ๊ฐ™์•˜๊ณ , ์–ด์…ˆ๋ธ”๋ฆฌ ์ฝ”๋“œ๋กœ ๋งŒ๋“œ๋‹ˆ ์˜ˆ์ œ ์ฝ”๋“œ์™€ ๊ฐ™์€ ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์™”๋‹ค.!!

 

 

#include <stdio.h>

int main(){
    int a = 10;
    int b = 20;
    
    if (a == 10)
    	puts("a is 10");
    if (b == 10)
    	puts("b is 10");
    else if (b == 20)
        puts("b is 20");
        
    if (a == b) puts("a=b");
    else puts("a!=b");
    
    return 0;
}