有一台vps被弱口令上马了

翻来翻去

找到个二进制文件如下

前言

记一次挖矿木马样本分析_木马分析

搜main函数关键字可以判断是用shc加密shell脚本生成的二进制文件

记一次挖矿木马样本分析_木马分析_02

记一次挖矿木马样本分析_shell脚本_03

在0000000000400F7E位置函数,找到了加载shell命令的位置

记一次挖矿木马样本分析_挖矿木马_04

shc部分源码

/* shc.c */

/**
 * This software contains an ad hoc version of the 'Alleged RC4' algorithm,
 * which was anonymously posted on sci.crypt news by cypherpunks on Sep 1994.
 *
 * My implementation is a complete rewrite of the one found in
 * an unknown-copyright (283 characters) version picked up from:
 *    From: allen@gateway.grumman.com (John L. Allen)
 *    Newsgroups: comp.lang.c
 *    Subject: Shrink this C code for fame and fun
 *    Date: 21 May 1996 10:49:37 -0400
 * And it is licensed also under GPL.
 *
 *That's where I got it, now I am going to do some work on it
 *It will reside here: http://github.com/neurobin/shc
 */

static const char my_name[] = "shc";
static const char version[] = "Version 4.0.3";
static const char subject[] = "Generic Shell Script Compiler";
static const char cpright[] = "GNU GPL Version 3";
static const struct { const char * f, * s, * e; }
    provider = { "Md Jahidul", "Hamid", "<jahidulhamid@yahoo.com>" };

尝试生成一个echo “helloworld”,看看shc生成的文件是什么构造

shc

安装shc

sudo add-apt-repository ppa:neurobin/ppa
sudo apt-get update
sudo apt-get install shc

加密后会得到一份生成的c源码和可执行文件

[04:08:08] ctfshow@ubuntu /home/ctfshow/Desktop/test (0) 
> shc -f ./test.sh 
[04:08:11] ctfshow@ubuntu /home/ctfshow/Desktop/test (0) 
> ls
test.sh  test.sh.x*  test.sh.x.c
[04:08:12] ctfshow@ubuntu /home/ctfshow/Desktop/test (0) 
> ./test.sh.x 
hello

会输出一个test.sh.c和编译好的test.sh.x


那么可以照着test.sh.c的源码来快速分析手上的二进制文件

调试发现ret会记录当前进程是否为父进程,

调试发现如果为父进程,则执行的命令是

exec bash ./<程序自己>

那么相当于把代码在子进程里面又跑了一遍

这个时候ret就是1了,加载的也会是text里面真正的代码段

记一次挖矿木马样本分析_木马分析_05

思路

程序把shell命令用rc4加密在了硬编码里面,回到样本,只要更改ret的值然后调到execvp 然后print mem就能得到shell脚本了。

帮助网安学习,全套资料S信免费领取:

① 网安学习成长路径思维导图

② 60+网安经典常用工具包

③ 100+SRC分析报告

④ 150+网安攻防实战技术电子书

⑤ 最权威CISSP 认证考试指南+题库

⑥ 超1800页CTF实战技巧手册

⑦ 最新网安大厂面试题合集(含答案)

⑧ APP客户端安全检测指南(安卓+IOS)

patch && dump mem

修改ret值

记一次挖矿木马样本分析_挖矿木马_06

在memcpy下断

记一次挖矿木马样本分析_木马分析_07

记一次挖矿木马样本分析_挖矿木马_08

祖传字符串脚本

base  =0x000000000602B83
end = 0x00000000006074F0

ans=[]

for i in range(base,end):
    tmp = idc.get_wide_byte(i)
    ans.append(tmp)
    if(tmp == 0):
        print(bytes(ans))
        ans=[]

记一次挖矿木马样本分析_木马分析_09

shlll = b''

with open("sh.tmp", "w") as f:
    print(shlll.decode(),file=f)

暂且写个脚本存一下

shell分析

到这一步就比较明了了

shell脚本里面存的命令全是用明文显示的

记一次挖矿木马样本分析_挖矿木马_10

首先是删除日志和竞品矿机,然后设置iptable

释放iptable_reject

然后从远程服务器下载矿机

记一次挖矿木马样本分析_挖矿木马_11

其中一个ip是172.104.170.240

上网搜一下ip是一个矿池

记一次挖矿木马样本分析_木马分析_12

搜索矿池ip发现样本行为和安天于今年5月发布的yayayaminer有一定相似之处,在初期的排查阶段借鉴了其思路。