コード例 #1
0
def main():
    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)

    while True:
        raw_buffer = sniffer.recvfrom(65565)[0]
        ip_header = raw_buffer[0:20]
        iph = struct.unpack('!BBHHHBBH4s4s', ip_header)

        # Create our IP structure
        version_ihl = iph[0]
        version = version_ihl >> 4
        ihl = version_ihl & 0xF
        iph_length = ihl * 4
        ttl = iph[5]
        protocol = iph[6]
        s_addr = socket.inet_ntoa(iph[8])
        d_addr = socket.inet_ntoa(iph[9])
        print('IP -> Version:' + str(version) + ', Header Length:' + str(ihl) + \
        ', TTL:' + str(ttl) + ', Protocol:' + str(protocol) + ', Source:'\
         + str(s_addr) + ', Destination:' + str(d_addr))

        # Create our ICMP structure
        buf = raw_buffer[iph_length:iph_length + ctypes.sizeof(ICMP)]
        icmp_header = ICMP(buf)
        print('ICMP -> Type:{0}, Code:{1}'.format(
            (icmp_header.type, icmp_header.code)))
コード例 #2
0
def main():

    t = threading.Thread(target=udp_sender, args=(SUBNET, MESSAGE))
    t.start()
    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)

    # continually read in packets and parse their information
    while True:
        raw_buffer = sniffer.recvfrom(65565)[0]
        ip_header = raw_buffer[0:20]
        iph = struct.unpack('!BBHHHBBH4s4s' , ip_header)

        # Create our IP structure
        version_ihl = iph[0]
        ihl = version_ihl & 0xF
        iph_length = ihl * 4
        src_addr = socket.inet_ntoa(iph[8]);

        # Create our ICMP structure
        buf = raw_buffer[iph_length:iph_length + ctypes.sizeof(ICMP)]
        icmp_header = ICMP(buf)
        
        # check for the type 3 and code and within our target subnet
        if icmp_header.code == 3 and icmp_header.type == 3:
            if IPAddress(src_addr) in IPNetwork(SUBNET):
                if raw_buffer[len(raw_buffer) - len(MESSAGE):] == MESSAGE:
                    print(f'Host up: {src_addr}')
コード例 #3
0
ファイル: scan.py プロジェクト: githubmaidou/python
def main():
    t = threading.Thread(target=udp_sender, args=(SUBNET, MESSAGE))
    t.start()

    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)

    while 1:
        raw_buffer = sniffer.recvfrom(65565)[0]
        ip_header = raw_buffer[0:20]
        iph = struct.unpack('!BBHHHBBH4s4s', ip_header)

        version_ihl = iph[0]
        ihl = version_ihl & 0xF
        iph_length = ihl * 4
        src_addr = socket.inet_ntoa(iph[8])

        buf = raw_buffer[iph_length:iph_length + ctypes.sizeof(ICMP)]
        icmp_header = ICMP(buf)

        if icmp_header.code == 3 and icmp_header.type == 3:
            if IPAddress(src_addr) in IPNetwork(SUBNET):
                if raw_buffer[len(raw_buffer) - len(MESSAGE):] == MESSAGE:
                    print("host up: %s" % src_addr)
コード例 #4
0
def main():
    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)

    # continually read in packets and parse their information
    while 1:
        # read in a packet and pass the first 20 bytes to initialize the IP structure
        raw_buffer = sniffer.recvfrom(65565)[0]

        #take first 20 characters for the ip header
        ip_header = raw_buffer[0:20]

        #unpack them
        iph = struct.unpack('!BBHHHBBH4s4s', ip_header)

        # print
        version_ihl = iph[0]
        ihl = version_ihl & 0xF
        iph_length = ihl * 4
        src_addr = socket.inet_ntoa(iph[8])

        # create our ICMP structure
        buf = raw_buffer[iph_length:iph_length + ctypes.sizeof(ICMP)]
        icmp_header = ICMP(buf)

        # check for the type 3 and code: first check to make sure that the ICMP
        # response is coming from within our target subenet
        if icmp_header.code == 3 and icmp_header.type == 3:
            # make sure host is in our target subnet
            if IPAddress(src_addr) in IPNetwork(SUBNET):
                # make sure it has magic message
                if raw_buffer[len(raw_buffer) - len(MESSAGE):] == MESSAGE:
                    print("Host up: %s" % src_addr)
