Http request spoofing in python

From UVOO Tech Wiki
Jump to navigation Jump to search

https://stackoverflow.com/questions/22609385/python-requests-library-define-specific-dns

from urllib3.util import connection
import urllib3

hostname = "MYHOST"
host_ip = "10.10.10.10"
_orig_create_connection = connection.create_connection

def patched_create_connection(address, *args, **kwargs):
    overrides = {
        hostname: host_ip
    }
    host, port = address
    if host in overrides:
        return _orig_create_connection((overrides[host], port), *args, **kwargs)
    else:
        return _orig_create_connection((host, port), *args, **kwargs)

connection.create_connection = patched_create_connection  

conn = urllib3.connection_from_url('https://MYHOST', ca_certs='ca_crt.pem', key_file='pr.pem', cert_file='crt.pem', cert_reqs='REQUIRED')
response = conn.request('GET', 'https://MYHOST/OBJ', headers={"HOST": "MYHOST"})
print(response.data)