-
Notifications
You must be signed in to change notification settings - Fork 0
/
unsquashfs_all.sh
executable file
·171 lines (147 loc) · 3.58 KB
/
unsquashfs_all.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#!/bin/bash
# Script to attempt to extract files from a SquashFS image using all of the available unsquashfs utilities in FMK until one is successful.
#
# Craig Heffner
# 27 August 2011
BINDIR=`dirname $0`
. "$BINDIR/common.inc"
IMG="$1"
DIR="$2"
ROOT="./src"
# should order in ascending version,
# since newer versions may be able to extact older
# and we want *first* supporting version (some exceptions apply)
SUBDIRS="squashfs-2.1-r2 \
squashfs-3.0 \
squashfs-3.0-lzma-damn-small-variant \
others/squashfs-2.0-nb4 \
others/squashfs-2.2-r2-7z \
others/squashfs-3.0-e2100 \
others/squashfs-3.2-r2 \
others/squashfs-3.2-r2-lzma \
others/squashfs-3.2-r2-lzma/squashfs3.2-r2/squashfs-tools \
others/squashfs-3.2-r2-hg612-lzma \
others/squashfs-3.2-r2-wnr1000 \
others/squashfs-3.2-r2-rtn12 \
others/squashfs-3.3 \
others/squashfs-3.3-lzma/squashfs3.3/squashfs-tools \
others/squashfs-3.3-grml-lzma/squashfs3.3/squashfs-tools \
others/squashfs-3.4-cisco \
others/squashfs-3.4-nb4 \
others/squashfs-4.2-official \
others/squashfs-4.2 \
others/squashfs-4.0-lzma \
others/squashfs-4.0-realtek \
others/squashfs-hg55x-bin"
TIMEOUT="60"
MKFS=""
function wait_for_complete()
{
I=0
PNAME="$1"
while [ $I -lt $TIMEOUT ]
do
sleep 1
if [ "$(pgrep $PNAME)" == "" ]
then
break
fi
((I=$I+1))
done
if [ "$I" == "$TIMEOUT" ]
then
kill -9 $(pgrep $PNAME) 2>/dev/null
fi
}
if [ "$IMG" == "" ] || [ "$IMG" == "-h" ]
then
echo "Usage: $0 <squashfs image> [output directory]"
exit 1
fi
if [ "$DIR" == "" ]
then
BDIR="./squashfs-root"
DIR="$BDIR"
I=1
while [ -e "$DIR" ]
do
DIR="$BDIR-$I"
((I=$I+1))
done
fi
IMG=$(readlink -f "$IMG")
DIR=$(readlink -f "$DIR")
# Make sure we're operating out of the FMK directory
cd $(dirname $(readlink -f "$0"))
MAJOR=$(./src/binwalk-1.0/src/bin/binwalk-script -m ./src/binwalk-*/src/binwalk/magic/binwalk -l 1024 "$IMG" | head -4 | tail -1 | sed -e 's/.*version //' | cut -d'.' -f1)
echo -e "Attempting to extract SquashFS $MAJOR.X file system...\n"
for SUBDIR in $SUBDIRS
do
if [ "$(echo $SUBDIR | grep "$MAJOR\.")" == "" ]
then
echo "Skipping $SUBDIR (wrong version)..."
continue
fi
unsquashfs="$ROOT/$SUBDIR/unsquashfs"
mksquashfs="$ROOT/$SUBDIR/mksquashfs"
if [ -e $unsquashfs-lzma ]; then
echo -ne "\nTrying $unsquashfs-lzma... "
$unsquashfs-lzma -dest "$DIR" "$IMG" 2>/dev/null &
#sleep $TIMEOUT && kill $! 1>&2 >/dev/null
wait_for_complete $unsquashfs-lzma
if [ -d "$DIR" ]
then
if [ "$(ls "$DIR")" != "" ]
then
# Most systems will have busybox - make sure it's a non-zero file size
if [ -e "$DIR/bin/sh" ]
then
if [ "$(wc -c "$DIR/bin/sh" | cut -d' ' -f1)" != "0" ]
then
MKFS="$mksquashfs-lzma"
fi
else
MKFS="$mksquashfs-lzma"
fi
fi
if [ "$MKFS" == "" ]
then
rm -rf "$DIR"
fi
fi
fi
if [ "$MKFS" == "" ] && [ -e $unsquashfs ]; then
echo -ne "\nTrying $unsquashfs... "
$unsquashfs -dest "$DIR" "$IMG" 2>/dev/null &
#sleep $TIMEOUT && kill $! 1>&2 >/dev/null
wait_for_complete $unsquashfs
if [ -d "$DIR" ]
then
if [ "$(ls "$DIR")" != "" ]
then
# Most systems will have busybox - make sure it's a non-zero file size
if [ -e "$DIR/bin/sh" ]
then
if [ "$(wc -c "$DIR/bin/sh" | cut -d' ' -f1)" != "0" ]
then
MKFS="$mksquashfs"
fi
else
MKFS="$mksquashfs"
fi
fi
if [ "$MKFS" == "" ]
then
rm -rf "$DIR"
fi
fi
fi
if [ "$MKFS" != "" ]
then
echo "File system sucessfully extracted!"
echo "MKFS=\"$MKFS\""
exit 0
fi
done
echo "File extraction failed!"
exit 1