I have found the solution to my syntax error, however I do not understand why original code did not work.
I have tried multiple different modules, like urllib
, requests
, and even sockets
. however i was directed to use requests
for its simplicity and accuracy. I do not want to give alot of info away as this was a challenge on HTB, however the people i have reached out too just tell me "ya man your syntax looks fine" and are no help....bah. If i need to remove post due to spoilers I will do so immediately.
Thee below snippet worked
import requests
import hashlib
s = requests.session()
url = 'someURL'
r = s.get(url)
cookie = s.cookies.get_dict() ##CHANGING THIS WORKED
x = r.text[167:187] #grabbing specific string
a = hashlib.md5(x.encode('utf-8')).hexdigest() ### CHANGING THIS WORKED
# b = s.post(url, data=a, cookies={'PHPSESSID': '{cookie}'})
final = s.post(url, data={'hash':a}, cookies=cookie)
print(final.text)
I am expecting the PHPSESSID to be passed in cookie form back to the server during the request. the above syntax works, however this does not...
import requests
import hashlib
s = requests.session()
url = 'someURL'
r = s.get(url)
cookie = s.cookies['PHPSESSID']
x = r.text[167:187]
h = hashlib.md5()
h.update(x)
a = h.hexdigest()
b = s.post(url, data=a, cookies={'PHPSESSID': '{cookie}'})
print(b.text)
what if i wanted to assign myself a cookie? I dont understand how get_dict()
is working and the other is not.
The cookie is actually passed correctly in both situations. the key difference being the way the
hashlib
module handlesunicode-objects
.
Solution
because x
is a unicode-object, I have to encode it before its sent to hexdigest()
that is why
h = hashlib.md5(x.encode('utf-8')).hexdigest()
works and the other does not. simply encoding x will work.