---
title: "挖矿病毒简易处理方法"
author: "lvjun106"
date: "2019-10-18T14:37:46Z"
tags: []
categories: ["操作系统"]
slug: "untitled"
draft: false
lastmod: "2019-10-18T14:37:46Z"
id: 2509002
url: "https://lvjun106.iteye.com/blog/2509002"
---

实在是被逼无奈，才想出如此招数！

 

服务器老是被攻击，cpu占用特别高，而且特别高的进程是httpd（我知道有程序漏洞，but程序不是我写的）。

 

第一种情况：

 

 

httpd的占用已经达到99.8%了

第一种解决办法

 

当cpu占用超过95%同时60s间隔内httpd的 pid相同时候 则杀死该pid

 

代码：

 

```
#!/bin/sh

# qiyulin to monitor used CPU

record=0

while true;

do

cpu=$(top -b -n1 | grep "httpd" | head -1 | awk '{print $9}')

pid=$(top -b -n1 | grep "httpd" | head -1 | awk '{print $1}')

#cpu check

result=${cpu/.*}

if [[ $record == $pid ]];then kill -9 $pid;echo "$pid was killed";fi

if [[ $result > 95 || $result == 100 ]];then let record=${pid};else let record=0;fi

#echo

echo `date +%F" "%H:%M:%S`+" cpu:$result% record pid:$record pid:$pid"

sleep 60

done
```

 

这样服务器平稳的运行了一段时间，至少保证不会再死了，后来又出现了状况2

 

第二种情况

 

top中没有特别高的cpu占用，但是总的mem占用超过了99%

 

 

唉！表示很无奈，怎么办呢？ 当used使用超过95%的值的时候则 重启httpd

 

代码：

 

```
#!/bin/sh

# qiyulin to monitor used CPU

record=0

while true;

do

cpu=$(top -b -n1 | grep "httpd" | head -1 | awk '{print $9}')

pid=$(top -b -n1 | grep "httpd" | head -1 | awk '{print $1}')

#cpu check

result=${cpu/.*}

if [[ $record == $pid ]];then kill -9 $pid;echo "$pid was killed";fi

if [[ $result > 95 || $result == 100 ]];then let record=${pid};else let record=0;fi

#mem check

mem=$(free -m | awk 'NR==2 {print $3}')

if [[ $mem > 3638 ]];then apache-restart;echo "$mem is out 95%,so the httpd restart";fi

#echo

echo `date +%F" "%H:%M:%S`+" cpu:$result% record pid:$record pid:$pid mem:$mem"

sleep 60

done

```

 

无奈之举....

哦对了，忘了说了这里如果想要 你写的脚本在后台飞~~~

 

需要使用命令如下：

 

nohup sh ./checkCpu.sh &

 

这样用户 即使退出则该脚本也会继续运行！

如何杀死该脚本

 

ps -ax #查看所有的进程pid

kill -9 pid //杀死一个某个pid