python - requests // HTS login...

For the discussion of Perl, Python, Ruby, and PHP and other interpreted languages.

python - requests // HTS login...

Post by zduke on Tue Sep 04, 2018 3:30 pm
([msg=96215]see python - requests // HTS login...[/msg])

Ive written code the the programming mission level 2 thats all fine, im having problems login in too HTS using python and the requests library, ive tried with cookies etc its just not authenticating and the page content still saying 'you need to log in'
etc...snippet of the login code below.

How has anyone else logged into the main HTS site using python? have done it before with other sites...i'm at a loss.

# Not working atm, cant get authentication to work properly...do not know why. (completed mission, sending key by hand. But still...)

Code: Select all
def upload_code(key):
   login_url = 'https://www.hackthissite.org/user/login'

   btn_submit = 'Login'
   username = 'example_login' # using example login name for obv reasons
   password = 'example_password' # using example login name for obv reasons
   login = {
         'btn_submit':btn_submit,
         'password':password,
         'username':username,
         }   
            
   session = requests.session()
   session.get(login_url)

   cookies = {
   'phpbb3_28pla_sid':'example_sid', #using example not mine acct one here for obv reasons
   'phpbb3_28pla_u':'1',
   'phpsessid':'example_phpsessid', #using example not mine acct one here for obv reasons
   'style_cookie':'null',
   }

   header_auth = {
   'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:61.0) Gecko',
   'referer':'www.hackthissite.org/user/login'
   }
   auth = session.post(login_url,cookies=cookies,headers=header_auth,data=login)
   print(auth.content) # to see if im logged in ...WICH IM NOT

   challange_url = 'https://www.hackthissite.org/missions/prog/2/index.php'
   header = {'referer':'www.hackthissite.org/missions/prog/2/index.php'}
   data = {'solution':key}

   r = session.post(challange_url,cookies=cookies,headers=header,data=data,verify=True)
   print(r.content)   # to see page content of the proggramming mission page that im putting key in form




I only post this snippet to not ruin it for other people doing the challenge, i'm just annoyed as this should be working and easy and its not. This would just finish my code of nicely and means i wouldn't of had to put key and submit it by hand which was hard with that bloody timer. lol.

thanks. 8-)
zduke
New User
New User
 
Posts: 4
Joined: Tue Sep 04, 2018 3:20 pm
Blog: View Blog (0)


Re: python - requests // HTS login...

Post by NETWORKsecurity on Tue Sep 04, 2018 5:59 pm
([msg=96223]see Re: python - requests // HTS login...[/msg])

Hi! I think I have managed to get your script working by adding https:// before www.hackthissite.org in referer header
NETWORKsecurity
Super Moderator
Super Moderator
 
Posts: 129
Joined: Wed Oct 15, 2014 3:20 pm
Blog: View Blog (0)


Re: python - requests // HTS login...

Post by zduke on Wed Sep 05, 2018 5:03 am
([msg=96224]see Re: python - requests // HTS login...[/msg])

ahh just tried it and it is not working still, this is strange as normally can its so annoying lol how did it work for you?

-- Wed Sep 05, 2018 5:28 am --

I think it was working all along, and with your edit to the code. HTS always says you need to log in to view this in the code yet i have also found code that says view profile zduke.... in source code when I'm logged in viewing from browser, and also same code in my python requests...so i guess I'm logged in even tho some code says other wise? V weird

-- Wed Sep 05, 2018 5:33 am --

Nope my mistake, still not working. I'm going to rewrite the code just trying to log in without the rest and see what happens maybe a fresh perspective will help. This is just bugging me to death as Ive done this load's before lol

-- Wed Sep 05, 2018 6:08 am --

Rite, done it finally, you need to use cookies that is a must, also had to encode the headers with utf8 as page has a Latin encoded character so get an encoding error and i used request.session() so i can hopefully continue use with forms.
requests.session() is meant too get cookies and store cookies for persistent use but did not work when i done it. it worked with using session and adding my own dict off cookies to the post request on each request (login, post answer to form).
says Im logged in now, and asks if i want to logout in HTML code. glad i done this as now if happens in future i know a certain way to being able to log in and use pages and forms 100% :)

Well feels good to of got the mind fuck over with lol. simple things are always the ones that fuck me haha :D

-- Wed Sep 05, 2018 7:17 am --

works but logging in using username and password dose not work on own (or at all i think) or the session() dose not keep your cookies for future use either :D , using cookies=cookie works but brings you to the page you last visited in my browser which is weird so i am logged in now, but posting my key to form is not really going as planned and yea its just not behaving like it should to be honest. I'm going to leave it for now as i managed to at least load the page logged in, maybe a break from thinking about this will help haha.
zduke
New User
New User
 
Posts: 4
Joined: Tue Sep 04, 2018 3:20 pm
Blog: View Blog (0)



Return to Interpreted Languages

Who is online

Users browsing this forum: No registered users and 0 guests