信息收集之censys

一.摘要

Censys提供了search、view、report、query、export以及data六种API接口。

search接口的请求地址是https://www.censys.io/api/v1/search/?,其中?的地方可以是ipv4、websites或者certificates,分别代表搜索ipv4主机、网站和证书。我们的POST请求应该是一组包含query、page、fields的json数据,其中query指的是相应的搜索语句;page代表返回的页码,Censys总是返回一页的数据;fields指的是你希望返回值中包含哪些字段,具体包含哪些字段你可以自己去看一下。

 

JSON有两种结构:
  对象在js中表示为“{}”括起来的内容,数据结构为 {key:value,key:value,...}的键值对的结构
  数组在js中是中括号“[]”括起来的内容,数据结构为 ["java","javascript","vb",...]

二.利用代码

# -*- coding: UTF-8 -*-
#https://www.censys.io/api/v1/search/ipv4
#post:{"query": "keyword", "page": 1, "fields": ["ip", "protocols", "location.country"]}
#query指的是相应的搜索语句;page代表返回的页码;fields指的是你希望返回值中包含哪些字段
import sys
import json
import requests
import time

API_URL = "https://www.censys.io/api/v1"
UID = "85e64536-7534-4177-8c72-9a383bf01f12"
SECRET = "9hCyul4KXJKXieyXeGIFT0lr04rbN9yQ"
page=1
PAGES=2
    
def getIp(page):
    iplist=[]
    data = {
                "query":"keyword", 
                "page":page, 
                "fields":["ip","protocols","location.country"]
            }        
    try:
        res = requests.post(API_URL + "/search/ipv4", data=json.dumps(data), auth=(UID, SECRET))
    except:
        pass
    try:
        results = res.json()
    except:
        pass
    if res.status_code != 200:
        print "error occurred: %s" % results["error"]
        sys.exit(1)
    #print "Total_count:%s" % (results["metadata"]["count"])
    iplist.append("Total_count:%s" % (results["metadata"]["count"]))
    for result in results["results"]:
        #print "%s in %s" % (result["ip"],result["location.country"][0])
        #iplist.append((result["ip"]+':'+i+' in '+result["location.country"][0]))
        for i in result["protocols"]:
            iplist.append(result["ip"]+':'+i+' in '+result["location.country"][0])    
    return iplist
    
if __name__ == '__main__':
    print "start..."
    with open('censys.txt','a') as f:
        while page <= PAGES:
            iplist=(getIp(page))
            print 'page is:'+str(page)
            page += 1
            time.sleep(1)
            for i in iplist:
                f.write(i+'\n')

    
    

首先request请求,post提交data和auth信息,返回json数据包,利用json数据结构找的所需信息,保存并返回一个列表。
主函数判断page大小,不够就调用getIp函数,最后循环写入结果。

 

posted @ 2016-08-01 13:19  Dleo  阅读(6474)  评论(1编辑  收藏  举报