在多个pcaps上使用pynids

Pha*_*ani 7 python tcp libnids

我试图使用pynids库解析多个pcap文件,但可以只解析第一个文件.我看到nids_unregister_tcplibnids 中有一个函数,会有帮助吗?我在pynids中找不到这个功能.

import nids


def handle_tcp_stream(tcp):
    print "In handle_tcp_stream"


def extract(pcap_file):
    nids.param("tcp_workarounds", 1)
    nids.param("pcap_filter", "tcp")         # bpf restrict to TCP only, note
    nids.param("scan_num_hosts", 0)          # disable portscan detection
    nids.chksum_ctl([('0.0.0.0/0', False)])  # disable checksumming

    nids.param("filename", pcap_file)
    nids.init()
    nids.register_tcp(handle_tcp_stream)

    try:
        nids.run()
    except Exception, e:
        print "Exception ", pcap_file + " ", e


def main():
    extract("a.pcap")
    print "Done"
    extract("a.pcap")


if __name__ == "__main__":
    main()
Run Code Online (Sandbox Code Playgroud)

这是输出:

In handle_tcp_stream
In handle_tcp_stream
In handle_tcp_stream
In handle_tcp_stream
Done
Run Code Online (Sandbox Code Playgroud)

sou*_*kah 4

看来绑定写得不正确。

Perl 版本过去也遇到过这个问题:https://rt.cpan.org/Public/Bug/Display.html ?id=51107

基本上可以概括为:

...run() 完成后,libnids 会清理并删除其回调。

该错误似乎与此处类似https://github.com/MITRECND/pynids/blob/master/nidsmodule.c#L533

我可能是错的,但是在早些时候定义elsean 时,there 使它错过了实际的注册。FPelse机构应该始终被处决。所以一个快速解决方法是:

https://github.com/soulseekah/pynids/commit/8d420e88dbdc340f309db9db7c3b9c2508b1cb80

我对 Python API 有点生疏,但我认为PyObject_Del应该Py_DECREF这样。尽管它也适用于删除。

观看https://github.com/MITRECND/pynids/pull/2了解更多进展,我相信他们会找到更正确的方法来解决这个问题。同时,我所做的暂时应该可以正常工作。

太糟糕了,没有任何单元测试来看看一切是否正常。