Created
June 25, 2020 00:42
-
-
Save pauldzy/fb03e2826dacaaaa7895598dbf39abd7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests; | |
url = "https://echo.epa.gov/files/echodownloads/npdes_biosolids_downloads.zip"; | |
target = r"npdes_biosolids_downloads.zip"; | |
print("\nQuerying " + str(url) + "...\n"); | |
step1 = requests.get( | |
url | |
,allow_redirects=False | |
); | |
if step1.status_code == 302: | |
print("Encountered initial webgate redirect...\n"); | |
location = step1.headers['Location']; | |
print("Redirecting to " + str(location) + "\n"); | |
cookie1 = None; | |
cookie2 = None; | |
for k,v in step1.cookies.iteritems(): | |
if k == 'OAMAuthnHintCookie': | |
print("Found cookie1 " + str(k) + ':' + str(v) + "\n"); | |
cookie1 = k; | |
elif k[0:31] == 'OAMRequestContext_echo.epa.gov:': | |
print("Found cookie2 " + str(k) + ':' + str(v) + "\n"); | |
cookie2 = k; | |
if cookie1 is None: | |
raise Exception('Unable to obtain OAMAuthnHintCookie cookie'); | |
if cookie2 is None: | |
raise Exception('Unable to obtain OAMRequestContext cookie'); | |
step2 = requests.get( | |
location | |
,cookies=step1.cookies | |
,allow_redirects=False | |
); | |
if step2.status_code != 302: | |
raise Exception('No redirection returned from second step.'); | |
location = step2.headers['Location']; | |
print("Redirecting to " + str(location) + "\n"); | |
cookie3 = None; | |
for k,v in step2.cookies.iteritems(): | |
if k == 'OAM_ID': | |
print("Found cookie3 " + str(k) + ':' + str(v) + "\n"); | |
cookie3 = k; | |
print("Redirecting back to original request at " + str(url) + "\n"); | |
resp = requests.get( | |
url | |
,cookies=requests.cookies.merge_cookies(step1.cookies,step2.cookies) | |
,allow_redirects=False | |
); | |
if resp.status_code != 200: | |
raise Exception('Webgate authentication failed with code ' + str(response.status) + '.'); | |
elif step1.status_code == 200: | |
print("Webgate already authenticated.\n"); | |
resp = step1; | |
else: | |
raise Exception('error in download, code ' + str(step1.status_code)); | |
print("Writing payload to file...\n"); | |
open(target,'wb').write(resp.content); | |
print("Download complete.\n"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment