[【通过】] 内网存活主机探测python简易脚本

[复制链接]
ev1ld1rge 发表于 2017-12-12 20:46:40 | 显示全部楼层 |阅读模式

正式成员|主题 |帖子 |积分 6


萌新写了个python脚本,主要实现了内网存活主机探测,后续会考虑多线程实现。
本菜鸟爱好渗透测试,代码审计以及脚本编写。
希望有机会能与90的各位大佬多多交流

运行示例:
1.png
python源码:
#!/usr/bin/envpython
#-*-coding: utf8 -*-
#Author: HandP
# Date:20171025
importos
importsys
importsocket
IPNumToString= lambda x: '.'.join([str(x/(256**i)%256) for i in range(3,-1,-1)])
IPStringToNum= lambda x:sum([256**j*int(i) for j,i in enumerate(x.split('.')[::-1])])
StartIP= 0
EndIP =0
defhelpmessage():
    message = """
   ======================================================
    =                                                   =
    =                    scanhosts V1.0                   =
    = Usage:                                            =
    =   python scanhosts.py 1.2.3.4                      =
    =   python scanhosts.py 1.2.3.4-255                  =
    =   python scanhosts.py 1.2.3.4 - 1.2.4.5            =
    =   python scanhosts.py f   targetfile
   ======================================================
            """
    print message
defsettargets():
    try:
        commandargs = sys.argv[1:]
        if not commandargs:
            return False
        commandargs = ''.join(commandargs)
        commandargs = commandargs.split('-')
        global StartIP
        global EndIP
        commandlen = len(commandargs)
        if commandlen == 1:
            StartIP = EndIP =int(IPStringToNum(commandargs[0]))
        elif commandlen == 2:
            StartIP = commandargs[0]
            EndIP = commandargs[1]
            if len(StartIP.split('.')) !=4 :
                return False
            endiplen =  len(EndIP.split('.'))
            if endiplen == 1:
                prefixip =StartIP.split('.')[0:3]
                prefixip.append(EndIP)
                EndIP = '.'.join(prefixip)
            elif endiplen == 4:
                pass
            else:
                return False
            StartIP =int(IPStringToNum(StartIP))
            EndIP   = int(IPStringToNum(EndIP))
    except Exception,e :
        print e
        return False
    return True
defcheckhoston(ip):
    try:
                   if "TTL"  in os.popen("ping -n 1%s"%IPNumToString(ip)).read():
                            return True
    except Exception as e:
        print e
defprocesscheckhost():
    global StartIP
    global EndIP
    alivecount = 0
    StartIP = int(StartIP)
    EndIP = int(EndIP)
    totalip = EndIP - StartIP + 1
    if totalip <= 0:
        helpmessage()
        exit(0)
    print 'Startint scan',IPNumToString(StartIP),' -> ',IPNumToString(EndIP), ',please wait...'
    fd = open('scanhost.txt',"w")
    ip = StartIP
    while True:
        if ip > EndIP:
            break
        if checkhoston(ip):
            fd.write(IPNumToString(ip)+'\n')
            alivecount = alivecount + 1
        ip = ip + 1
        sys.stdout.write('#')
        if (ip-StartIP) % 20 == 0:
            sys.stdout.write('\r\n')
    fd.close()
    return alivecount
defshowresult(shownnum):
    if not os.path.isfile('scanhost.txt'):
        print '[-]'+'scanhost.txt'+'does notexists!'
        exit(0)
    if not os.access('scanhost.txt',os.R_OK):
        print '[-]'+'scanhost.txt'+'accessdenied'
        exit(0)
    fd = open('scanhost.txt',"r")
    for line in fd.readlines(shownnum):
        print line.strip('\n')
    fd.close()
defscanactivehost():
    pass
defmain():
    global StartIP
    global EndIP
    alivehost = 0
    count = 0
    if not sys.argv[1:]:
        helpmessage()
        exit(0)
    if str(sys.argv[1]).lower() == "f":
        filename = sys.argv[2]
        if not os.path.isfile(filename):
            print '[-]'+filename+'does notexists!'
            exit(0)
        if not os.access(filename,os.R_OK):
            print '[-]'+filename+'accessdenied'
            exit(0)
        else:
            f = open(filename,'r')
            saveresult =open('scanhost.txt',"w")
            print "[+] scanprogress startnow , please wait!"
            for line in f.readlines():
                line = line.strip('\n')
                if checkhoston(int(IPStringToNum(line))):
                    saveresult.write(line+'\n')
                    alivehost = alivehost + 1
                count += 1
                sys.stdout.write('#')
                if count % 20 == 0:
                    sys.stdout.write('\r\n')
            saveresult.close()
            f.close()
            print "\r\n [%d] host ison,please see the scanhost.txt, top 300 will be shown below" % alivehost
            showresult(300)
            if alivehost > 300:
                print "More ips please seescanhost.txt"
            exit(0)
    else:
        if not settargets():
            helpmessage()
            exit(0)
        alivecount = processcheckhost()
        print "\r\n [%d] host is on,pleasesee the scanhost.txt, top 300 will be shown below" % alivecount
        showresult(300)
        if alivecount > 300:
            print "More ips please seescanhost.txt"
if__name__ == "__main__":

    main()

附件里有py脚本。
告辞



scanhosts.py

4.91 KB, 下载次数: 614

存活主机扫描脚本

评分

参与人数 1酒票 +5 收起 理由
管理05 + 5 欢迎加入90!

查看全部评分

hundan 发表于 2017-12-12 22:05:12 | 显示全部楼层

正式成员|主题 |帖子 |积分 364

本帖最后由 hundan 于 2017-12-12 22:14 编辑

试试for循环?
for /l %i in (0,1,254) do @cmd /c for /l %n in (1,1,254) do @cmd /c "title 192.168.%i.%n && ping 192.168.%i.%n -n 1 -w 10 "|find "TTL="
小莫 发表于 2017-12-13 16:52:29 | 显示全部楼层

正式成员|主题 |帖子 |积分 77

RE: 内网存活主机探测python简易脚本

感谢。你这个脚本要扫描内网的所有ip的话,要配合这个内网ip段大全。
例如:10.0.0.1 - 10.0.0.255
10.0.1.1 - 10.0.1.255
10.0.2.1 - 10.0.2.255
10.0.3.1 - 10.0.3.255
10.0.4.1 - 10.0.4.255
10.0.5.1 - 10.0.5.255
10.0.6.1 - 10.0.6.255
10.0.7.1 - 10.0.7.255
10.0.8.1 - 10.0.8.255
10.0.9.1 - 10.0.9.255
10.0.10.1 - 10.0.10.255
10.0.11.1 - 10.0.11.255
10.0.12.1 - 10.0.12.255



ips.txt

1.96 MB, 下载次数: 690

小莫 发表于 2017-12-13 16:53:42 | 显示全部楼层

正式成员|主题 |帖子 |积分 77

RE: 内网存活主机探测python简易脚本


兄得,来个linux的啊。
ttimasdf 发表于 2017-12-13 19:33:47 | 显示全部楼层

正式成员|主题 |帖子 |积分 107

这个lambda写的很骚。。不过我记得python标准库里有个ipaddress来着,,还可以更骚(′・_・`)
ttimasdf 发表于 2017-12-13 19:54:35 | 显示全部楼层

正式成员|主题 |帖子 |积分 107

RE: 内网存活主机探测python简易脚本

小莫 发表于 2017-12-13 16:53
兄得,来个linux的啊。

linux 更简单了

for ((i=1;i<256;++i)); do ping -qc4 10.0.0.$i ; done

快速回复 返回顶部 返回列表