Skip to content

Instantly share code, notes, and snippets.

@GHolk
Last active February 7, 2018 06:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GHolk/743691d555962fb9863b5b1355c1883e to your computer and use it in GitHub Desktop.
Save GHolk/743691d555962fb9863b5b1355c1883e to your computer and use it in GitHub Desktop.
help find possible order of unorder base64 fragment
#!/bin/sh
# this script help find possible order of base64 fragment.
#
# like "hello world" is aGVsbG8gd29ybGQ= ,
# it would help find "hello world" from "G8g Vsb d2 9yb GQ= aG"
#
# usage: sh find-base64-fragment-order.sh $known_start $frag1 $frag2 ...
# unknown init base:
# sh find-base64-fragment-order.sh '' G8g Vsb d2 9yb GQ= aG
# know start with h, which is `a` in base64, so first is `aG`
# sh find-base64-fragment-order.sh aG G8g Vsb d2 9yb GQ=
#
# just pick the longest match pattern,
# because wrong base64 code # often cause
# mojibake (like not printable text)
#
# need: base64 awk grep
nth() {
echo $list | awk "{print \$$1}"
}
debase64() {
echo $1 | base64 -d 2>/dev/null
}
all_match() {
for next in $list
do
debase64 "$base$next"
echo
done
}
filter_out() {
match=$1
echo $list | sed 's/ /\n/g' | grep --invert-match "^$match\$"
}
base=$1
shift
list="$*"
while [ -n "$list" ]
do
echo
echo base: $base
echo list: $list
echo possible match:
all_match "$base" $list | nl
echo -n 'which one most possible? '
read n
match_frag=$(nth $n $list)
base="$base$match_frag"
list=$(filter_out $match_frag $list)
done
debase64 $base
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment