使用带有用户名和密码的cURL?

使用带有用户名和密码的cURL?,curl,credentials,Curl,Credentials,我想访问需要用户名/密码的URL。我想尝试使用curl访问它。现在我正在做一些类似的事情: curl http://api.somesite.com/test/blah?something=123 我犯了一个错误。我想我需要在上面的命令中指定用户名和密码 如何操作?使用-u标志包含用户名,curl将提示输入密码: curl -u username http://example.com 您也可以在命令中包含密码,但您的密码将在bash历史记录中可见: curl -u username:pass

我想访问需要用户名/密码的URL。我想尝试使用curl访问它。现在我正在做一些类似的事情:

curl http://api.somesite.com/test/blah?something=123
我犯了一个错误。我想我需要在上面的命令中指定用户名和密码


如何操作?

使用
-u
标志包含用户名,curl将提示输入密码:

curl -u username http://example.com
您也可以在命令中包含密码,但您的密码将在bash历史记录中可见:

curl -u username:password http://example.com

或者相同但语法不同的东西

curl http://username:password@api.somesite.com/test/blah?something=123

您也可以通过以下方式发送用户名:

curl -u USERNAME http://server.example
然后,Curl将询问您密码,密码将不会显示在屏幕上(或者如果您需要复制/粘贴命令)。

更安全的做法是:

curl --netrc-file my-password-file http://example.com
…因为在命令行上传递普通的用户/密码字符串是个坏主意

密码文件的格式为(根据
man curl
):

