abekkine/NAViewer — pull request #1 by ATaday (2014-06-12 14:33:21+00:00)
Title: text.py

import socket, sys
from flask import request
from array import *
from struct import *
from flask import Flask
app = Flask(__name__)

@app.route('/')

def hello_world():

s = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_TCP)

packet = s.recvfrom(65536)

packet = packet[0]

ip_header = packet[0:20]

iph = unpack('!2B3H2BH4s4s' , ip_header)

t_length = iph[2]
protocol = iph[6]

s_addr = socket.inet_ntoa(iph[8]);
d_addr = socket.inet_ntoa(iph[9]);

protocol_s = protocol

if protocol == 1:
protocol_s = 'ICMP'
if protocol == 6:
protocol_s = 'TCP'
if protocol == 17:
protocol_s = 'UDP'

tcp_header = packet[20:40]

tcph = unpack('!HHLLBBHHH' , tcp_header)

dest_port = tcph[1]

test = 'Protocol : ' + protocol_s + ' | Source Address : ' + str(s_addr) + ' | Destination Address : ' + str(d_addr) + ' | Dest Port : ' + str(dest_port) + ' | Packet Length : ' + str(t_length)

return test

if __name__ == '__main__':
app.run(debug=True)