首页
社区
课程
招聘
[原创] 使用visualstudio写简单的smc汇编代码
发表于: 2020-11-25 17:29 3601

[原创] 使用visualstudio写简单的smc汇编代码

2020-11-25 17:29
3601

简单介绍

Self-Modifying Code,自修改代码

 

可以提高静态分析的难度,这里简单写一个示例

生成基本的exe

Visual Studio创建一个空项目,在"源文件"文件夹里新建一个 test.c,粘贴内容如下:

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
// InlineAssembler_Calling_C_Functions_in_Inline_Assembly.cpp
// processor: x86
#include <stdio.h>
 
char format[] = "%s %s\n";
char hello[] = "Hello";
char world[] = "World";
 
int main()
{
    __asm
    {
        mov eax, start
        mov ecx, end
        sub ecx, eax
        the_loop:
            mov ebx, [eax]
            xor ebx, 0x23
            mov [eax], ebx
            inc eax
            loop the_loop
        start:
            pushad
            mov  eax, offset world
            push eax
            mov  eax, offset hello
            push eax
            mov  eax, offset format
            push eax
            call printf
            //clean up the stack so that main can exit cleanly
            //use the unused register ebx to do the cleanup
            pop  ebx
            pop  ebx
            pop  ebx
            popad
        end:
    }
    return 0;
}

代码比较简单,大概说一下
the_loop上面的3行汇编代码是获取要异或的区域,开始地址存在eax,长度存在ecx;
the_loop到start是异或的逻辑,这里用0x23异或;
start到end是输出"Hello World"的逻辑

 

然后编译x86版本的exe,注意这个时候的exe是不能正常输出"Hello World"的,因为还没有对start到end的部分做异或处理,另外还有两个默认的PE机制问题,后面再说。

使用IDA对部分代码做异或

IDA打开上一步生成的exe,找到我们要处理的部分,如下:

1
2
3
4
5
6
7
8
9
10
11
12
.text:00411894 60                                pusha
.text:00411895 B8 10 A0 41 00                    mov     eax, offset aWorld ; "world"
.text:0041189A 50                                push    eax
.text:0041189B B8 08 A0 41 00                    mov     eax, offset aHello ; "Hello"
.text:004118A0 50                                push    eax
.text:004118A1 B8 00 A0 41 00                    mov     eax, offset aSS ; "%s %s\n"
.text:004118A6 50                                push    eax
.text:004118A7 E8 21 F8 FF FF                    call    sub_4110CD
.text:004118AC 5B                                pop     ebx
.text:004118AD 5B                                pop     ebx
.text:004118AE 5B                                pop     ebx
.text:004118AF 61                                popa

记下开始地址和结束地址,这里是0x00411894和004118AF,
然后在IDA里,File -> Script file...,运行如下python脚本:

1
2
3
4
5
6
7
8
9
10
# coding:utf8
 
from ida_bytes import get_byte, patch_byte
 
def range_xor(start_addr, end_addr, xor_num):
    for the_addr in range(start_addr, end_addr+1):
        the_byte = get_byte(the_addr)
        patch_byte(the_addr, the_byte ^ xor_num)
 
range_xor(0x00411894, 0x004118AF, 0x23)

之后 Edit -> Patch program -> Apply patches to input file...,这样保存之后,我们就得到异或处理之后的exe了,但现在还是不能正常运行。

处理PE机制造成的问题

上面提到PE的两个机制,导致程序还不能正常运行。
一个是代码段默认不可写,另一个是动态基址,我们使用xpeviewer来修改相应的位置。

 

禁用动态基址:
先把右上角的"只读"取消勾选
IMAGE_NT_HEADERS -> IMAGE_OPTIONAL_HEADER -> DllCharacteristics 取消勾选"DYNAMIC_BASE"

 

设置代码段可写:
区块 -> .text -> 右键编辑 -> 取消右上角的"只读"勾选,Characteristics 点击勾选 "MEM_WRITE"

 

这样之后,就能实现一个最简单的smc例子了。

最后

这个过程只是自己的想法,如果有不合适的地方,请各位大神指正。

收藏
免费 0
支持
分享
最新回复 (1)
雪    币: 174
活跃值: (620)
能力值: ( LV4,RANK:50 )
在线值:
发帖
回帖
粉丝
2
加密软件一般是用个工具,把原代码读取出来,加密后写回去,程序里有就自解码代码,边运行边解密
不知道这方面有没有现成的开源项目?
知名工具因为已经被人琢磨透了反而不如自己改的安全。
2020-11-26 12:28
0
游客
登录 | 注册 方可回帖
返回
//