网络攻防之——Python实现ARP扫描

代码如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#!/usr/bin/env python
from scapy.all import *
import optparse
from threading import *

def sweep(packet):
try:
reply = srp1(packet,timeout = 1,verbose = 0,iface='eth0')
print 'IP:' + reply.psrc + ' MAC:' + reply.hwsrc

except:
pass


def main():

parser = optparse.OptionParser('usage%prog'+'-H <target host segment/eg:(192.168.1.)>')
parser.add_option('-H',dest='tgtHost',type='string',help='specify target host segment/eg:(192.168.1.)')
(options,args) = parser.parse_args()
host = options.tgtHost
if host == None:
print parser.usage
exit(0)

eth = Ether()
eth.dst = 'ff:ff:ff:ff:ff:ff'
eth.type = 0x0806
arp = ARP()

for n in range(1,254):
arp.pdst = host + str(n)
packet = eth/arp
t = Thread(target = sweep,args = (packet))
t.start()


if __name__ == '__main__':
main()