gpt4 book ai didi

linux - 通过 Bash 登录站点 (Stack Overflow)

转载 作者:行者123 更新时间:2023-12-04 15:57:15 25 4
gpt4 key购买 nike

如何在 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。

最佳答案

仅供娱乐:连接:keep-alive

简介

我的目标是构建一个 Bash 环境 当前控制台(或脚本)可以在其中交互处理经过身份验证的 HTTPS session 。这可用于解决很多 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[@]}"
}

Usage: 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

(刚刚)。

当然,您现在可以随时运行 doReq,因为连接保持打开状态。我们现在处于 简介 中引用的环境/条件。 (我网站上的版本做一个永远的循环,以更有效的方式请求它,每次四舍五入两分钟(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.txt 找到这个带有扩展主循环 的脚本(不太简洁) , getSo.sh .

2022-09-29 编辑 getSo.sh badge1 替换为 badge3。 (脚本仅在您已经获得至少一枚青铜徽章时才有效!!这是一个错误,但比需要金徽章的先前版本无害。)

关于linux - 通过 Bash 登录站点 (Stack Overflow),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68143422/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com