3

As a fun challenge, there is a bash script that echo's to the screen an encoded output (base64). Every 15 seconds it will change. The goal is to input the decoded base64 value(s) and submit before the 15 seconds are up. Since its bash, you could easily cheat and just change the params or even time delay but my goal would be to leave the bash script alone and write another script using bash or python to catch output and decode the string automatically. Does anyone have an opinion on the best way to do this?

Is there a way to catch the output from this bash script and have it processed by an external script ?

Here is the challenge script:

#!/bin/bash

V_COUNT=0

f_encode(){
        V_NUM=$(echo $RANDOM)
        V_ENC=$(echo $V_NUM | base64)
        f_question
}

f_question(){
        V_COUNT=$((V_COUNT+1))
        echo '[*] You have 15 seconds to decode this and submit: '$V_ENC
        echo ''
        read -t 15 -p 'Submit Decoded Key: ' V_DEC

        if [ "$V_DEC" = "$V_NUM" ]; then
                echo '[*] Congrats, you did it!'
                echo 'Attempts: '$V_COUNT
                exit 1
        else
                f_encode
        fi
}

f_encode
  • [run the bash script](http://stackoverflow.com/questions/26236126/how-to-run-bash-command-inside-python-script) and print the output – R Nar Nov 20 '15 at 19:56
  • @4ae1e1 It's tricky. You need to input the output of the pipe into the script again :) – hek2mgl Nov 20 '15 at 20:06
  • @user3820758 Catching the output is not a big deal `bash a.sh 2>&1 | sed -r 's/.*: //;/^$/d' | base64decode`. I'm asking myself how to input the result into the running scripts input (except manually).. It should be possible somehow... Btw, the question is interesting +1 – hek2mgl Nov 20 '15 at 20:13
  • Use a coproc maybe. See https://www.gnu.org/software/bash/manual/bash.html#Coprocesses. – 4ae1e1 Nov 20 '15 at 20:13
  • You could use the python [pexpect](https://pexpect.readthedocs.org/en/stable/) module to interact with the bash script. – tdelaney Nov 20 '15 at 20:18
  • @hek2mgl yes I find it interesting as well as you could probably do some interesting fuzzing activities with this in other situations. – user3820758 Nov 20 '15 at 21:59

2 Answers2

1

You can use the coproc keyword in bash 4+ to establish a two-way pipe between the executing shell and a "coprocess". See the linked documentation for more info. (For earlier versions of bash you probably need to set up named pipes yourself. Just find a tutorial of mkfifo and read it.)

For illustrative purposes, here is a dumbed down version of your challenge, which I'll save in a script challenge:

#!/usr/bin/env bash
num=$RANDOM
echo "$num"
read -t 15 -p "Please type the number printed above:" typed_num
[[ $num == $typed_num ]] && echo "Correct." >&2 || echo "Wrong." >&2

It prints a number and asks you to retype it in 15 seconds, and prints Correct or Wrong to stderr based on whether you typed it correctly.

And here is a very basic "solver" using coproc:

#!/usr/bin/env bash
coproc ./challenge
read -r num <&"${COPROC[0]}"  # read number from stdout of ./challenge
echo $num >&"${COPROC[1]}"  # print number to stdin of ./challenge

When you run it:

> ./solver
Correct.

You can see the basic idea here. Since you can read from stdout and print to stdin of ./challenge, you can finish the rest of your work (i.e., automatically decoding) on your own.

4ae1e1
  • 7,228
  • 8
  • 44
  • 77
0

You can simulate a solution in 1 file, using tail.

You would like to see what your challenge and respond functions are doing, so I write the output to stderr for you (using >&2).
The challenge function actually wants the value 2 as input. It all starts with a Smile.

function challenge {
   while read x; do
      if [ "$x" = "2" ]; then
         msg="You cracked my code"
         echo "${msg}"
         echo "${msg}" >&2
         exit 0
      fi
      sleep 1
      newchallenge=${RANDOM}
      echo "${newchallenge}" >&2
      echo "${newchallenge}"
   done
}

function respond {
   while read challenge; do
      echo ${#challenge}
      echo "Answer: ${#challenge} " >&2
   done
}

echo ";) Smile ;)" > loopback
tail -f loopback | challenge | respond >> loopback
Walter A
  • 19,067
  • 2
  • 23
  • 43