检测web服务器指定位置大文件是否存在

在bugscan群里看到有人问有一个大文件,想探测其是否存在。如果使用curl的话,会将整个文件下载到节点,对于扫描没有任何用处,反而浪费了扫描时间。

 

于是我想到的解决办法是不使用curl,直接用底层socket,发包之后接收http head部分,然后拿到返回码之后就断开链接。不知道这样做有没有什么弊端,如果有的话,请指出。

 

下面直接贴上源码:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
#__author__ = 'Medici.Yan'
#
'''测试文件是wps官网上的提供的下载安装包'''
import socketdef assign(service, arg):
    if service == "ip":
        return True, arg
def audit(arg):
    doGet(arg,'/wdl1.cache.wps.cn/wps/download/W.P.S.4954.19.552.exe')

def doGet(host,path):
    payload='''GET %s HTTP/1.1\r\nHost: %s\r\nConnection: keep-alive\r\nAccept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8\r\nUser-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36\r\nAccept-Encoding: gzip, deflate, sdch\r\nAccept-Language: zh-CN,zh;q=0.8,en;q=0.6\r\n\r\n''' % (path,host)
    print payload
    s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
    try:
        socket.setdefaulttimeout(20)#超时
        s.connect((host,80))#连接对应主机和端口
        s.send(payload)
        data=s.recv(len(payload))
        httphead=data.split('\r\n')
        if '200 OK' in httphead[0]:
            print 'exist'
        else:
            print 'error or not exist'
    except Exception :
        pass
    finally:
        s.close()

if __name__ == '__main__':
    from dummy import *
    audit(assign('ip', '222.178.202.37')[1])

测试结果:

posted @ 2015-03-14 15:10  Medici.Yan  阅读(686)  评论(0编辑  收藏  举报