python - 在 Python2.6+ 中为 Solaris 创建 netstat 的实现

标签 python c python-2.7 solaris computer-forensics

我正在用 Python 编写一个收集脚本,以使用 Python 2 脚本从各种 *nix 主机中获取取证工件,但是我在为 Solaris 编写 netstat 收集时遇到了一些问题。

我要如何实现这一点有一些限制。我不能在计算机上使用 ELF 二进制文件(Python 除外)(因为当 *nix 机器受到攻击时它们经常被操纵),我不能导入外部库(因为这不会在我管理的计算机上运行),并且必须编写出于向后兼容的原因,在 Python 2 中。

综上所述,我正在使用 Illumos(一个开源 Solaris 实现)的 netstat.c 文档作为我的基础,并且基本上试图对其进行逆向工程并在 Python 2 中重写它。

https://searchcode.com/codesearch/raw/52916131/

到目前为止我发现的是,脚本打开“/dev/arp”和“/dev/kstat”以确保有返回值。

import os
sd = os.open('/dev/arp', os.O_RDWR)
kc = os.open('/dev/kstat', os.O_RDWR)

完成此操作后,“sd”值用于通过 mibget() 方法收集网络统计信息。

问题就在这里。为了收集 netstat 信息,Solaris 正在使用系统调用 getmsg 查询 sd,但它使用的是我不熟悉的数据结构。

getcode = getmsg(sd, &ctlbuf, (struct strbuf *)0, &flags);

我不知道如何在 Python 2 中重建它,你们中的任何人都知道我可以从这里去哪里吗?我的解释有误吗?

我了解 netstat 的 psutil 实现可以在 Solaris 上运行,因此必须有一种方法来实现它。

谢谢大家的帮助。

编辑:

这似乎是 truss netstat 的相关部分:

open("/etc/default/inet_type", O_RDONLY)        Err#2 ENOENT
open("/dev/arp", O_RDWR)                        = 3
ioctl(3, I_PUSH, "tcp")                         = 0
ioctl(3, I_PUSH, "udp")                         = 0
ioctl(3, I_PUSH, "icmp")                        = 0
putmsg(3, 0x08047E3C, 0x00000000, 0)            = 0
getmsg(3, 0x08047E3C, 0x00000000, 0x08047E5C)   = 2
getmsg(3, 0x00000000, 0x08047E48, 0x08047E5C)   = 0
getmsg(3, 0x08047E3C, 0x00000000, 0x08047E5C)   = 2
getmsg(3, 0x00000000, 0x08047E48, 0x08047E5C)   = 0
getmsg(3, 0x08047E3C, 0x00000000, 0x08047E5C)   = 2
getmsg(3, 0x00000000, 0x08047E48, 0x08047E5C)   = 0
getmsg(3, 0x08047E3C, 0x00000000, 0x08047E5C)   = 2
getmsg(3, 0x00000000, 0x08047E48, 0x08047E5C)   = 0
getmsg(3, 0x08047E3C, 0x00000000, 0x08047E5C)   = 2
getmsg(3, 0x00000000, 0x08047E48, 0x08047E5C)   = 0
getmsg(3, 0x08047E3C, 0x00000000, 0x08047E5C)   = 2
brk(0x080736E0)                                 = 0

最佳答案

getmsg()struct strbufspecified by POSIX, albeit marked as obsolescent in POSIX 7 :

NAME

getmsg, getpmsg - receive next message from a STREAMS file (STREAMS)

SYNOPSIS

[OB XSR] [Option Start] #include <stropts.h>

int getmsg(int fildes, struct strbuf *restrict ctlptr,
       struct strbuf *restrict dataptr, int *restrict flagsp);
int getpmsg(int fildes, struct strbuf *restrict ctlptr,
       struct strbuf *restrict dataptr, int *restrict bandp,
       int *restrict flagsp); [Option End]

DESCRIPTION

The getmsg() function shall retrieve the contents of a message located at the head of the STREAM head read queue associated with a STREAMS file and place the contents into one or more buffers. The message contains either a data part, a control part, or both. The data and control parts of the message shall be placed into separate buffers, as described below. The semantics of each part are defined by the originator of the message.

...

我不知道 Solaris 上的 STREAMS 有任何 Python 绑定(bind)。

STREAMS functionality从未在 Linux 上实现。

关于python - 在 Python2.6+ 中为 Solaris 创建 netstat 的实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58627421/

相关文章:

python - 在Python中迭代双端队列

python - PyQt4:如何/何时从 QTabWidget 的子类发出自定义信号?

c - 该程序中创建了多少个进程?

在 macOS Mojave 中使用 gcc 进行编译

python - 如何访问用于 python 中的 urllib 模块的字典值?

python - 使用类的实例

python - “CsrfViewMiddleware”对象不可迭代

python - Scipy 标签侵 eclipse

c++ - memcpy 从 char 数组到结构体

Django 存储亚马逊 S3,给出 400 个错误的请求异常