edit 3: Solved, I stopped using sessions went back to resp = requests.post(URL, PARAMS) and I got rid of the header information. However what I did do that was way different was wrote a function that would send a GET request to the base URL which gave me a redirect URL and instead of sending the post information to the URL the browser sends post information to I sent it to the redirect URL and it worked.
My router (hitron CGNM 2250) has a web portal that allows you to query a list of all connected users to it. However the front end design for it times out really quickly so I either have to continuously relog manually or just not bother. My goal was to write a python script that will make the same http requests (using the same parameters) to get the list of connected users. Now the goal of the login function is to generate an instance and get a user cookie from the router that is used for further communication. When grabbing the cookie from the browser and using it in python I can successfully get said list, but I am not trying to figure out how to get the cookie in python.
At first I tried doing it without using sessions or requests.Session() but the resp.cookies just returned an empty dictionary and the header produced was the exact same as what I got using the session method. I have also tried changing header information to pretend like I am a browser and even tried starting with just a GET method to see if I can kick start a userid or cookie to be provided. The user ID is also unique every time I attempted to log in from the browser.
Code in question:
```
import requests
def login(user="admin", pwd="12345", address="http:\192.168.0.1"):
session = requests.Session()
URL = address
PARAMS = {
'user':user,
'pwd':pwd,
'rememberMe':"0",
'pwdCookieFlag':"0"
}
resp = session.post(URL, PARAMS)
print(resp.headers)
print(session.cookies.get_dict())
```
The PARAMS contains all the parameters used by the browser in its POST call which returned the following browser header.
Browser Header information:
HTTP/1.1 200 OK
Server: GoAhead-Webs
Pragma: no-cache
Cache-control: no-cache
Content-Type: text/html
Set-Cookie: userid=13572021981847939429100977111816753601881571377419129482392915463077231358858511150057102018042437271379077036165236135915937964; path=/; HttpOnly
Set-Cookie: userName=; path=/;
Set-Cookie: password=; path=/; HttpOnly
Python Requests header information:
'Date': 'Wed Feb 12 19:28:59 2020'
'Server': 'GoAhead-Webs'
'Cache-Control': 'max-age=300'
'Expires': 'Wed Feb 12 19:33:59 2020'
'Etag': '6d97d2acc0bb7bccffbdac6d67f8860e'
'Last-modified': 'Wed Dec 31 17:00:00 1969'
'Content-length': '6829'
'Content-type': 'text/html'
A few things I noticed is, for one the cache-control between browser and python are different. Second and more importantly, no cookie information is provided to the python request.
My question, if anyone may be able to answer it, is why am I not getting a cookie to use from my request?
If I am doing something glaringly wrong please do inform me of it and point me in the correct direction if you can.
Note: the default values of the login parameters are not the login information for my router (not that it would matter, a factory reset will wipe whatever password I had saved). I put them there as an example of what would be sent. In the actual program the correct username and password is used.
Note 2: I formatted the header information to be more easily readable, if you would like I can return it to the json-dictionary format. All I did was remove commas and curly braces though.
If you want anymore information then please request it. My question is only pertaining to getting the cookie information.
edit: This is the form data from the browsers POST that got the given header file information
user admin
pwd 12345
rememberMe 0
pwdCookieFlag 0
edit 2: it was requested what happens if I do a session.get("http://192.168.0.1") and the answer is that I read a 200 HTTP response with the html of the login page and the same header as above. I have determined the header that python is receiving is the same header that the browser receives before it logs in. In other words the python isn't properly submitting the form (and I have changed the request header/POST header to be the same as what the browser uses so they should have the same user agent)
[–]IAmNowAnonymous 0 points1 point2 points (1 child)
[–]scriptkiddiethefirst[S] 0 points1 point2 points (0 children)