curl 登录到一个网站(堆栈溢出)由Bash

tgabmvqs  于 2022-11-13  发布在  其他
关注(0)|答案(2)|浏览(176)

如何在Linux中使用Bash登录网站?
例如,为了登录到Stack Overflow,我尝试了许多不同的方法,如下所示,但没有任何效果。

wget --save-cookies cookies.txt --keep-session-cookies --post-data="username=blahblah&password=blahblahblha" "https://stackoverflow.com/users/login?ssrc=head&returnurl=https%3a%2f%2fstackoverflow.com%2f"

curl --user myusername:mypassword https://stackoverflow.com/users/login?ssrc=head&returnurl=https%3a%2f%2fstackoverflow.com%2f -v

我尝试使用Chrome来检查元素以复制curl请求,但没有成功(可能它依赖于cookie,并且仅在特定时间段内有效)。
请注意,我需要使用用户名和密码登录,而不是使用cookie。

byqmnocz

byqmnocz1#

仅供娱乐:* 一月一日 *

简介
我的目标是建立一个Bash环境,在这个环境中,当前的控制台(或脚本)可以交互式地处理一个经过验证的HTTPS会话。这可以用来处理大量的IoT目标,处理任何提供商平台来监控个人帐户,等等。
不幸的是,这可能会被错误地用于强调或黑客网络平台(针对任何人),甚至针对Stack Overflow's Fanatic badge(肮脏的骗子!)。
我为任何不当的使用道歉。无论如何,我不对人们做什么负责。

靠近***纯Bash***:仅需要OpenSSL:

...和gpg,但您可以自由地以其他方式存储您的 * 凭据 *。
准备一些值:

#!/bin/bash
shopt -s extglob

URL='https://stackoverflow.com/'
IFS=: read  -r user pass < <(gpg -qd <socred.gpg)

IFS=/ read -r _ _ hst _ <<<"$URL"

将OpenSSL可执行文件作为后台任务运行:

exec {wwwE}<> <(: - O)
exec {wwwI}<> <(: - b)
exec {wwwO}< <(
  exec stdbuf -o0 openssl s_client -quiet -connect "$hst":443 <&$wwwI 2>&$wwwE)
osslpid=$!

然后现在,有一个小doReq函数来创建两个变量:* $cookie * 和 * $htstatus,以及三个数组:$hthead$htbody * 和 * $hterr *:

doReq() {
    hthead=() htbody=() hterr=()
    local target=$1 method=${2:-GET} head=true line cookies
    printf >&$wwwI '%s\r\n' "$method $target HTTP/1.1" "Host: $hst" \
           "User-Agent: aobs/0.01" "Connection: keep-alive" "Accept: */*"
    [ "$cookie" ] && printf >&$wwwI '%s' "$cookie"
    if [ "$method" = "POST" ];then
        printf >&$wwwI '%s\r\n%s\r\n\r\n%s' "Content-Length: ${#3}" \
                       'Content-Type: application/x-www-form-urlencoded' "$3"
    else printf >&$wwwI '\r\n'
    fi
    read -t 10 -ru $wwwO line
    htstatus=${line%$'\r'} ; hthead=("$htstatus")
    while read -t .3 -ru $wwwO line;do
        [ "${line%$'\r'}" ] || head=false;
        if $head ;then
            hthead+=("${line%$'\r'}");
            case $line in
                [sS]et-[cC]ookie:* ) line=${line#*: };
                                 cookies+=("${line%%;*}");;
            esac
        else htbody+=("${line%$'\r'}") ;fi
    done
    if read -t 0 -ru $wwwE;then
        while read -t .1 -ru $wwwE line;do
            hterr+=("${line%$'\r'}")
            case $line in
                depth* | verify* ) ;;
                * ) echo "ERR: $line" ;;
    esac ; done ; fi
    [ ! -v "cookie" ] && [ "${cookies[0]}" ] &&
        printf -v cookie 'Cookie: %s\r\n' "${cookies[@]}"
}

用途:doReq /file_part_of_URL [method] [post datas]
让我们登录:

doReq /users/login POST "email=$user&password=$pass"

现在出示我的徽章:

doReq /
for ((i=${#htbody[@]};i--;)) ;do
    line="${htbody[i]}"
    case $line in
        *badge1* ) line="${htbody[i-1]}${htbody[i]}${htbody[i+1]}"
                   line=${line//>+([0-9])</><} line=${line//<*([^>])>}
                   printf '%b\n' "${line//&#9679;/ \\U25cf }" ;;
esac ; done

在我的table上,我的帐户,这打印:

● 13 gold badges ● 88 silver badges ● 112 bronze badges

(just现在)。
当然,你现在可以随时运行doReq了,因为连接是打开的。我们现在处于***简介***中引用的 environment/condition 中。(我的站点上的版本做了一个永久循环,以一种更有效的方式请求这个循环,每次 * 取整 * 两分钟(EPOCHSECONDS % 120),直到用户交互。请参见本文底部。)
...
完成后,在退出之前,可以停止openssl并关闭file descriptor
(我添加了lsps作为 debug 命令。)

ls -l /dev/fd/ ; ps --tty $(tty) ufw
kill $osslpid
exec {wwwE}<&-
exec {wwwI}>&-
exec {wwwO}<&-
ls -l /dev/fd/ ; ps --tty $(tty) ufw

这说明:

total 0
lrwx------ 1 user user 64 jui  2 13:52 0 -> /dev/pts/2
l-wx------ 1 user user 64 jui  2 13:52 1 -> /dev/pts/2
lrwx------ 1 user user 64 jui  2 13:52 10 -> pipe:[940266653]
lrwx------ 1 user user 64 jui  2 13:52 11 -> pipe:[940266654]
lr-x------ 1 user user 64 jui  2 13:52 12 -> pipe:[940266655]
lrwx------ 1 user user 64 jui  2 13:52 2 -> /dev/pts/2
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
user    28110  0.0  0.0  11812  7144 pts/7    Ss   jui01   0:02 bash
user    14038 30.0  0.0   9116  4228 pts/7    S+   13:52   0:00  \_ /bin/bash ./getSo.sh
user    14045  0.5  0.0   9356  5856 pts/7    S+   13:52   0:00       \_ openssl s_client -quiet -connect stackoverflow.com:443
user    14048  0.0  0.0  12404  3400 pts/7    R+   13:52   0:00       \_ ps --tty /dev/pts/7 ufw

total 0
lrwx------ 1 user user 64 jui  2 13:52 0 -> /dev/pts/2
l-wx------ 1 user user 64 jui  2 13:52 1 -> /dev/pts/2
lrwx------ 1 user user 64 jui  2 13:52 2 -> /dev/pts/2
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
user    28110  0.0  0.0  11812  7144 pts/7    Ss   jui01   0:02 bash
user    14038 30.0  0.0   9632  4756 pts/7    S+   13:52   0:00  \_ /bin/bash ./getSo.sh
user    14051  0.0  0.0  12404  3332 pts/7    R+   13:52   0:00      \_ ps --tty /dev/pts/7 ufw

OpenSSL进程完成,所有三个文件描述符都关闭。
您可以在getSo.sh.txtgetSo.sh找到这个脚本(不太精简),它有一个扩展的 * 主循环 *。

**2022-09-29编辑getSo.sh**将badge1替换为badge3。(只有当你已经获得至少一个青铜徽章时脚本才能工作!!这是一个bug,但比以前需要黄金徽章的版本无害。)

tv6aics1

tv6aics12#

它完全取决于目标网站,但例如,对于堆栈溢出,您可以使用以下命令:

curl -X POST "https://stackoverflow.com/users/login" -H "Content-Type: application/x-www-form-urlencoded" -d "email=user@email.com" -d "password=password" -c cookie.txt

# Verify your login
curl -b cookie.txt https://stackoverflow.com | grep YOUR_NAME
# OR check the status code from edit profile that should be 200 OK
curl -b cookie.txt https://stackoverflow.com/users/edit/YOUR_USER_ID -i 2>/dev/null

相关问题