コード例 #5
0
def main():
    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)

    # continually read in packets and parse their information
    while 1:
        # read in a packet and pass the first 20 bytes to initialize the IP structure
        raw_buffer = sniffer.recvfrom(65565)[0]

        #take first 20 characters for the ip header
        ip_header = raw_buffer[0:20]

        #unpack them
        iph = struct.unpack('!BBHHHBBH4s4s', ip_header)

        # print
        version_ihl = iph[0]
        version = version_ihl >> 4
        ihl = version_ihl & 0xF
        iph_length = ihl * 4
        ttl = iph[5]
        protocol = iph[6]
        s_addr = socket.inet_ntoa(iph[8])
        d_addr = socket.inet_ntoa(iph[9])

        print 'IP -> Version:' + str(version) + ', Header Length:' + str(ihl) + \
        ', TTL:' + str(ttl) + ', Protocol:' + str(protocol) + ', Source:'\
         + str(s_addr) + ', Destination:' + str(d_addr)

        # create our ICMP structure
        buf = raw_buffer[iph_length:iph_length + ctypes.sizeof(ICMP)]
        icmp_header = ICMP(buf)

        print "ICMP -> Type:%d, Code:%d" % (icmp_header.type,
                                            icmp_header.code) + '\n'
コード例 #6
0
def main():
    # type: () -> object
    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)

    myIP = get('https://api.ipify.org').text

    print myIP
    myCoord = coord_fetch.ipinfo(myIP)
    print myCoord

    # continually read in packets and parse their information
    while 1:
        # read in a packet and pass the first 20 bytes to initialize the IP structure
        raw_buffer = sniffer.recvfrom(55056)[0]  #original: 65565

        # take first 20 characters for the ip header
        ip_header = raw_buffer[0:20]

        # unpack them
        iph = struct.unpack('!BBHHHBBH4s4s', ip_header)

        # print
        version_ihl = iph[0]
        version = version_ihl >> 4
        ihl = version_ihl & 0xF
        iph_length = ihl * 4
        ttl = iph[5]
        protocol = iph[6]
        s_addr = socket.inet_ntoa(iph[8])
        d_addr = socket.inet_ntoa(iph[9])
        ip = str(s_addr)

        # print 'IP -> Version:' + str(version) + ', Header Length:' + str(ihl) + \
        #       ', TTL:' + str(ttl) + ', Protocol:' + str(protocol) + ', Source:' \
        #       + str(s_addr) + ', Destination:' + str(d_addr)

        # create our ICMP structure
        buf = raw_buffer[iph_length:iph_length + ctypes.sizeof(ICMP)]
        icmp_header = ICMP(buf)

        #print "ICMP -> Type:%d, Code:%d" % (icmp_header.type, icmp_header.code) + '\n'

        #return str(s_addr)
        thisCoord = coord_fetch.ipinfo(ip)
        print thisCoord

        if 'undefined' not in thisCoord:
            if thisCoord not in coordarr:
                coordarr.append(thisCoord)
                #app.showMap(coordarr)
                myDistance = great_circle(myCoord, thisCoord).miles
                print 'Distance: %s' % myDistance

                if myDistance > 25:
                    malarr.append(thisCoord)
                    print "malarr: %s" % malarr
                    print "coordarr: %s" % coordarr
                    #app.showmap(coordarr, malarr)
                print coordarr

        if len(coordarr) > 5:
            return coordarr, malarr
コード例 #7
0
def parse_icmp(raw_buffer, iph_length):
    buf = raw_buffer[iph_length:iph_length + ctypes.sizeof(ICMP)]
    icmp_header = ICMP(buf)

    logging.debug(('ICMP -> Type:%d, Code: %d, CheckSum: %d' %
                   (icmp_header.type, icmp_header.code, icmp_header.checksum)))