机器登录密码
注:

  • 计算机名称不得包含
    https://
    或类似名称!只是主机名
  • 单词“
    machine
    ”、“
    login
    ”和“
    password
    ”只是关键词;实际信息是那些关键字后面的内容

  • 要使密码至少不会出现在您的
    .bash\u历史记录中,请执行以下操作:

    curl -u user:$(cat .password-file) http://example-domain.tld
    

    简单地说,最安全的方法是使用环境变量来存储/检索您的凭据。因此,curl命令类似于:

    curl -Lk -XGET -u "${API_USER}:${API_HASH}" -b cookies.txt -c cookies.txt -- "http://api.somesite.com/test/blah?something=123"
    
    然后调用restful api并传递http
    WWW\u Authentication
    标头,其中包含
    api\u USER
    api\u HASH
    的Base64编码值。
    -Lk
    只告诉curl遵循http30x重定向并使用不安全的tls处理(即忽略ssl错误)。而double
    --
    只是停止处理命令行标志的bash语法。此外,
    -b cookies.txt
    -c cookies.txt
    标志处理带有
    -b
    发送cookies和
    -c
    本地存储cookies的cookies


    本手册提供了更多方法。

    如果您使用的系统具有Gnome钥匙圈应用程序,则可以使用一种避免直接公开密码的解决方案从钥匙圈中提取密码:

    server=server.example.com
    file=path/to/my/file
    user=my_user_name
    pass=$(gkeyring.py -k login -tnetwork -p user=$user,server=$server -1)
    
    curl -u $user:$pass ftps://$server/$file -O
    

    您可以使用如下命令:

    curl -u user-name -p http://www.example.com/path-to-file/file-name.ext > new-file-name.ext
    
    然后将触发HTTP密码

    参考:

    要在脚本中安全地传递密码(即防止密码显示在ps auxf或日志中),您可以使用-K-标志(从stdin读取配置)和herdoc:

    curl --url url -K- <<< "--user user:password"
    

    curl--url-K-我在bash(ubuntu16.04 LTS)中也有同样的需求,答案中提供的命令在我的例子中无法工作。我必须使用:

    curl -X POST -F 'username="$USER"' -F 'password="$PASS"' "http://api.somesite.com/test/blah?something=123"
    
    只有在使用变量时,
    -F
    参数中才需要双引号,因此从命令行
    -F'username=myuser'…
    就可以了


    相关安全注意事项:作为注释点,此命令在processlist中显示密码($PASS variable,expanded)

    其他答案建议netrc根据我所读的内容指定用户名和密码,我同意。以下是一些语法详细信息:

    curl -X GET -u username:password  {{ http://www.example.com/filename.txt }} -O
    

    与其他答案一样,我要强调,在这个问题上需要注意安全

    虽然我不是专家,但我发现这些链接很有见地:

    总结如下:

    使用协议的加密版本(HTTPS vs HTTP)(FTPS vs FTP)有助于避免网络泄漏

    使用netrc可以帮助避免命令行泄漏

    更进一步,您似乎还可以使用gpg对netrc文件进行加密


    这样,您的凭证就不会以纯文本形式“静止”(存储)。

    非常简单,请执行以下操作:

    curl -X GET/POST/PUT <URL> -u username:password
    
    curl-xget/POST/PUT-u用户名:密码
    
    通常CURL命令称为

    curl https://example.com\?param\=ParamValue -u USERNAME:PASSWORD
    
    如果您没有任何密码,或者希望跳过命令提示以请求简单密码,请将密码部分留空


    i、 e.
    curlhttps://example.com\?param\=ParamValue-u用户名:

    将凭据传递给curl最安全的方法是提示插入凭据。这是按照前面的建议传递用户名时发生的情况(
    -u username

    但是如果你不能通过这种方式传递用户名呢? 例如,用户名可能需要是url的一部分,而只有密码才是json负载的一部分

    tl;医生: 这是如何在这种情况下安全地使用curl:

    read -p "Username: " U; read -sp "Password: " P; curl --request POST -d "{\"password\":\"${P}\"}" https://example.com/login/${U}; unset P U
    
    read
    将从命令行提示输入用户名和密码,并将提交的值存储在两个变量中,这两个变量可以在后续命令中引用并最终取消设置

    我将详细说明为什么其他解决方案并不理想

    为什么环境变量不安全

  • 无法跟踪环境变量内容的访问和公开模式(ps-eww),因为环境对进程隐式可用
  • 通常应用程序会抓取整个环境并记录下来以进行调试或监控(有时会记录在磁盘上的明文日志文件中,尤其是在应用程序崩溃后)
  • 环境变量被传递给子进程(因此打破了最小特权原则)
  • 维护它们是一个问题:新工程师不知道它们在那里,也不知道它们周围的需求——例如,不将它们传递给子流程——因为它们没有得到实施或记录
  • 为什么直接在命令行上将其输入到命令中是不安全的 因为运行
    ps-aux
    的任何其他用户都可以看到您的秘密,因为它列出了为每个当前运行的进程提交的命令。 这也是因为secrte会在bash历史记录中结束(一旦shell终止)

    为什么将其包含在本地文件中不安全 f上严格的POSIX访问限制
    read -p "Username: " U; read -sp "Password: " P; curl --request POST -d "{\"password\":\"${P}\"}" https://example.com/login/${U}; unset P U
    
    url='https://example.com'
    printf "Username: "
    read username
    printf "Password: "
    stty -echo  # disables echoing user input, POSIX equivalent for 'read -s'
    read pass
    printf "\n" # we need to move the line ahead
    stty echo   # re-enable echoing user input
    echo ${pass} | sed -e "s/^/-u ${username}:/" | curl --url "${url}" -K-
    unset username
    unset pass
    
    url='https://example.com'
    printf "Username: "
    read username
    printf "Password: "
    stty -echo  # disables echoing user input, POSIX equivalent for 'read -s'
    perl -e '
        my $val=<STDIN>;
        chomp $val;
        print STDERR "\n";  # we need to move the line ahead, but not send a newline down the pipe
        print $val;
    ' | sed -e "s/^/-u ${username}:/" | curl --url "${url}" -K-
    stty echo   # re-enable echoing user input
    unset username
    
    url='https://example.com'
    filepath="$(mktemp)"  # random path, only readable by current user
    printf "Username: "
    read username
    printf "Password: "
    stty -echo  # disables echoing user input, POSIX equivalent for 'read -s'
    read pass
    echo "${pass}" > "${filepath}"
    unset pass
    printf "\n" # we need to move the line ahead
    stty echo   # re-enable echoing user input
    
    cat "${filepath}" | sed -e "s/^/-u ${username}:/" | curl --url "${url}" -K-
    
    rm "${filepath}"  # don't forget to delete the file when done!!
    unset username
    
    url='https://example.com'
    filepath="$(mktemp)"  # random path, only readable by current user
    printf "Username: "
    read username
    printf "Password: "
    stty -echo  # disables echoing user input, POSIX equivalent for 'read -s'
    $(perl -e '
        my $val=<STDIN>;
        chomp $val;
        open(my $fh, ">", $ARGV[0]) or die "Could not open file \"$ARGV[0]\" $\!";
        print $fh $val;
        close $fh;
    ' "$filepath")
    printf "\n" # we need to move the line ahead
    stty echo   # re-enable echoing user input
    
    cat "${filepath}" | sed -e "s/^/-u ${username}:/" | curl --url "${url}" -K-
    
    rm "${filepath}"  # don't forget to delete the file when done!!
    unset username
    
    curl http://username:password@example.com
    
    curl http://admin:123456@example.com