본문으로 바로가기

ASM to C with IDA - 053

category Reversing/ASM to C 2018. 7. 12. 09:45

C300_053



C300_소스코드



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <stdio.h>
 
#define KOREA    "대한민국"
 
char* My_strcpy(char* dest, const char* src);
 
void main(void)
{
    char string[100];
 
    My_strcpy(string, KOREA);
 
    puts(string);
}
 
char* My_strcpy(char* dest, const char* src)
{
    if (dest == (int)NULL || src == (int)NULL)
    {
        if (*dest != (int)NULL*dest = (int)NULL;
        return NULL;
    }
 
    do
    {
        *dest++ = *src;
    } while (*src++ != (int)NULL);
 
    return dest;
}
cs



IDA - Layout Graph






IDA - Text View




movsx: 부호 있는 산술 값에 사용되기 때문에 피연산자의 왼쪽 비트를 부호비트로 채운다.

바이트나 워드를 목적지에 전달한다.


My_strcpy 함수에서 먼저 어떠한 코드를 실행하고 조건을 통해 분기 및 반복 하므로 do~while로 반복문을 구성하였다.


반환 값이 두 경우로 나뉜다.

한 경우는 NULL을 반환할 때, 다른 한 경우는 dest를 반환할 때 이다. 이것을 보고 My_strcpy 반환형을 결정했다.


pseudo code - 의사코드



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <stdio.h>
 
char * My_strcpy(char *dest, char *src)
{
    int var_4;
 
    if(dest == NULL || src == NULL){
        eax = dest;
        ecx = [eax];
        if(ecx != 0){
            edx = dest;
            edx =0;
        }
    }
 
    do{
        eax = dest;
        ecx = src;
        dl = [ecx];
        [eax] = dl;
        eax = dest;
        eax = eax + 1;
        dest = eax;
        ecx = src;
        edx = [ecx]; 
        var_4 = edx;
        eax = src;
        eax = eax + 1;
        src = eax;
    }while(var_4!=0)
    
    eax = dest;
}
 
 
 
main()
{
    char string[100];
    eax = string;
    My_strcpy(string, offset src);
 
    ecx = string;
    puts(ecx);
 
    eax = 0;
}
 
 
 
cs




pseudo code - 최종 의사코드



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <stdio.h>
 
char *My_strcpy(char *dest, char *src)
{
    int var_4;
 
    if(dest == NULL || src == NULL){
        if(*dest != 0){
            *dest == NULL;
            return NULL;
        }
    }
 
    do{
        *dest = *src;
        dest++;
    }while(*src++ != NULL)
    
    return dest;
}
 
 
 
main()
{
    char string[100];
    eax = string;
    My_strcpy(string, offset src);
 
    ecx = string;
    puts(ecx);
 
    return 0;
}
 
 
 
cs



offset src = "대한민국"










'Reversing > ASM to C' 카테고리의 다른 글

ASM to C with IDA - 055 (strncmp)  (0) 2018.07.12
ASM to C with IDA - 054 (strcmp)  (0) 2018.07.12
ASM to C with IDA - 052 (strcpy)  (0) 2018.07.11
ASM to C with IDA - 051  (0) 2018.07.11
ASM to C with IDA - 050  (0) 2018.07.11