【问题标题】:How to simulate stdin in shell script and check the stdout answer如何在 shell 脚本中模拟标准输入并检查标准输出答案
【发布时间】:2020-11-24 18:45:39
【问题描述】:

我目前正在做 overthewire 练习,但我遇到了一个问题,或者更准确地说,我想解决一个与我在网络上找到的不同的 lvl。

我必须将密码传递给带有 pin 码的 nc 命令,据此我在 stdout 上写了一个答案(告诉它是对还是错)。 通行证是通过修复第一部分和 0000 到 9999 之间的密码进行的。

事实是,通过使用 cmd 行执行此操作,就像:echo "my pass" "4-digits pin code" | nc localhost 30002 然后我会根据我的 pin 是否正确得到答案。

我想作为脚本做什么(也许从第一步开始我就完全错了): 我与 nc 可以接受的人核实,以使用“-e”选项启动脚本。 所以我决定构建一个脚本来执行以下操作:

我在端口上调用本地主机:nc localhost 30002 -e "myScript" 在我的脚本中,我想递归地将通行证和 4 位密码的组合回显到标准输入,以便让服务器回答正确或错误。 然后我检查标准输出(我想我可以从服务器读取正确或错误的答案),以确定我是否找到了正确的密码。

只是说,我只知道我能得到的错误答案,不知道正确的答案是什么。

这是我尝试构建的代码(请不要发火,我是新手 :)):

#!/bin/bash                                                                                                                                          
 
pwd=UoMYTrfrBFHyQXmg6gzctqAwOmw1IohZ
 
#nc localhost 30002
 
for i in {0001..9999}
do
    cat <<< "$pwd $i" #Here I try to simulate a input on stdin
    cat 1> /tmp/Frack/password
        if ( grep -o "Wrong" { "$(</dev/fd/3)"; } 3<&1 ) #Here I try to check if there's an answer from the server, and if it contains the word "Wrong"
    then
        continue
    else
        echo "pass trouvé : $i" > /tmp/Frack/password #If I found the right pin code, I write it within this file (on which I gave the right access to be able to write on it).
        break
    fi
  
 done

我添加了一个示例,说明我应该读取的内容作为我对服务器的呼叫的答案,但密码错误:“我是用户 bandit25 的密码检查器。请输入用户 bandit24 的密码和密码单行密码,用空格隔开。 错误的!请输入正确的密码。再试一次。”

【问题讨论】:

  • I check with the man that nc could accept to launch a script with the '-e' option netcat 有(可悲的)多个版本。 BSD netcat 不支持 -e。你确定你使用的是 GNU netcat 吗? Here is the code I tried to build 有用吗? $(&lt;/dev/fd/3)fd/3 你在什么地方打开了吗?
  • would like to, recursively, 为什么不迭代呢?您在脚本中迭代每个可能的组合。为什么是递归的? Then I check the stdout你必须描述通信的样子。您将由 两列 组成的 ascii 可打印数据单行 传递给远程服务器。你从服务器得到的答案是什么格式的?
  • 嗨,谢谢你的回答,因为递归,我不是英语所以只是一个错误,但快速查看我的代码表明我正在循环(所以迭代地做你是对的) .对于我的代码,不幸的是它不起作用并且文件是空的。

标签: linux shell stdout stdin


【解决方案1】:

你可以:

#!/bin/bash
# note stdout is connected to remote end
# note stdin reads from remote end
for i in {0001..9999}; do
    # hacky hack to output to controlling terminal
    echo "testing $i" > /dev/tty
    # output the password to stdout
    printf "%s\n" "password $i"
    # read one line from stdin
    if ! IFS= read -r line; then
         echo "error" > /dev/tty
         exit 1
    fi
    # if the word "wrong" case insensitive is missing from readed line
    if ! grep -i -q 'wrong' <<<"$line"; then
        # then we are finished
        echo "I'm in" > /dev/tty
        exit 0
    fi
done

那么我可以:

$ nc -e /tmp/1.sh localhost 10000
testing 0001
testing 0002
testing 0003
testing 0004
I'm in
< --- snip -------- >
$ nc -v -v -l localhost -p 10000 <<<$' wrong \n wrong \n wrong \n ok'
Listening on any address 10000 (ndmp)
Connection from 127.0.0.1:39152
password 0001
password 0002
password 0003
password 0004
read(net): Connection reset by peer

【讨论】:

  • 您好,感谢您的回答,但不适用于我。它只执行第一个循环,我猜它正在读取屏幕上的内容,而不是 nc 调用发送的内容。我添加了一个 nc 调用可以在我的第一篇文章中发送的答案。
  • 我补充说,经过进一步测试,我似乎只有服务器答案的第一部分,但没有错误的句子(所以你的 if 不起作用),我再次尝试修改你的 if。
猜你喜欢
  • 1970-01-01
  • 2015-01-26
  • 1970-01-01
  • 2013-09-18
  • 2016-03-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-23
相关资源
最近更新 更多