Python errors


#1

hi guys i started to study python…
i was studyng “how to build a simple network sniffer” but i have a lot of problem:
i created 2 script(really easy),but in both of these i have everythime an error.
plis help me.

first code:

import socket
import os
import struct
from ctypes import *

host = “192.168.1.37”

class IP(Structure):
fields = [
(“ihl”, c_ubyte, 4),
(“version”, c_ubyte, 4),
(“tos”, c_ubyte),
(“len”, c_ushort),
(“id”, c_ushort),
(“offset”, c_ushort),
(“ttl”, c_ubyte),
(“protocol_num”, c_ubyte),
(“sum”, c_ushort),
(“src”, c_ulong),
(“dst”, c_ulong),
]

def new(self, socket_buffer=None):
return self.from_buffer_copy(socket_buffer)

def init(self, socket_buffer=None):

self.protocol_map = {1:"ICMP", 6:"TCP", 17:"UDP"}

self.src_address = socket.inet_ntoa(
    struct.pack("<L",self.src))
self.dst_address = socket.inet_ntoa(
    struct.pack("<L",self.dst))

if os.name == “nt”:
socket_protocol = socket.IPPROTO_IP
else:
socket_protocol = socket.IPPROTO_ICMP

sniffer = socket.socket(
socket.AF_INET,
socket.SOCK_RAW,
socket_protocol)

sniffer.bind((host, 0))

sniffer.setsockopt(socket.IPPROTO_IP, socket.IP_HDRINCL, 1)

if os.name == “nt”:
sniffer.ioctl(socket.SIO_RCVALL, socket.RCVALL_ON)
try:
while true:

    raw_buffer = sniffer.recvfrom(65565)[0]

    ip_header = IP(raw_buffer[0:20])

    print ("Protocol: %s %s -> %s") % (ip_header.protocol, \
           ip_header.src_adress, ip_header.dst_adress)

except KeyboardInterrupt:
if os.name == “nt”:
sniffer.ioctl(socket.SIO_RCVALL, socket.RCVALL_OFF)

when i execute on my android i have this error:
errno 99 invalid adress…(i changed the host adress a lot of times but nothing)

second code:(really easy with scapy)
from scapy.all import *

pkts = sniff(iface=“eth0”, count=10)
but when i try to execute this code i have this error:
unknow network interface “eth0”(or wlan0 ecc…)
hope you can help me
x


#3

Try whichever address this gives you on that device

import socket
print(socket.gethostbyname(socket.gethostname()))

You might also already know which address your device has on the network you want your socket to be available from

127.0.0.1 would also let that line execute (whether that does what you want is another matter)

…oh and you might flat out not have permission to do it at all (binding a raw socket) unless you’re a superuser


#4

i will try thanks…
i know the devices and i try more than one time but nothing.
i am superuser (in my phone i have kali chroot bash and root!)


#5

update:
if i execute : print(socket.gethostbyname(socket.gethostname()))
in my android shell the output is 127.0.0.1 and im not offline.
do u know any command for see every computer/devices in my network?


#6

Your router would know, that’s what assigned the ip right. Otherwise you could ping everyone in that range, but they’d have to respond for you to see if there’s something there. There is also nmap which can try multiple common ports (as opposed to just sending ping)

you say you have kali installed, then you are probably able to run:

$ ip a

for me that shows:

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000              
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: enp2s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000    
    link/ether 6c:62:6d:97:10:d2 brd ff:ff:ff:ff:ff:ff
    inet 192.168.0.100/24 brd 192.168.0.255 scope global noprefixroute enp2s0
       valid_lft forever preferred_lft forever
    inet6 fe80::db37:5098:8d5a:87b0/64 scope link noprefixroute
       valid_lft forever preferred_lft forever
3: wlp4s0: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN group default qlen 1000                  
    link/ether 86:04:8e:71:d7:1d brd ff:ff:ff:ff:ff:ff

The second interface (enp2s0) is plugged into my router, and that interface’s ip is 192.168.0.100 which is what I would put as host in your code if I ran it on this machine

I’d try asking in #kali-linux on freenode, they know so very much more


#7

hi and thanks.
in my command shell when i do ip a i haven’t enp2so…
how can i plugged it?


#8

On an android phone you probably wouldn’t… It’s an ethernet port
Also, the exact name varies, and on slightly older systems it’d be called eth0
You might have an interface whose name starts with wlp, that would be more reasonable.

If all that shows up is lo, then I don’t know how you’re “supposed” to find it. I suggested looking at your router’s interface. You could also start a server on a device whose ip you do know, and send a request - if that server prints out the ip of each connection then you’d see what it was

Again. #kali-linux would know better


#9

sorry sir im a newbie…
im studing infiormatic since last year