如何在 Shell 中使用 CURL 在支持 CSRF 的网站上通过 https 上传文件?

How to upload files via https on CSRF enabled website using CURL in Shell?

我需要使用为 HTTPS 协议启用的 CURL and upload a file in a specific folder. I able to login into the website via curl on CSRF enabled website via HTTPS. However am unable to upload the file. I guess its because of CSRF 令牌通过 https 协议登录网站。 所以我需要弄清楚如何处理 CSRF 令牌并通过 HTTPS 协议使用 CURL 命令上传文件。

登录格式:(有效)

curl -u username:password https://ftp.example.com

上传格式:(不工作)

curl -k -T "./testfile.txt" -u "username":"password" https://example.com/upload/

注1:我目前运行这个命令是RHEL 6.

注2:我需要处理CSRF token。

注意 3:我能够通过 ftp 和 sftp 协议 login/upload。

非常感谢为此提供解决方案。谢谢!

可行的解决方案!

最后,我是如何设法找到解决方案的,

我们只需要在第一次连接时下载 CSRF 令牌,然后在第二次尝试连接时使用下载的 CSRF 令牌。让我们看看下面的示例脚本。

#!/bin/sh

logs_path="/home/usr/logs" #Change your path here
my_username="testuser"     #Change your username here
my_password="testuser"     #Change your password here
host_name="localhost.com"  #Change your sitename here   

touch "$logs_path/testfile.test"

curl -k -c "$logs_path/cookies.txt" -u "$my_username":"$my_password" "https://$host_name:443" > /dev/null 2>&1

curl -k -b "$logs_path/cookies.txt" -T "$logs_path/testfile.test" -u "$my_username":"$my_password" "https://$host_name:443/puts/" > /dev/null 2>&1

status="$?"
if [[ $status == 0 ]]; then
echo -e "\nSuccess!"
else
echo -e "\nFailed!"
fi