archim
驱动牛犊
驱动牛犊
  • 注册日期2001-08-18
  • 最后登录2006-03-17
  • 粉丝0
  • 关注0
  • 积分10分
  • 威望1点
  • 贡献值0点
  • 好评度1点
  • 原创分0分
  • 专家分0分
阅读:2366回复:5

这段程序是实现代码变型功能的,大家认为它有没有实用价值呢?

楼主#
更多 发布于:2003-05-08 13:21
【evolve.asm】

;Compile command Line:
;ML /c /coff /Cx /Cp /Gd /Fl\"evolve.lst\" \"evolve.asm\"
;
; Design by PRC
; Completed on 11-27, 2002
; Connect to me[archim@163.com] for technic discussions

.586p
.model flat,stdcall

public Metamorphosize



.code

StartOfEvolutionEngine = $


;
;变形引擎原理:
;   变形代码由2部分组成:
;   1)一段经过32位密钥异或加密的代码
;   2)由引擎随机生成的一段对这段加密代码进行解码的模块
;
;标准的解码模块如下所示:
; ____________________________________________
;|                                           |
;|      pushad                               |
;|      call @0                              |
;|@0:                                        |
;|      pop Index_reg                        |
;|      add Index_reg, offset EncryptedData  |
;|      mov Count_reg, count                 |
;|      mov Key_reg, key                     |
;|@1:                                        |
;|      xor [Index_reg], Key_reg             |
;|      add Index_reg, 4                     |
;|      dec Count_reg                        |
;|      jnz @1                               |
;|      popad                                |
;|___________________________________________|
;
;从7个通用寄存器中随机选择3个分别作为索引寄存器,
;密钥寄存器和计数寄存器。并在解密模块的每条有效语句
;之间随机插入1~3条垃圾代码。

EAX_REG = 0
ECX_REG = 1
EDX_REG = 2
EBX_REG = 3
ESP_REG = 4
EBP_REG = 5
ESI_REG = 6
EDI_REG = 7

;   0 ---- eax
;   1 ---- ecx
;   2 ---- edx
;   3 ---- ebx
;   4 ---- esp
;   5 ---- ebp
;   6 ---- esi
;   7 ---- edi

INDEX_REG   =   0
KEY_REG     =   1
COUNT_REG   =   2
FREE_REG1   =   3
FREE_REG2   =   4
FREE_REG3   =   5
FREE_REG4   =   6

;************************************
;*  Return a random number between  *
;*  0~(X-1)                         *
;************************************
Random:
        ;rdtsc
        push eax
        db 0Fh, 031h
        xor edx, edx
        div dword ptr [esp+8]
        pop eax
        ret 4


routine1:

        mov byte ptr [edi], 0B8h
        mov dl, byte ptr [ebx+ecx]
        add [edi], dl
        inc edi
        ret


;********************************
;*  Generate garbaby statement  *
;********************************
GenerateGarbabyCode:

        push eax

        push 3              ;<
        call Random         ;<Decide how many garbaby statement will be generated.
        lea ecx, [edx+1]    ;<

lp_GenerateGarbabyCode:

        push 4          ;<
        call Random     ;<Decide what register will be used in the statement.
        mov eax, edx    ;<

        push 3          ;<Decide what type of statement will be generated.
        call Random     ;<

        cmp edx, 2
        jz lb_$A001
        cmp edx, 1
        jz lb_$A002

        mov byte ptr [edi], 48h ;<
        push 1                  ;<Generate \'DEC reg\'-type statement
        jmp lb_$A003            ;<

lb_$A001:

        mov byte ptr [edi], 0B8h    ;<
        push 5                      ;<Generate \'MOV REG, DATA\'-type statement
        jmp lb_$A003                ;<

lb_$A002:

        mov word ptr [edi], 0C083h  ;<
        inc edi                     ;<Generate  \'ADD REG, DATA\'-type statement
        push 2                      ;<

lb_$A003:

        add eax, FREE_REG1
        mov dl, [ebx+eax]
        add [edi], dl
        pop eax
        add edi, eax
        loop lp_GenerateGarbabyCode

        pop eax
        ret

;*********************************************************************
;*
;*  Parameters:
;*      [Param1]
;*          Size
;*      [Param2]
;*          Key
;*
;*********************************************************************
;RegTable = $
;@A = $
;           db  0, 1, 2, 3, 5, 6, 7

;GenerateDecodeModule:
GenerateDecodeModule PROC

