如何使用 wget 越过登录页面?

Lor*_*ard 7 download passwords wget login

我正在尝试使用Wget下载我的私人 GitHub 页面,但我无法通过登录屏幕。

如何login/password在登录页面上发送using post 数据,然后以经过身份验证的用户身份下载实际页面?

这是我尝试使用输出运行的命令:

 wget --save-cookies cookies.txt \
 --post-data 'login=myUserName&password=myPassword' \
 https://github.com/login
Run Code Online (Sandbox Code Playgroud)

输出:

Resolving github.com... 207.97.227.239
Connecting to github.com|207.97.227.239|:443... connected.
HTTP request sent, awaiting response... 403 Forbidden
2012-11-23 19:58:13 ERROR 403: Forbidden.
Run Code Online (Sandbox Code Playgroud)

我也尝试了以下命令:

 wget --save-cookies cookies.txt \
 --post-data 'authenticity_token=sPV07gM2/OHYDAT99WmawItd8R7hiTaJnBAs/b3zN9Y=&login=myUserName&password=myPassword' \
 https://github.com/login
Run Code Online (Sandbox Code Playgroud)

这是登录页面的表单 HTML 代码https://github.com/login

<form accept-charset="UTF-8" action="/session" method="post"><div style="margin:0;padding:0;display:inline"><input name="authenticity_token" type="hidden" value="sPV07gM2/OHYDAT99WmawItd8R7hiTaJnBAs/b3zN9Y=" /></div> 
    <h1>Sign in <a href="https://github.com/plans">(Pricing and Signup)</a> </h1>
    <div class="formbody">

        <label for="login_field">
            Username or Email<br />
            <input autocapitalize="off" autofocus="autofocus" class="text" id="login_field" name="login" style="width: 21em;" tabindex="1" type="text" />
        </label>

        <label for="password">
            Password <a href="/sessions/forgot_password">(forgot password)</a>
            <br />
            <input autocomplete="disabled" class="text" id="password" name="password" style="width: 21em;" tabindex="2" type="password" />
        </label>

        <label class='submit_btn'>
            <input name="commit" tabindex="3" type="submit" value="Sign in" />
        </label>
    </div>
</form>
Run Code Online (Sandbox Code Playgroud)

Dan*_*eck 10

不做浏览器会做的事情,你犯了几个错误:

  • 您需要将带有登录凭据的 POST 请求发送到表单操作,即https://github.com/session.
  • 您需要提供所有表单参数,包括百分比编码的隐藏表单参数authenticity_token
  • 您需要提供由 设置的会话 cookie /login

我所期望的唯一不需要的是设置引用者。


你需要做什么:

$ wget --keep-session-cookies --save-cookies cookies.txt -O login.rsp https://github.com/login
$ grep authenticity_token login.rsp
Run Code Online (Sandbox Code Playgroud)

这将请求登录页面,存储会话,并打印CSRF令牌隐藏表单值(加上一些周围的 HTML)。

现在在所有参数进行百分比编码后登录,尤其是隐藏表单参数的值authenticity_token,通常包含标点符号:

 $ wget --load-cookies cookies.txt --keep-session-cookies --save-cookies cookies.txt --post-data='login=USERNAME&password=PASSWORD&authenticity_token=TOKEN_VALUE_PRINTED_BY_GREP_THEN_PERCENT_ENCODED' https://github.com/session
Run Code Online (Sandbox Code Playgroud)

你会被弹跳一下,最终会出现在https://github.com,就像在浏览器中登录一样。

  • 这不再起作用,得到“错误 422:不可处理的实体”。 (2认同)