use*_*114 428 curl credentials
我想访问需要用户名/密码的URL.我想尝试用curl访问它.现在我做的事情如下:
curl http://api.somesite.com/test/blah?something=123
Run Code Online (Sandbox Code Playgroud)
我收到一个错误.我想我需要指定一个用户名和密码以及上面的命令.
我怎样才能做到这一点?
Fin*_*arr 644
使用该-u
标志包含用户名,curl将提示输入密码:
curl -u username http://example.com
Run Code Online (Sandbox Code Playgroud)
您也可以在命令中包含密码,但是密码将在bash历史记录中显示:
curl -u username:password http://example.com
Run Code Online (Sandbox Code Playgroud)
Pie*_*e D 230
这样做更安全:
curl --netrc-file my-password-file http://example.com
Run Code Online (Sandbox Code Playgroud)
...因为在命令行上传递普通用户/密码字符串是个坏主意.
密码文件的格式是(按照man curl
):
machine <example.com> login <username> password <password>
Run Code Online (Sandbox Code Playgroud)
注意:
https://
或类似!只是主机名.machine
',' login
'和' password
'只是关键字; 实际信息是那些关键字之后的东西.Dan*_*son 74
或者同样的事情,但不同的语法
curl http://username:password@api.somesite.com/test/blah?something=123
Run Code Online (Sandbox Code Playgroud)
Kri*_*ian 61
您也可以通过写入来发送用户名:
curl -u USERNAME http://server.example
Run Code Online (Sandbox Code Playgroud)
然后,Curl将询问您密码,并且屏幕上将不会显示密码(或者如果您需要复制/粘贴命令).
小智 23
要在脚本中安全地传递密码(即防止它显示ps auxf或日志),您可以使用-K-标志(从stdin读取配置)和heredoc:
curl --url url -K- <<< "--user user:password"
Run Code Online (Sandbox Code Playgroud)
use*_*364 10
curl -X GET -u username:password {{ http://www.example.com/filename.txt }} -O
Run Code Online (Sandbox Code Playgroud)
小智 10
很简单,请执行以下操作:
curl -X GET/POST/PUT <URL> -u username:password
Run Code Online (Sandbox Code Playgroud)
小智 8
其他答案建议 netrc 根据我阅读的内容指定用户名和密码,我同意。下面是一些语法细节:
https://ec.haxx.se/usingcurl-netrc.html
像其他答案一样,我想强调需要注意这个问题的安全性。
虽然我不是专家,但我发现这些链接很有见地:
https://ec.haxx.se/cmdline-passwords.html
总结一下:
使用协议的加密版本(HTTPS 与 HTTP)(FTPS 与 FTP)有助于避免网络泄漏。
使用netrc可以帮助避免命令行泄漏。
更进一步,您似乎还可以使用 gpg 加密 netrc 文件
https://brandur.org/fragments/gpg-curl
有了这个,您的凭据就不会“静止”(存储)为纯文本。
通常CURL命令称为
curl https://example.com\?param\=ParamValue -u USERNAME:PASSWORD
Run Code Online (Sandbox Code Playgroud)
如果您没有任何密码,或者想跳过命令提示符以要求输入简单密码,请将密码部分留空。
即 curl https://example.com\?param\=ParamValue -u USERNAME:
要让密码最少不要弹出.bash_history
:
curl -u user:$(cat .password-file) http://example-domain.tld
Run Code Online (Sandbox Code Playgroud)
小智 6
你可以使用像这样的命令,
curl -u user-name -p http://www.example.com/path-to-file/file-name.ext > new-file-name.ext
Run Code Online (Sandbox Code Playgroud)
然后将触发 HTTP 密码。
参考:http : //www.asempt.com/article/how-use-curl-http-password-protected-site
我在 bash (Ubuntu 16.04 LTS) 中有同样的需求,答案中提供的命令在我的情况下不起作用。我不得不使用:
curl -X POST -F 'username="$USER"' -F 'password="$PASS"' "http://api.somesite.com/test/blah?something=123"
Run Code Online (Sandbox Code Playgroud)
-F
只有在使用变量时才需要参数中的双引号,因此从命令行... -F 'username=myuser' ...
就可以了。
相关安全注意事项:正如Mark Ribau 先生在评论中指出的,此命令在进程列表中显示密码($PASS 变量,已扩展)!
将凭据传递给curl 的最安全方法是提示插入它们。这就是按照前面的建议传递用户名时发生的情况 ( -u USERNAME
)。
但是如果您无法通过这种方式传递用户名怎么办? 例如,用户名可能需要成为 url 的一部分,而只有密码成为 json 负载的一部分。
tl;dr: 这是在这种情况下安全使用curl的方法:
read -p "Username: " U; read -sp "Password: " P; curl --request POST -d "{\"password\":\"${P}\"}" https://example.com/login/${U}; unset P U
Run Code Online (Sandbox Code Playgroud)
read
将从命令行提示输入用户名和密码,并将提交的值存储在两个变量中,这些变量可以在后续命令中引用并最终取消设置。
我将详细说明为什么其他解决方案不理想。
为什么环境变量不安全
为什么直接在命令行上将其输入到命令中是不安全的
,因为您的秘密最终会被任何其他正在运行的用户看到ps -aux
,因为它列出了为每个当前正在运行的进程提交的命令。另外,因为您的 secrte 最终会出现在 bash 历史记录中(一旦 shell 终止)。
为什么将其包含在本地文件中不安全 对 文件进行严格的 POSIX 访问限制可以减轻这种情况下的风险。但是,它仍然是文件系统上的一个文件,静态时未加密。
简单而简单地说,最安全的方法是使用环境变量来存储/检索您的凭据.因此卷曲命令如:
curl -Lk -XGET -u "${API_USER}:${API_HASH}" -b cookies.txt -c cookies.txt -- "http://api.somesite.com/test/blah?something=123"
Run Code Online (Sandbox Code Playgroud)
然后就打电话给你宁静的API,并通过HTTP WWW_Authentication
使用的Base64编码值头部API_USER
和API_HASH
.在-Lk
刚刚告诉curl遵循HTTP重定向30倍和使用不安全的TLS处理(即忽略SSL错误).虽然double --
只是bash语法糖来停止处理命令行标志.此外,-b cookies.txt
和-c cookies.txt
标志处理cookie,-b
发送cookie并-c
在本地存储cookie.
归档时间: |
|
查看次数: |
702688 次 |
最近记录: |