Self-Improvement

shodan의 Synology NAS의 UPNP 제품명 알아오는 코드 본문

프로그래밍/Python

shodan의 Synology NAS의 UPNP 제품명 알아오는 코드

JoGeun 2019. 12. 4. 11:04
import shodan
import requests
import subprocess
import re
from colorama import Fore
from xml.etree import ElementTree as ET
 
SHODAN_API_KEY = 'GKja8yvDZh5B1gDF6jhz64hTJkPtOCUV' 
count=0
query='synology port:1900'
data=(('command','login'),('username','admin'),('password',''))
def shodan_query():
    try:
        api = shodan.Shodan(SHODAN_API_KEY)
        result = api.search(query, page=1) #You can modify the page
    except shodan.APIError as e:
        print (Fore.RED + e.value + Fore.RESET)
        return False
 
    if len(result['matches']) > 0:
        print ('Found ' + str(result['total']) + " results")
 
    else:
        print ("Nothing was found")
        return False
 
    return result
 
def query_print(result):
lines = []
for service in result['matches']: 
try:
data=service['data']

ip=service['ip_str']
lines = data.split('\n')
for each in lines:
if each.find("LOCATION:") >= 0:
split1=each.split('LOCATION: ')[1]
split2=split1.split(':')[2]
result='http://'+service['ip_str']+':'+split2
result2=result.rstrip()
ssdp_request(ip, result2)
except KeyboardInterrupt:
raise
except:
pass

def ssdp_request(ip,url):
try:
url=url
s=requests.Session()
resp=s.get(url, timeout=2)
if resp.status_code != 200:
print ("not 200")
else:
tree=ET.fromstring(resp.content)
if tree[1][5].text == "DS918+":
print (ip)
print (tree[1][5].text)
count=count+1
print(count)
except KeyboardInterrupt:
raise
except:
pass

if __name__=="__main__":
    results=shodan_query()
    query_print(results)