【问题标题】:How to use curl with Django, csrf tokens and POST requests如何在 Django、csrf 令牌和 POST 请求中使用 curl
【发布时间】:2012-05-24 14:10:10
【问题描述】:

我正在使用 curl 来测试我的 Django 表单之一。我尝试过的调用(每个调用都有错误,并且为了便于阅读,多行):

(1):

curl
-d "{\"email\":\"test@test.com\"}"
--header "X-CSRFToken: [triple checked value from the source code of a page I already loaded from my Django app]"
--cookie "csrftoken=[same csrf value as above]"
http://127.0.0.1:8083/registrations/register/

(在 cookie 中带有 http 标头和 csrftoken)导致 400 错误且没有返回数据。

(2):

curl
-d "{a:1}"
--header "X-CSRFToken:[as above]"
--cookie "csrftoken=[as above];sessionid=[from header inspection in Chrome]"
http://127.0.0.1:8083/registrations/register/

(如 (1) 中,但标头属性声明中没有空格,并且 cookie 中也有 sessionid)导致相同的 400 错误,没有返回数据。

(3):

curl
-d "{a:1}"
--header "X-CSRFToken:[as above]"
http://127.0.0.1:8083/registrations/register/

(只有带有X-CSRFToken的http头,没有cookie)导致错误代码403,消息:CSRF cookie未设置。

如何使用 curl 测试我的表单?除了 cookie 值和 http 标头之外,我没有考虑哪些因素?

【问题讨论】:

  • 您能告诉我您是如何获得 X-CSRF-Token 的吗? --header "X-CSRFToken: [triple checked value from the source code of a page I already loaded from my Django app]"

标签: django curl django-forms csrf django-csrf


【解决方案1】:

我用这样的 curl 工作

  • 您必须在标头中提交 csrftoken 作为 X-CSRFToken。
  • 您必须以 JSON 格式提交表单数据。 演示,

首先我们将获取 csrf_token 并存储在 cookie.txt(或他们所说的 cookie.jar)中

$ curl -c cookie.txt http://localhost.com:8000/ 

cookie.txt 内容

# Netscape HTTP Cookie File
# http://curl.haxx.se/docs/http-cookies.html
# This file was generated by libcurl! Edit at your own risk.
localhost.com  FALSE   /   FALSE   1463117016  csrftoken   vGpifQR12BxT07moOohREGmuKp8HjxaE

接下来我们以json格式重新发送用户名、密码。 (您可以以正常方式发送)。检查json数据转义。

$curl --cookie cookie.txt http://localhost.com:8000/login/   -H "Content-Type: application/json" -H "X-CSRFToken: vGpifQR12BxT07moOohREGmuKp8HjxaE" -X POST -d "{\"username\":\"username\",\"password\":\"password\"}" 
{"status": "success", "response_msg": "/"}
$

您可以将返回的新 csrf_token 会话 cookie 存储在同一文件或新文件中(我已使用选项 -c 存储在同一文件中。)

$curl --cookie cookie.txt http://localhost.com:8000/login/   -H "Content-Type: application/json" -H "X-CSRFToken: kVgzzB6MJk1RtlVnyzegEiUs5Fo3VRqF" -X POST -d "{\"username\":\"username\",\"password\":\"password\"}" -c cookie.txt

-cookie.txt的内容

# Netscape HTTP Cookie File
# http://curl.haxx.se/docs/http-cookies.html
# This file was generated by libcurl! Edit at your own risk.

localhost.com  FALSE   /   FALSE   1463117016  csrftoken   vGpifQR12BxT07moOohREGmuKp8HjxaE
#HttpOnly_localhost.com    FALSE   /   FALSE   1432877016  sessionid   cg4ooly1f4kkd0ifb6sm9p

当您在 cookie.txt 中存储新的 csrf_token 和会话 id cookie 时,您可以在整个网站上使用相同的 cookie.txt。

您正在从 cookie.txt (--cookie) 的先前请求中读取 cookie,并从同一 cookie.txt (-c) 中的响应中写入新的 cookie。

现在可以使用 csrf_token 和会话 ID 阅读和提交表单。

$curl --cookie cookie.txt http://localhost.com:8000/home/

【讨论】:

  • 如果从 cmd 调用,curl -c cookie.txt http://localhost.com:8000/ 在我的情况下不会保存文件。
  • 抓住session 和csrf cookie like here
【解决方案2】:

标头中的X-CSRFToken 只需与cookie 中的csrftoken 相同即可。

示例:

curl -v http://www.markjour.com/login/ -H "X-CSRFToken: 123" -b "csrftoken=123" -d "username=admin&password=admin"

