코딩/Python

ARP를 이용한 host scan

비니화이팅 2020. 2. 24. 16:23
 
from scapy.all import *
import threading
import os

host_list = {}
num = 0
threadLock = threading.Lock()
threads = []

def printHosts(host_list):
    print('-------------------------- HOST LIST --------------------------')
    print('NUM         IP                               MAC')
    print('---------------------------------------------------------------')
    for num in host_list:
        ip, mac = host_list[num]
        print("%d           %s                      %s" % (num, ip, mac))
    print('---------------------------------------------------------------')

def getHosts(ip):
    global host_list, num
    ans, unans = srp(Ether(dst='ff:ff:ff:ff:ff:ff') / ARP(pdst=ip), timeout=5, retry=1)
    for s, r in ans:
        threadLock.acquire()
        num = num + 1
        host_list.update()
        threadLock.release()

def main():
    for i in range(1, 256):
            ip = "192.168.0.%d" % i
            th = threading.Thread(target=getHosts, args=(ip, ))
            th.start()
            threads.append(th)

    for t in threads:
        t.join()

    os.system('clear')
    printHosts(host_list)

if __name__ == '__main__':
    main()
​

 

 

 

왜 인지는 모르겠지만 가끔가다 여러 개의 호스트에서 응답이 안돌아오는 경우가 있다.ㅠ

(와이어 샤크로 잡아봐도 마찬가지 ㅠㅠ)