-
Notifications
You must be signed in to change notification settings - Fork 0
/
barf.sh
executable file
·145 lines (135 loc) · 4.3 KB
/
barf.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#!/bin/bash
#
# wrapper script around barf.py
#
# In doubt, see https://github.com/maride/barf
# Have fun with the script! :)
# setting defaults for the arguments
POSITIVEADDR=""
NEGATIVEADDR=""
WINADDR=""
LOSEADDR=""
STARTADDR=""
ENDADDR=""
BUFFADDR=""
KNOWNPREFIX=""
KNOWNSUFFIX=""
BARFPATH="$(dirname $(realpath $0))/src"
CHUNKSIZE=1
CHARSET=""
PERSISTENT="False"
# show the help and exit
function show_help {
echo "Usage: ./barf.sh <options ...> ./path/to/your/binary"
echo ""
echo " BASIC OPTIONS"
echo " -p | --positive-addr 0x123456 a location to be counted as good hit"
echo " -n | --negative-addr 0x234567 a location to be counted as bad hit"
echo " -w | --win-addr 0x345678 a location reached if your input is correct"
echo " -l | --lose-addr 0x456789 a location reached if your input is incorrect"
echo ""
echo " PERSISTENT MODE OPTIONS"
echo " -x | --persistent enable the experimental (!) persistent mode"
echo " -s | --start-addr 0x56789A a location directly after your input is fed into the target"
echo " -e | --end-addr 0x6789AB a location where the to-be-fuzzed logic is done"
echo " --buff-addr 0x789ABC the location where user input is stored"
echo ""
echo " MISC OPTIONS"
echo " -b | --prefix CTF{ a known prefix, e.g. the prefix of your flag"
echo " -a | --suffix } a known suffix, e.g. the suffix of your flag"
echo " -c | --chunksize 2 amount of characters to try at once (default: 1)"
echo " --charset 23@fl4g! characters to use (default: printable ASCII)"
echo " -h | --help a great and useful help message, you should try it!"
echo ""
echo "See https://github.com/maride/barf for more information and examples!"
exit 1
}
# getopt is kind-of unstable across distributions and versions, so we implement it on our own
# hat-tip to https://stackoverflow.com/questions/192249/how-do-i-parse-command-line-arguments-in-bash
while [[ $# -gt 0 ]]; do
key="$1"
case $key in
-p|--positive-addr)
POSITIVEADDR="$2"
shift; shift
;;
-n|--negative-addr)
NEGATIVEADDR="$2"
shift; shift
;;
-w|--win-addr)
WINADDR="$2"
shift; shift
;;
-l|--lose-addr)
LOSEADDR="$2"
shift; shift
;;
-s|--start-addr)
STARTADDR="$2"
shift; shift
;;
-e|--end-addr)
ENDADDR="$2"
shift; shift
;;
--buff-addr)
BUFFADDR="$2"
shift; shift
;;
-h|--help)
SHOWHELP=1
shift
;;
-b|--prefix)
KNOWNPREFIX="$2"
shift; shift
;;
-a|--suffix)
KNOWNSUFFIX="$2"
shift; shift
;;
-c|--chunksize)
CHUNKSIZE="$2"
shift; shift
;;
--charset)
CHARSET=$(echo -n "$2" | base64)
shift; shift
;;
-x|--persistent)
PERSISTENT="1"
shift
;;
*) # unknown option - we assume it is the target literal
TARGETFILE="$key"
shift
;;
esac
done
# see if the user needs our help
if [ "$SHOWHELP" == 1 ]; then
show_help
fi
# check if we have all arguments we need
if [ "$POSITIVEADDR" == "" ] && [ "$NEGATIVEADDR" == "" ] || [ "$TARGETFILE" == "" ] ; then
# nope, missing some args
echo "Missing -p and -n or a target"
show_help
fi
# check if the arguments are valid
if [ ! "$TARGETFILE" == "" ] && [ ! -e "$TARGETFILE" ]; then
echo "The file $TARGETFILE does not exist."
show_help
fi
# check if the persistent mode can be used
if [[ "$PERSISTENT" == "1" && ("$STARTADDR" == "" || "$ENDADDR" == "" || "$BUFFADDR" == "" ) ]]; then
# missing the end address for persistent mode
echo "You need to specify --start-addr, --end-addr and --buff-addr if you want to use persistent mode."
echo "Set --start-addr to an address before your input reaches the program (e.g. before fgets())"
echo "Set --end-addr to an address after the program has checked if the input is good or not (e.g. somewhere after gets('Yay!') and gets('Nay!'))"
echo "Set --buffer-addr to the address where user input is stored (e.g. the address of b in case of fgets(b, 16, stdin)"
show_help
fi
# ready for take-off
gdb --quiet -nx --eval-command "py barf_positive_addr='$POSITIVEADDR';barf_negative_addr='$NEGATIVEADDR';barf_win_addr='$WINADDR';barf_lose_addr='$LOSEADDR';barf_start_addr='$STARTADDR';barf_end_addr='$ENDADDR';barf_buff_addr='$BUFFADDR';barf_known_prefix='$KNOWNPREFIX';barf_known_suffix='$KNOWNSUFFIX';barf_path='$BARFPATH';barf_chunksize=$CHUNKSIZE;barf_charset_b64='$CHARSET';barf_persistent=$PERSISTENT" --command barf.py $TARGETFILE