-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathrun.sh
executable file
·107 lines (89 loc) · 2.96 KB
/
run.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
#!/usr/bin/env bash
#
# snet-extractor
# -----------------------------------------
#
# Anestis Bechtsoudis <[email protected]>
# Copyright 2017 by CENSUS S.A. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
set -e # fail on unhandled error
set -u # fail on undefined variable
#set -x # debug
readonly SNET_FLAGS="https://www.gstatic.com/android/snet/snet.flags"
readonly TMP_WORK_DIR=$(mktemp -d /tmp/snet-extractor.XXXXXX) || exit 1
readonly TOOL_ROOT="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
# JSON beautifier (https://stedolan.github.io/jq/)
readonly JBEA_MAC="$TOOL_ROOT/bin/jq-osx-amd64"
readonly JBEA_LINUX="$TOOL_ROOT/bin/jq-linux64"
readonly kMetadataFile="metadata_flags.txt"
readonly kPayloadFile="payload.snet"
readonly FLAGS_FILE="snet_flags.json"
readonly JAR_FILE="snet.jar"
abort() {
# If debug enabled, keep work directory for further investigation
if [[ "$-" == *x* ]]; then
echo "[!] Workspace available at '$TMP_WORK_DIR' - delete manually when done"
else
rm -rf "$TMP_WORK_DIR"
fi
exit "$1"
}
isMacOS() {
if [[ "$(uname -s)" == "Darwin" ]]; then
return 0
fi
return 1
}
# Main
trap "abort 1" SIGHUP SIGINT SIGTERM
if isMacOS; then
JBEA="$JBEA_MAC"
else
JBEA="$JBEA_LINUX"
fi
echo "[*] Downloading SNET flags file"
curl "$SNET_FLAGS" --output "$TMP_WORK_DIR/snet.flags" || {
echo "[-] Failed to download snet flags data"
abort 1
}
gcc -Wall -Wextra -Werror -o "$TOOL_ROOT/snet-bin-parser" "$TOOL_ROOT/snet-bin-parser.c" || {
echo "[-] Failed to compile snet binary parser tool"
abort 1
}
"$TOOL_ROOT/snet-bin-parser" "$TMP_WORK_DIR/snet.flags" "$TMP_WORK_DIR" || {
echo "[-] Failed to parse snet.flags"
abort 1
}
version=$(grep "^VERSION:" "$TMP_WORK_DIR/$kMetadataFile" | cut -d ":" -f2)
echo "[*] Detected snet version '$version'"
outputDir="snet-$version"
mkdir -p "$outputDir"
cp "$TMP_WORK_DIR/$kPayloadFile" "$outputDir/$FLAGS_FILE"
cp "$TMP_WORK_DIR/$kMetadataFile" "$outputDir"
cp "$TMP_WORK_DIR/snet.flags" "$outputDir"
downloadUrl=$($JBEA .url "$outputDir/$FLAGS_FILE" | tr -d '"')
fileName="${downloadUrl##*/}"
echo "[*] Downloading SNET Jar file"
curl "$downloadUrl" --output "$outputDir/$fileName" || {
echo "[-] Failed to download snet jar data"
abort 1
}
"$TOOL_ROOT/snet-bin-parser" "$outputDir/$fileName" "$TMP_WORK_DIR" || {
echo "[-] Failed to parse $fileName"
abort 1
}
cp "$TMP_WORK_DIR/$kPayloadFile" "$outputDir/$JAR_FILE"
echo "[*] All files successfully extract at '$outputDir'"
abort 0