通过 bash 登录站点(stackoverflow)

Maj*_*aba 3 linux bash curl

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

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"
Run Code Online (Sandbox Code Playgroud)

或者

curl --user myusername:mypassword https://stackoverflow.com/users/login?ssrc=head&returnurl=https%3a%2f%2fstackoverflow.com%2f -v 
Run Code Online (Sandbox Code Playgroud)

我尝试使用 Chrome 检查元素以复制curl请求,但它不起作用(可能它取决于 cookie 并且仅在特定时间段内有效)。

请注意,我需要使用用户名和密码而不是 cookie 登录。

F. *_*uri 7

仅供娱乐: Connection: keep-alive

介绍

我的目标是构建一个环境,在该环境中当前脚本可以交互地处理经过身份验证的 https 会话。这可用于解决许多物联网目标,解决任何提供商平台以监控个人帐户等。

不幸的是,这可能会被严重用于压力或黑客攻击网络平台(针对任何人),甚至针对SO 的狂热徽章(肮脏的骗子!)。

对于任何不当使用,我深表歉意,无论如何我不对人们的行为负责。

接近纯 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"
Run Code Online (Sandbox Code Playgroud)

openssl作为后台任务运行:

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

现在,有一个小doReq函数可以创建 2 个变量:$cookieand $htstatus和 3 个数组:$hthead, $htbodyand $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[@]}"
}
Run Code Online (Sandbox Code Playgroud)

用法: doReq /file_part_of_URL [method] [post datas]

让我们登录:

doReq /users/login POST "email=$user&password=$pass"
Run Code Online (Sandbox Code Playgroud)

现在显示我的徽章:

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
Run Code Online (Sandbox Code Playgroud)

在我的桌子上,用我的帐户,打印:

 ? 13 gold badges ? 88 silver badges ? 112 bronze badges                            
Run Code Online (Sandbox Code Playgroud)

(现在)。

当然,您现在可以随时运行doReq,因为连接保持打开状态。我们现在处于引言中引用的环境/条件中。(我网站上的版本做一个永远循环,以更有效的方式请求这个,每 20 秒,直到用户交互。见底部。)

...

完成后,在退出之前,您可以停止openssl并关闭您的 fd:

(我已经添加lsps作为调试命令)

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

本期节目:

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
Run Code Online (Sandbox Code Playgroud)

openssl过程完成,所有 3 个 FD 都关闭。

您可以在getSo.sh.txtgetSo.sh找到这个脚本(较少压缩),扩展主循环

  • 疯了吧。没有一个理智的人会在 bash 中解决这个问题。有点酷 (2认同)