Simple sniffer on python based on raw sockets.

This is a very simple example of sniffer
The protocol can be socket.IPPROTO_TCP, socket.IPPROTO_UDP, socket.IPPROTO_ICMP. In my case i choose socket.IPPROTO_TCP
For raw sockets you need root privileges.


sudo python simple_sniffer.py

#!/usr/bin/python
#encoding:utf8
'''
Created on 11.06.2010
Copyright (C) 2010 Alexander S. Razzhivin ( site http://httpbots.com  )
 
#  This program is free software: you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation, either version 3 of the License, or
#  (at your option) any later version.
# 
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
# 
#  You should have received a copy of the GNU General Public License
#  along with this program.  If not, see .
'''
import sys
import socket

FILTER=''.join([(len(repr(chr(x)))==3) and chr(x) or '.' for x in range(256)])

def dump(src, length=8):
    N=0; result=''
    while src:
       s,src = src[:length],src[length:]
       hexa = ' '.join(["%02X"%ord(x) for x in s])
       s = s.translate(FILTER)
       result += "%04X   %-*s   %s\n" % (N, length*3, hexa, s)
       N+=length
    return result

def main():
  num_of_packets = 3
  s = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_TCP)
  for i in range(0, num_of_packets):
    packet = s.recv(16000)
    print "Got a %d byte packet\n" % len(packet)
    print dump(packet)
  
if __name__ == '__main__':
  main()
Login Sign up

Login

Tag cloud

count count all django django admin feedparser libxml2dom linux mysql parser payway python pyunit pyvkapi qiwi return sniffer sockets ssh ssh tunnel sublime text tdd tehtv templatetags ubuntu unittest webmoney X11 аргументы биллинг видео видеоурок видео уроки вконтакте возврат диаграмма диаграмма стека заметки инструкция комментарии композиция логические операторы мышка образование обучение операторы параметры функции парсинг переменная переменные питон платежи поток выполнения преобразование типов программирование проекты процессинг разработка разработкаразработка рекурсия сеть скрипт сниффер сокеты стек стоки тестирование тип тип данных типы урок уроки условия функция
free counters