MIG是一款由Mozilla开发的分布式取证开源框架。MIG即使检测数千台服务器速度依旧很快,其只专注于在大量的系统中搜索正则表达式和字符串,非常高效。

近来我在奥斯丁举办的DFIR峰会上展示MIG时,其中一位与会人员向我提出了一个问题“它可以用来检测未知或者伪造的系统么?”最好的办法就是执行一种检测方法来观察其网络,特别是伪造主机的出站连接或者恶意软件建立的一个C&C服务。MIG还可以通过检测远程系统的ARP表以及与已知系统的本地MAC地址交叉引用结果来继续探测。在已知系统中任何未配置MAC地址的都可能是伪造代理。

首先,我们需要在已知系统ARP表中检索所有的MAC地址,netstat模块可以通过邻近MAC地址来匹配正则表达式“^[0-9a-f]”(匹配所有的十六进制)来完成这个任务。

$ mig netstat -nm "^[0-9a-f]" > /tmp/seenmacs

将结果保存到/tmp/seenmacs,并使用一些bash列出独特的MAC地址

$ awk '{print tolower($5)}' /tmp/seenmacs | sort | uniq
00:08:00:85:0b:c2
00:0a:9c:50:b4:36
00:0a:9c:50:bc:61
00:0c:29:41:90:fb
00:0c:29:a7:41:f7
00:10:db:ff:10:00
00:10:db:ff:30:00
00:10:db:ff:f0:00
00:21:53:12:42:c1

接着我们需要检测每一个已经配置MAC地址的已知代理,当然我们可以继续使用netstat模块来完成这个任务。这一次通过-lm来查询本地MAC地址。

现在MACs列表可能已经很长了,使用下面脚本我们将其分为50个一组。

#! /usr/bin/env bash
i=50
input=$1
output=$2
while true
do
    echo -n "mig netstat " >> $output
    for mac in $(awk '{print tolower($5)}' $1|sort|uniq|head -$i|tail -50)
        do
        echo -n "-lm $mac " >> $output
    done
    echo >> $output
    i=$((i+50))    if [ $i -gt $(awk '{print tolower($5)}' $1|sort|uniq|wc -l) ]
    then
        exit 0
    fi
done

该脚本将netstat命令的最大参数限制为50,参数1调用/tmp/seenmacs,参数2为输出文件。

$ bash /tmp/makemigmac.sh /tmp/seenmacs /tmp/migsearchmacs

/tmp/migsearchmacs包含许多MIG  netstat指令用于通过已知主机配置接口搜索MAC地址,运行指令并将结果输出到文件

$ for migcmd $(cat /tmp/migsearchmacs); do $migcmd >> /tmp/migfoundmacs; done

现在我们有一个包含MAC地址文件,以及一个已知系统下配置了MAC地址的文件。在bash中做两个判别式很简单

$ for seenmac in $(awk '{print tolower($5)}' /tmp/seenmacs|sort|uniq); do
    hasseen=""; hasseen=$(grep $seenmac /tmp/migfoundmacs)
        if [ "$hasseen" == "" ]; then
        echo "$seenmac is not accounted for"
    fi
done
00:21:59:96:75:7f is not accounted for
00:21:59:98:d5:bf is not accounted for
00:21:59:9c:c0:bf is not accounted for
00:21:59:9e:3c:3f is not accounted for
00:22:64:0e:72:71 is not accounted for
00:23:47:ca:f7:40 is not accounted for
00:25:61:d2:1b:c0 is not accounted for
00:25:b4:1c:c8:1d is not accounted for

自动化检测

定时运行这个程序是个不错的主意哟,下面的脚本能够实现自动化操作,并将最后生成的报告邮寄给你最喜欢的安全团队。

#!/usr/bin/env bash
SEENMACS=$(mktemp)
SEARCHMACS=$(mktemp)
FOUNDMACS=$(mktemp)
echo "seen mac addresses are in $SEENMACS"
echo "search commands are in $SEARCHMACS"
echo "found mac addresses are in $FOUNDMACS"
echo "step 1: obtain all seen MAC addresses"
$(which mig) netstat -nm "^[0-9a-f]" 2>/dev/null | grep 'found neighbor mac' | awk '{print tolower($5)}' | sort | uniq > $SEEN
MACSMACCOUNT=$(wc -l $SEENMACS | awk '{print $1}') 
echo "$MACCOUNT MAC addresses found"echo "
step 2: build MIG commands to search for seen MAC addresses"
i=50
while true;
do
    echo -n "$i.."
    echo -n "$(which mig) netstat -e 50s " >> $SEARCHMACS
    for mac in $(cat $SEENMACS | head -$i | tail -50)
    do        
         echo -n "-lm $mac " >> $SEARCHMACS
    done
            
    echo -n " >> $FOUNDMACS" >> $SEARCHMACS
    if [ $i -gt $MACCOUNT ]    
    then        
        break    
    fi    
    echo " 2>/dev/null &" >> $SEARCHMACS    
    i=$((i+50))
done
echo
echo "step 3: search for MAC addresses configured on local interfaces"
bash $SEARCHMACS
sleep 60

echo "step 4: list unknown MAC addresses"
for seenmac in $(cat $SEENMACS)
do    
    hasseen=$(grep "found local mac $seenmac" $FOUNDMACS)
    if [ "$hasseen" == "" ]; then        
       echo "$seenmac is not accounted for"    
    fi
done

未知MACs列表可以用来研究端点,他们可能是交换器,路由器,或者其他不需要运行MIG代理的网络设备。再或者他们可能是伪造端点,这就需要你注意了

* 参考来源Linuxwall,译者/鸢尾 转载请注明来自FreeBuf黑客与极客(FreeBuf.COM)

源链接

Hacking more

...