How can my Python code obtain a value located inside a text/javascript <script> from a website?

Viewed 63

I'm trying to get some value between brackets within text/javascript source. I've documented myself on how to do so, but couldn't really find a solution using Regex, although I think its the easiest way out. Here's the source:

$(document).ajaxSend(function (event, jqxhr, settings) {
    if (settings.type == "POST") {
        jqxhr.setRequestHeader("ReqVerToken", "jqJ8-Xt4Bw88FWgzXk3HAykj79URgq8NgOSDrCXVZvoANhsiFZ9vSKqf8tC6iNxYEyh9y8GEl1Rsi4flYt2VEDWaixv0G16-GZcPqG_u8Yg1:ATFdoJkWCB8hcQtGRZGP498E5OksSmSbc96WlKnX7rmxhKQDkrkpVEMd1lLos5wOC8IeWZ13hEhYltp3kNRdie3mrh11upWaO7104A3Hojk1");
    }
});

Basically, I want to reach to ReVerToken's value, so that later I can log into interested website. The website is using some sort of CSRF token, which in this case, is the ReVerToken.

1 Answers

Assuming setRequestHeader is always used and the "-char is not allowed in the token:

import re
match = re.search(r'setRequestHeader\(\s*"(\w+)"\s*,\s*"([^"]+)', js_string)
name = match[1] # ReqVerToken
token = match[2] # jqJ8-Xt4Bw88FWgzXk3HAykj79URgq8NgO.....
Related