【讨论】:

    【解决方案3】:

    curl-auth-csrf 是一个基于 Python 的开源工具,能够为您执行此操作:“模仿 cURL,但执行登录并处理任何跨站点请求伪造 (CSRF) 令牌的 Python 工具。用于抓取 HTML通常只有在登录时才能访问。”

    这将是你的语法:

    echo -n YourPasswordHere | ./curl-auth-csrf.py -i http://127.0.0.1:8083/registrations/register/ -d 'email=test@test.com&a=1' http://127.0.0.1:8083/registrations/register/
    

    这将传递列出的 POST 数据,但也包括通过标准输入传递的密码。我假设您“登录”后访问的页面是同一页面。

    完全披露:我是 curl-auth-csrf 的作者。

    【讨论】:

    • 你有没有办法将生成的 cookie 存储到文件中,类似于 curl 的做法,以便之后可以针对该 cookie 运行其他 curl 命令?我查看了您的代码,但没有看到如何做到这一点。如果那里有,那将非常有用,因为我需要在登录后进行 POST,而不是 GET。
    • curl 使用旧的 Netscape 格式的 cookie。您可以使用 Python 的 http.cookiejar.MozillaCookieJar 以 save() 格式保存 cookie。例如,查看this answer。然而,通常这可能比要求的更困难。对于您在获取 cookie 后想要发出的任何 curl 请求,只需在调用 curl-auth-csrf.py 时将这些 URL 附加到列表中即可。该脚本将代表您应用会话 cookie。
    【解决方案4】:

    Damien 的回答和您的示例 2 对我有用。我用一个简单的登录页面来测试,我希望你的注册视图是相似的。 Damien 的响应几乎可以正常工作,但缺少 sessionid cookie。

    我推荐一种更稳健的方法。与其手动输入来自其他请求的 cookie,不如尝试使用 curl 内置的 cookie 管理系统来模拟完整的用户交互。这样,您就可以减少出错的机会:

    $ curl -v -c cookies.txt -b cookies.txt host.com/registrations/register/
    $ curl -v -c cookies.txt -b cookies.txt -d "email=user@site.com&a=1&csrfmiddlewaretoken=<token from cookies.txt>" host.com/registrations/register/
    

    第一个 curl 模拟用户首先通过 GET 请求到达页面,并保存所有必要的 cookie。第二个 curl 模拟填写表单字段并将它们作为 POST 发送。请注意,您必须按照 Damien 的建议在 POST 数据中包含 csrfmiddlewaretoken 字段。

    【讨论】:

    【解决方案5】:

    这是我使用其余框架教程的方法

    打开浏览器,例如chrome 然后按 F12 打开开发人员选项卡并监控网络,使用您的用户凭据登录并从监控 POST 中获取您的 CRSF 令牌

    然后在 curl 中执行:

    curl http://127.0.0.1:8000/snippets/ \
     -X POST \
     -H "Content-Type: application/json" \
     -H "Accept: text/html,application/json" \
     -H "X-CSRFToken: the_token_value" \
     -H "Cookie: csrftoken=the_token_value" \
     -u your_user_name:your_password \
     -d '{"title": "first cookie post","code": "print hello world"}' 
    

    我认为不将令牌放在正文中而是使用 X-CSRFToken 将标头放在头中会更干净

    【讨论】:

    • 我需要-H "X-CSRFToken: the_token_value" 属性才能使其正常工作。完整代码,先获取cookiecurl -v -c cookies.txt -b cookies.txt http://url.com。我打开并从cookies.txt 复制了csrftoken 然后发布:curl -v -c cookies.txt -b cookies.txt -H "X-CSRFToken:&lt; the token &gt;" --data "foo=bar&amp;csrftoken=&lt; the token &gt;" http://url.com
    【解决方案6】:

    试试:

    curl
     -d "email=test@test.com&a=1"
     http://127.0.0.1:8083/registrations/register/
    

    特别注意-d 参数的格式。

    但是,这可能行不通,因为您的视图可能需要 POST 请求而不是 GET 请求。因为它会修改数据,而不仅仅是返回信息。

    只有“不安全”请求(POST、PUT、DELETE)才需要 CSRF 保护。它的工作原理是根据“csrfmiddlewaretoken”表单字段或“X-CSRFToken”http 标头检查“csrftoken”cookie。

    所以:

    curl
     -X POST
     -d "email=test@test.com&a=1&csrfmiddlewaretoken={inserttoken}"
     --cookie "csrftoken=[as above]"
     http://127.0.0.1:8083/registrations/register/
    

    也可以使用--header "X-CSRFToken: {token}" 而不是将其包含在表单数据中。

    【讨论】:

    • -d 意味着 POST 所以额外的 -X 是完全多余的
    • 我必须将 sessionid 添加到 cookie 中才能在我的情况下工作:--cookie "csrftoken=[as above];sessionid=[corresponding session id]"
    猜你喜欢
    • 2012-10-01
    • 2019-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-28
    • 2016-08-21
    • 2018-11-02
    • 2015-05-16
    相关资源
    最近更新 更多