;       call @0
;@0:
;       pop ebx
;       lea ebx, [ebx][@A-@0]

        push 00070605h
        push 03020100h
        mov ebx, esp

;********************************************************************
;*  Randomly array the elements of the register table, then select  *
;*  the first three ones to use as the index register, the key      *
;*  register and the counter register                               *
;********************************************************************
lb_RandomizeRegisterTableAgain:
        mov ecx, 19796
lp_RandomizeRegTable:
        push 7
        call Random
        xchg edx, esi
        push 7
        call Random
        mov al, [ebx+edx]
        xchg al, [ebx+esi]
        xchg al, [ebx+edx]
        loop lp_RandomizeRegTable

;********************************************************************
;*  Don\'t use the EBP register as the index register, because an    *
;*  statement of \'XOR [EBP], reg2\' is 3-byte-length. And for other  *
;*  registers, a statement of \'XOR [reg1], reg2\' is 2-byte-length   *
;********************************************************************
        cmp byte ptr [ebx][INDEX_REG], EBP_REG
        jz lb_RandomizeRegisterTableAgain

        call @0
@0:
        pop esi
        add esi, GenerateGarbabyCode-@0

        mov [edi], byte ptr 60h
        inc edi
        call esi    ;Generate garbaby code

        mov [edi], byte ptr 0E8h
        inc edi
        push 13
        call Random
        add edx, 6
        mov [edi], edx
        add edi, 4
        push edi
        lea edi, [edi+edx]

        mov byte ptr [edi], 58h
        mov al, [ebx][INDEX_REG]
        add [edi], al
        inc edi
        call esi    ;Generate garbaby code

        mov word ptr [edi], 0C083h
        inc edi
        add [edi], al
        inc edi
        push edi
        inc edi
        call esi    ;Generate garbaby code

        push KEY_REG
        pop ecx
        call routine1
        ;mov [edi], dword ptr TOTAL_DWORDS
        mov edx, [esp+00000008h][00000008h+8]
        mov [edi], edx
        add edi, 4
        call esi

        push COUNT_REG
        pop ecx
        call routine1
        ;mov [edi], dword ptr ENCRYPT_KEY
        mov edx, [esp+00000004h][00000008h+8]
        mov [edi], edx
        add edi, 4
        call esi

        push edi

        mov word ptr [edi], 0031h
        movzx edx, byte ptr [ebx][KEY_REG]
        shl edx, 3
        add dl, [ebx][INDEX_REG]
        inc edi
        add [edi], dl
        inc edi
        call esi

        mov dword ptr [edi], 4804C083h
        inc edi
        add [edi], al
        inc edi
        inc edi

        mov dl, [ebx][COUNT_REG]
        add [edi], dl
        inc edi

        mov dword ptr [edi], 00610075h

        lea ecx, [edi+1]
        pop eax
        sub eax, edi
        dec eax
        dec eax
        mov [ecx], al
        inc edi
        inc edi
        inc edi

        pop eax
        pop edx
        mov ecx, edi
        sub ecx, edx
        mov [eax], cl

        add esp, 8
        ret 8

GenerateDecodeModule ENDP


SIZE_OF_ENGINE = $-StartOfEvolutionEngine

Metamorphosize PROC

        db 0Fh, 031h


        push esi
        push eax
        push ecx
        push edi

        push eax
        push ecx
        call GenerateDecodeModule

        pop edx
        pop ecx
        pop eax
        pop esi

        sub edx, edi
        neg edx
        push edx

lp_EncryptData:
        mov edx, [esi]
        xor edx, eax
        mov [edi], edx
        add esi, 4
        add edi, 4
        loop lp_EncryptData

        pop eax
        ret

Metamorphosize ENDP

End




【test.c】

#include <windows.h>
#include <stdio.h>
#include <conio.h>

/*
    为了使程序可以正常运行,请打开\"Project\" --> \"Settings\" --> \"Link\"
    在Category里选择\"General\",然后把\"Link incrementally\"选项去掉。
*/

/*****************************************************************

    变形引擎测试程序
        调用Metamorphosize函数随机生成100种变形代码。然后分别运行
        这些变形代码以验证其功能的正确性,并将变形代码存放在.evo
        文件中。

*****************************************************************/

void WINAPI Metamorphosize(void);

static char cMetamorphousCode[0x800];
static char msg_string[] = \"Testing fuction running properly!\";
static int (*fn_puts)(const char *) = puts;

/*
int WINAPI testing_function()
{
    int (*fn_puts)(const char *) = puts;
    //char str[100] = \"Testing fuction running normally!\";
    char *str = \"Testing fuction running normally!\";

    fn_puts(str);
    return 0;
}
*/

