Sinkholing Suspicious Scripts or Executables on Linux
当需要分析可疑代码时,沙箱环境是理想的执行场所。若缺乏完整沙箱或希望避免网络噪声,可通过sinkhole或NULL-route重定向流量(即数据包不会通过常规网络和默认网关发送)。
通过/proc
[1]虚拟文件系统检查进程时,存在"route"文件:
1
2
3
4
5
6
|
remnux@remnux:~$ cat /proc/1180/net/route
Iface Destination Gateway Flags RefCnt Use Metric Mask MTU Window IRTT
ens19 00000000 01FEA8C0 0003 0 0 100 00000000 0 0 0
ens18 004A10AC 00000000 0001 0 0 0 00FFFFFF 0 0 0
ens19 00FEA8C0 00000000 0001 0 0 0 00FFFFFF 0 0 0
ens19 01FEA8C0 00000000 0005 0 0 100 FFFFFFFF 0 0 0
|
该文件显示进程分配的IP路由表。IP地址通常以小端十六进制编码,可通过Python解码:
1
2
3
4
|
gw = "01FEA8C0"
octets = [gw[i:i+2] for i in range(0, len(gw), 2)]
ip = '.'.join(str(int(o, 16)) for o in octets)
print(ip) # 输出: 1.254.168.192
|
虽然/proc
中的"route"文件是只读的,但Linux的命名空间(namespaces)[2]技术可实现进程级网络隔离。这是2016年左右引入的内核特性,能为进程提供独立的网络栈(类似容器)。
实践演示
示例1:完全网络隔离
1
2
3
4
|
remnux@remnux:~$ sudo unshare --net bash
root@remnux:/home/remnux# ./sample.sh
Am I bad?
curl: (6) Could not resolve host: isc.sans.edu
|
示例2:构建专用IP栈
创建虚拟以太网接口对(10.0.0.1为新命名空间,10.0.0.2为主命名空间):
1
2
3
|
namespace> ip link add veth0 type veth peer name veth1
namespace> ip addr add 10.0.0.1/24 dev veth0
namespace> ip link set veth1 netns 1
|
在主命名空间配置:
1
2
|
root@remnux:/home/remnux# ip addr add 10.0.0.2/24 dev veth1
root@remnux:/home/remnux# ip link set veth1 up
|
添加默认路由后可通过tcpdump捕获流量:
1
2
|
root@remnux:/home/remnux# tcpdump -i veth1 -n
11:02:32.122380 ARP, Request who-has 10.0.0.2 tell 10.0.0.1, length 28
|
最终路由表验证:
1
2
3
4
|
root@remnux:/home/remnux# cat /proc/149522/net/route
Iface Destination Gateway Flags RefCnt Use Metric Mask MTU Window IRTT
veth0 00000000 0200000A 0003 0 0 0 00000000 0 0 0
veth0 0000000A 00000000 0001 0 0 0 00FFFFFF 0 0 0
|
警告:此方案仅实现网络隔离,并非恶意软件分析的完美解决方案
[1] https://docs.kernel.org/filesystems/proc.html
[2] https://en.wikipedia.org/wiki/Linux_namespaces