PDA

View Full Version : Logging into Neopets - Python and Javascript/jQuery



Sephora
10-31-2020, 12:02 AM
1) I looked at the tutorial here ([Only registered and activated users can see links]) and tried to login in a similar way but I discovered all HTTP requests are blocked by StackPath. Is it possible to bypass StackPath and log in to Neopets via HTTP request? If anyone knows how to log in programmatically, could you share a snippet of code? It doesn't have to be in Python, I am willing to code in other languages.


import requests
login_url = '[Only registered and activated users can see links]'
with requests.Session() as s:
r = s.get(login_url)
print(r.content)
payload = {'destination': '[Only registered and activated users can see links]',
'username': 'username_here', 'password': 'password_here'}
r = s.post(login_url, data=payload)
print(r.content)

2) I also attempted to login through a GreaseMonkey userscript but I got the 'No username found! Please go back and re-enter your username' page. I'm confused since I think I'm providing it with the right parameters.


$.post( "[Only registered and activated users can see links]", {username:'username_here', password:'password_here', destination: 'neopets.com/index.phtml'},
function( data ) {
$( ".result" ).html( data );
alert(data);
});


I tried it like this as well but got the same thing

$.ajax({
url: '[Only registered and activated users can see links]',
type: 'POST',
data: jQuery.param({username:'username_here', password:'password_here'}) ,
contentType: 'application/x-[Only registered and activated users can see links] charset=UTF-8',
success: function (response) {
alert(response);
},
error: function () {
alert("error");
}
});

3) Lastly, I had the userscript go to the login page ([Only registered and activated users can see links]), auto-fill the username and password, but I could not get the script to hit the green 'Login' button.


$('input[name=username').val('username_here');
$('input[name=password').val('password_here');

//None of these clicked the sign in button:
//$("input.submit[name = 'welcomeLoginButton']").submit();
//$("input.submit[name = 'welcomeLoginButton']").click();
//$('body').on('click','welcomeLoginButton',function (){alert('logged in?');});
//new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable( By.cssSelector("input.submit[name = 'welcomeLoginButton']"))).click();
//

I would love if someone could share a solution to any of these three questions. Ideally, I would like to know how to login through both GreaseMonkey userscripts and an IDE.

ozfe
10-31-2020, 09:49 PM
Hope I can help a bit from my experiences.

1. Login:
From what I can see, you are just using the plain Pythons session mechanism. I'm not that familiar with Python, but in Kayses tutorial, it seems that the NeoAccount class wraps the Python requests and adds the referrer and headers (such as the User-agent).

If your Python script is submitting the default Python user agent header, it would be extremely easy for StackPath to detect it as spam.

You could also encounter StackPath issues if you're using a bad proxy or VPN.

2. Unknown username: not 100% sure about this, but looks like there is another variable called pageResponse. It might also check the cookies and headers.

3. There is no element with name = welcomeLoginButton. There is a '.welcomeLoginButton' (class), but they've also done something weird to it, preventing $('form .welcomeLoginButton').click() from working.

By "through an IDE" do you mean using Selenium?

WebDriverWait looks like something Selenium-related. This following for me in PHP (I presume the code would be similar in JS), assuming you have waited for everything to load:

$webDriver->findElement(WebDriverBy::cssSelector('form .welcomeLoginButton'))->click();

Sephora
11-01-2020, 01:59 PM
ozfe Thanks so much for your help! I disabled my VPN, added in headers, and I was able to successfully log in and get the html of pages.
I was able to get the HTML of the home page like this:

import urllib, [Only registered and activated users can see links]
import io, gzip
from io import BytesIO
cj = [Only registered and activated users can see links]()
cookie_handler = urllib.request.[Only registered and activated users can see links](cj)
headers = [('User-Agent', 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:15.0) Gecko/20100101 Firefox/15.0.1'),
('Accept', 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'),
('Accept-Language', 'en-us,en;q=0.5'),
('Accept-Encoding', 'gzip, deflate')]
opener = urllib.request.build_opener(cookie_handler)
url = '[Only registered and activated users can see links]'
referer = ''
opener.addheaders = [('Referer', referer)] + headers
res = opener.open(url)
referer = res.geturl()

def readable(data):
if 'gzip' in str(data.info()):
return gzip.GzipFile(fileobj=BytesIO(data.read())).read()
else:
return data.read()

if readable:
print(readable(res))
else:
print(res)

And I was able to log in like this:


from urllib.parse import urlparse

def readable(data):
if 'gzip' in str(data.info()):
return gzip.GzipFile(fileobj=BytesIO(data.read())).read()
else:
return data.read()


def neo_post(url, data, referer = '', readable = True):
url = '[Only registered and activated users can see links]'
referer = ''
opener.addheaders = [('Referer', referer)] + headers
res = opener.open(url, str.encode(urllib.parse.urlencode(data)))
if readable:
return readable(res)
else:
return res

def login(username, password):

res = neo_post('[Only registered and activated users can see links]', {'username': username,
'password': password,
'destination': "/index.phtml"}, readable = False)
#print res.geturl()
if 'badpassword' in res.geturl():
return False, 'Bad password'
elif 'hello' in res.geturl():
return False, 'Birthday locked'
elif 'login' in res.geturl():
return False, 'Frozen'
elif 'index' in res.geturl():
return True, 'Logged in'

neouser = "username_here"
neopass = "password_here"

acc = login(neouser, neopass)

For the question 3, I got the solution from someone on StackOverflow. I only had to do
$('.welcomeLoginButton')[0].click(); or
document.getElementsByClassName("welcomeLoginButton")[0].click();

Still seeking a solution to question 2 and would appreciate it very much if someone knows. :)

By IDE, I mean an integrated development environment like PyCharm, Spyder, Eclipse. Basically software you can download onto your computer that allows you to view code and run it.