Skip to content

Instantly share code, notes, and snippets.

@n3tsurge
Created October 26, 2022 13:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save n3tsurge/307bc8fa4c3d43ede2651318e6249761 to your computer and use it in GitHub Desktop.
Save n3tsurge/307bc8fa4c3d43ede2651318e6249761 to your computer and use it in GitHub Desktop.
Downloads Microsoft IPs for specific URLs to a file
from datetime import datetime
from requests import Session
def fetch_microsoft_ips(url: str) -> dict:
'''
Pulls down the Microsoft IP JSON list
'''
s = Session()
result = s.get(url)
if result.status_code == 200:
return result.json()
else:
raise Exception('Unable to fetch IP information from Microsoft')
def get_ips_by_url(data: list, urls: list) -> list:
'''
Retreives all the IPS from a service area based on the URLs in the service
area
'''
ips = []
# Type checking
if not isinstance(data, list):
raise ValueError('{data} is not a Dict')
if not isinstance(urls, list):
raise ValueError("URLs {urls} is not a List")
else:
for url in urls:
if not isinstance(url, str):
raise ValueError("Service Area {sa} is not a String")
for entry in data:
if 'urls' in entry and url in entry['urls']:
if 'ips' in entry and isinstance(entry['ips'], list):
ips += entry['ips']
return ips
outfile = "/opt/threat-lists/microsoft-exchange-ips.txt"
urls = ['smtp.office365.com','*.mail.protection.outlook.com']
data = fetch_microsoft_ips('https://endpoints.office.com/endpoints/worldwide?clientrequestid=b10c5ed1-bad1-445f-b386-b919946339a7')
ips = get_ips_by_url(data, urls)
with open(outfile, 'w') as f:
f.write(f"# Last Updated: {datetime.utcnow()}\n#\n")
for ip in ips:
f.write(f"{ip}\n")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment