2012-01-05 37 views
2

我正在用python編寫一個工具(平臺是linux),其中一個任務是捕獲一個實時tcp流並對每行應用一個函數。目前我使用如何在Python中純粹實現tcpflow功能(遵循tcp流)

import subprocess 
proc = subprocess.Popen(['sudo','tcpflow', '-C', '-i', interface, '-p', 'src', 'host', ip],stdout=subprocess.PIPE) 

for line in iter(proc.stdout.readline,''): 
    do_something(line) 

這工作得很好(與在/ etc/sudoers文件的相應條目),但我想避免調用外部程序。

到目前爲止,我已經調查了以下可能性:

  • flowgrep:一個Python的工具,它看起來就像是我需要什麼,但是:它採用pynids 內部,這是7歲和似乎相當被遺棄了。我的gentoo系統沒有pynids軟件包 ,它附帶了libnids 的修補版本,如果不進行進一步調整,將無法編譯。

  • scapy:這是蟒蛇包處理程序/庫, 我不知道,如果TCP流重組 支持。

  • pypcappylibpcap作爲libpcap的包裝。同樣,libpcap用於包 捕獲,在那裏我需要流重組,這是不可能根據 到this question

之前,我更深入地瞭解任何一個庫,我想知道是否也許有人 有一個工作代碼段(這似乎是一個相當普遍的問題)。我也很感激,如果 有人可以提供正確的方式去建議。

感謝

回答

1

喬恩Oberheide帶頭努力保持pynids,這是相當最新的: http://jon.oberheide.org/pynids/

所以,這可能允許您進一步探索flowgrep。 Pynids本身處理流重建相當優雅。參考http://monkey.org/~jose/presentations/pysniff04.d/一些很好的例子。

+0

感謝您的鏈接。我完全錯過了這個更新的pynids,我可以編譯這個版本。但首先測試表明,使用libnids只能捕獲新建立的tcp連接,因爲回調函數的工作方式(我需要捕獲正在進行的流)。那麼這不在這個問題的範圍之內...... – PiQuer 2012-01-06 16:27:53

1

正如後續:我放棄了監控tcp層上的流的想法。相反,我在python中編寫了一個代理,並讓我想監視的連接(一個http會話)通過此代理連接。結果更穩定,不需要root權限來運行。該解決方案取決於pymiproxy

這進入獨立程序,例如, helper_proxy.py

from multiprocessing.connection import Listener 
import StringIO 
from httplib import HTTPResponse 
import threading 
import time 
from miproxy.proxy import RequestInterceptorPlugin, ResponseInterceptorPlugin, AsyncMitmProxy 

class FakeSocket(StringIO.StringIO): 
    def makefile(self, *args, **kw): 
     return self 

class Interceptor(RequestInterceptorPlugin, ResponseInterceptorPlugin): 
    conn = None 
    def do_request(self, data): 
     # do whatever you need to sent data here, I'm only interested in responses 
     return data 
    def do_response(self, data): 
     if Interceptor.conn: # if the listener is connected, send the response to it 
      response = HTTPResponse(FakeSocket(data)) 
      response.begin() 
      Interceptor.conn.send(response.read()) 
     return data 

def main(): 
    proxy = AsyncMitmProxy() 
    proxy.register_interceptor(Interceptor) 
    ProxyThread = threading.Thread(target=proxy.serve_forever) 
    ProxyThread.daemon=True 
    ProxyThread.start() 
    print "Proxy started." 
    address = ('localhost', 6000)  # family is deduced to be 'AF_INET' 
    listener = Listener(address, authkey='some_secret_password') 
    while True: 
     Interceptor.conn = listener.accept() 
     print "Accepted Connection from", listener.last_accepted 
     try: 
      Interceptor.conn.recv() 
     except: time.sleep(1) 
     finally: 
      Interceptor.conn.close() 

if __name__ == '__main__': 
    main() 

python helper_proxy.py開始。這將創建一個代理監聽端口8080上的http連接,並監聽端口6000上的另一個python程序。一旦另一個python程序連接到該端口,代理將發送所有http回覆。這樣,助手代理可以繼續運行,保持http連接,並且可以重新啓動偵聽器進行調試。

以下是聽者的工作原理,例如listener.py

from multiprocessing.connection import Client 

def main(): 
    address = ('localhost', 6000) 
    conn = Client(address, authkey='some_secret_password') 
    while True: 
     print conn.recv() 

if __name__ == '__main__': 
    main() 

這將只打印所有回覆。現在將瀏覽器指向運行在端口8080上的代理,並建立您要監控的http連接。

相關問題