Chrome无法获取本地存储



我目前正在尝试制作一个脚本,您可以使用令牌自动登录discord。然而,当我尝试做window.localStorage.setItem("token", "value");时,它只是说CCD_ 2。那么,如果window.localStorage未定义,我如何更改它并访问本地存储?

如果有帮助的话,这是我的完整代码:

from selenium import webdriver
token = input("Enter User Token:n  -> ")
driver = webdriver.Chrome()
driver.get("https://www.discord.com/login")
driver.execute_script(f"window.localStorage.setItem('token', '{token}');")
driver.refresh()

编辑:我的最终代码在这个GitHub Repo上可用。(https://github.com/RealMoondancer/DiscordTokenLogin)

我为其他页面测试了window.localStorage,只有这个页面没有。

在检查了我在中发现的所有JavaScript文件中的单词localStorage之后

https://discord.com/assets/43c944f57ecd3f83e53c.js

线路

delete window.localStorage

它删除了这个对象-这就产生了问题。

我认为他们这样做是出于安全考虑。

delete之前还有

r=window.localStorage

这可能意味着他们将其分配给变量r以保持访问权限,但这个变量对我不起作用。可能他们稍后将其分配到其他变量,但代码被混淆了,我无法识别他们将其指定在哪里。


编辑:

在谷歌上查看后,我发现了类似的问题

Discord窗口.localStorage未定义。如何访问Discord上的localStorage页面?

答案显示了如何重新创建对CCD_ 8的访问。

// If we create an <iframe> and connect it to our document, its
// contentWindow property will return a new Window object with
// a freshly created `localStorage` property. Once we obtain the
// property descriptor, we can disconnect the <iframe> and let it
// be collected — the getter function itself doesn’t depend on
// anything from its origin realm to work**.
function getLocalStoragePropertyDescriptor() {
const iframe = document.createElement('iframe');
document.head.append(iframe);
const pd = Object.getOwnPropertyDescriptor(iframe.contentWindow, 'localStorage');
iframe.remove();
return pd;
}
// We have several options for how to use the property descriptor
// once we have it. The simplest is to just redefine it:
Object.defineProperty(window, 'localStorage', getLocalStoragePropertyDescriptor());
window.localStorage.heeeeey; // yr old friend is bak
// You can also use any function application tool, like `bind` or `call`
// or `apply`. If you hold onto a reference to the object somehow, it
// won’t matter if the global property gets deleted again, either.
const localStorage = getLocalStoragePropertyDescriptor().get.call(window);

当我在JavaScript控制台(用于页面discord.com(中测试它时,这对我来说很有效


编辑:

最小工作代码。

from selenium import webdriver
import time
def test1(driver, url, token='abc'):
print('[test1] url:', url) 
driver.get(url)
time.sleep(1)
try:
driver.execute_script(f"window.localStorage.setItem('token', '{token}');")
driver.refresh()
print('[test1] get token:', driver.execute_script(f"return window.localStorage.getItem('token');") )
except Exception as ex:
print('[Exception]', ex)

def test2(driver, url, token='abc'):
recreate_localStorage_script = '''
const iframe = document.createElement('iframe');
document.head.append(iframe);
const pd = Object.getOwnPropertyDescriptor(iframe.contentWindow, 'localStorage');
iframe.remove();    
Object.defineProperty(window, 'localStorage', pd);
'''
print('[test2] url:', url) 
driver.get(url)
time.sleep(1)

try:
driver.execute_script(recreate_localStorage_script)
driver.execute_script(f"window.localStorage.setItem('token', '{token}');")
driver.refresh()
driver.execute_script(recreate_localStorage_script)
print('[test2] get token:', driver.execute_script(f"return window.localStorage.getItem('token');") )
except Exception as ex:
print('[Exception]', ex)
if __name__ == '__main__':
token = 'abc'
driver = webdriver.Chrome()
#driver = webdriver.Firefox()  # raise error when you try to use localStorage: "SecurityError: The operation is insecure."
# selenium.common.exceptions.JavascriptException: Message: SecurityError: The operation is insecure.
test1(driver, "https://httpbin.org/get", token)    # OK
test1(driver, "https://stackoverflow.com", token)  # OK if it sleeps few (milli)seconds
test1(driver, "https://discord.com/login", token)  # ERROR
test2(driver, "https://httpbin.org/get", token)    # OK
test2(driver, "https://stackoverflow.com", token)  # OK
test2(driver, "https://discord.com/login", token)  # OK

BTW:

当我尝试在Firefox中使用localStorage时,它会引发错误

selenium.common.exceptions.JavascriptException: 
Message: SecurityError: The operation is insecure.

它可能需要其他东西来解决这个问题。


编辑:

对于其他访客:正如@Moondancer在评论中提到的,如果token像这个一样在" "中,它就会起作用

driver.execute_script(f"window.localStorage.setItem('token', '"{token}"');")

相关内容

  • 没有找到相关文章

最新更新