I want to scrap the data from my router for some home automation, but im facing some trouble I cannot solve/crack.
I have managed to successfully login into the router, but when accessing the data with python script (opening links in router web interface) I recieve an error saying: You have no authority to access this router!
If I manually copy and paste url that python script is accessing into browser (with cookies set), the response is the same. But if i click the buttons inside router web interface i get no "authority" complains. Any ideas how to fix this?
here is the script:
import re
import mechanize
import cookielib
br = mechanize.Browser()
cookies = cookielib.LWPCookieJar()
br.set_cookiejar(cookies)
#they "encrypt" the username and password and store it into the cookie. I stole this value from javascript in runtime.
br.addheaders = [('Cookie','Authorization=Basic YWRtaW46MjEyMzJmMjk3YTU3YTVhNzQzODk0YTBlNGE4MDFmYzM=;')]
#open connection to the router address
br.open('http://192.168.1.1/')
#the only form is "login" form (which we dont have to fill up, because we already have the cookie)
br.select_form(nr=0)
br.form.enctype = "application/x-www-form-urlencoded"
br.submit()
#then the router returns redirect script, so we have to parse it (get the url).
redirect_url = re.search('(http:\/\/[^"]+)',br.response().read()).group(1)
token = re.search("1\/([A-Z]+)\/",redirect_url).group(1) #url always has a random token inside (some kind of security?)
#So with this url I should be able to navigate to page containing list of DHCP clients
br.open("http://192.168.1.1/"+token+"/userRpm/AssignedIpAddrListRpm.htm")
print(br.response().read()) #But response contains html saying "You have no authority to access this router!".
I have solved the issue by adding this:
br.addheaders.append(
('Referer', "http://192.168.1.1/userRpm/LoginRpm.htm?Save=Save")
)
Reason:
Searching the message on web I navigated to a forum where users with firefox version (old) complained about the same warning. The fix was to enable the referrer to be sent, so I did the same in the script and it worked out.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With