__declspec(naked) int WINAPI testing_function()
{
    __asm
    {
        mov ecx, offset msg_string
        push ecx
        call dword ptr [fn_puts]
        add esp, 4

        xor eax, eax
        ret
    }

}

void do_nothing()
{
    return;
}

typedef int (*TSETING_FUNCTION)();

void GenerateMetamorphousCode(PVOID lpRawCode, PVOID lpMetamorphousCode,
    ULONG ulNumberOfDwords)
{
    static int iCount = 1;
    char cFileName[256];
    FILE *fp;
    int iSizeOfDecodeModule;

    TSETING_FUNCTION f = (TSETING_FUNCTION) cMetamorphousCode;

    __asm
    {
        push esi
        push edi
        mov esi, lpRawCode
        mov edi, lpMetamorphousCode
        mov ecx, ulNumberOfDwords
        call Metamorphosize
        pop edi
        pop esi
        mov iSizeOfDecodeModule, eax
    }

    // run the metamorphosized code
    f();

    // store the metamorphosized code
    strcpy(cFileName, \"Code\");
    sprintf(&cFileName[4], \"%04d\", iCount);
    strcat(cFileName, \".evo\");
    iCount++;

    fp = fopen(cFileName, \"wb\");
    if( !fp )
    {
        return;
    }

    fwrite(lpMetamorphousCode, 1, ulNumberOfDwords*sizeof(ULONG)+iSizeOfDecodeModule, fp);
    fclose(fp);
}

int main(char *argv, int argc)
{
    int i;
    ULONG dwNumberOfDwords;

    //testing_function();

    dwNumberOfDwords = ( (ULONG)do_nothing - (ULONG)testing_function + 3 ) / 4;

    for(i=0; i<100; i++)
    {
        GenerateMetamorphousCode(
            testing_function,
            cMetamorphousCode,
            dwNumberOfDwords
            );
    }

    return 0;
}
archim
VanCheer
驱动老牛
驱动老牛
  • 注册日期2002-02-21
  • 最后登录2003-08-28
  • 粉丝0
  • 关注0
  • 积分-20分
  • 威望-10点
  • 贡献值0点
  • 好评度0点
  • 原创分0分
  • 专家分0分
沙发#
发布于:2003-05-09 14:11
俺们论坛有人贴过了
http://bbs.logincom.com/bbs/cgi-bin/leoboard.cgi
[img]http://www.driverdevelop.com/forum/upload/VanCheer/2003-03-21_mon.gif[/img][img]http://www.driverdevelop.com/forum/upload/VanCheer/2002-12-07_smallbaby.jpg[/img]
archim
驱动牛犊
驱动牛犊
  • 注册日期2001-08-18
  • 最后登录2006-03-17
  • 粉丝0
  • 关注0
  • 积分10分
  • 威望1点
  • 贡献值0点
  • 好评度1点
  • 原创分0分
  • 专家分0分
板凳#
发布于:2003-05-09 16:55
那里的帖子也是我贴的:)
archim
liuyan1
驱动老牛
驱动老牛
  • 注册日期2001-08-27
  • 最后登录2023-04-18
  • 粉丝0
  • 关注0
  • 积分1031分
  • 威望477点
  • 贡献值0点
  • 好评度187点
  • 原创分0分
  • 专家分0分
  • 社区居民
地板#
发布于:2003-05-10 15:41
SMC (Self Modifiy Code )
把?
这种技术
楼上的客,楼下的客,听我老坎说明白,要苛屎有草纸,不要扯我的麦席子,要苛尿有夜壶,不要在床上划地图。
yefani
驱动牛犊
驱动牛犊
  • 注册日期2003-05-14
  • 最后登录2004-07-28
  • 粉丝0
  • 关注0
  • 积分0分
  • 威望0点
  • 贡献值0点
  • 好评度0点
  • 原创分0分
  • 专家分0分
地下室#
发布于:2003-05-14 22:47
不会吧,这么长
晕~~~~~~~~~~~~~~~了
archim
驱动牛犊
驱动牛犊
  • 注册日期2001-08-18
  • 最后登录2006-03-17
  • 粉丝0
  • 关注0
  • 积分10分
  • 威望1点
  • 贡献值0点
  • 好评度1点
  • 原创分0分
  • 专家分0分
5楼#
发布于:2003-05-15 12:35
才几百行的程序也算长呀?
更晕!
archim
游客

返回顶部