forked from LeanVel/iInject
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiInject.sh
executable file
·246 lines (192 loc) · 6.37 KB
/
iInject.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
#!/bin/bash
#TODO
# - Add *proper* support for online/offline dylib provision
# - Add support for optional vervosity
NORMAL=$(tput sgr0)
RED=$(tput setaf 1)
GREEN=$(tput setaf 2)
YELLOW=$(tput setaf 3)
#Switches
#TODO: use proper options to change the swithces from the command line
removePlugIns=false
useLocalCopy=true
cleanUpEnabled=true
logEnabled=false
workDirectory=/tmp/iInject
#Clean up function definition
cleanUp () {
if [ "$cleanUpEnabled" = true ]
then
printf "${NORMAL}%s${NORMAL}\n" "Cleaning up work directory "$workDirectory" "
echo "rm -rf "$workDirectory""
rm -rf "$workDirectory"
fi
}
checkProvisioning (){
#Get iDevice UUID
iDevice=$(idevice_id -l)
#Check that iDevice was connected
if [ "$?" -ne "0" ]
then
printf "${RED}%s${NORMAL}\n" "Error while checking provisioning, the target iDevice needs to be connected!"
exit 1
fi
#Check that provision profile, private key and certificate are already installed
if [ -f ~/.isign/isign.mobileprovision ] && [ -f ~/.isign/certificate.pem ] && [ -f ~/.isign/key.pem ]
then
grep -i "$iDevice" ~/.isign/isign.mobileprovision >> "$debugDir" 2>&1
#Check that current provision profile include target iDevice
if [ "$?" -eq "0" ]
then
#No need for new provision profile
printf "${GREEN}%s${NORMAL}\n" "Provisioning profile correctly installed"
return 0
fi
fi
printf "${RED}%s${YELLOW}\n%s\n%s${NORMAL}\n" "A new provision profile is needed, please use one of the provided helper scripts:" "For paid account -> genProvisioningProfileDev <AppleID> <Password> <Device UUID>" "For Free account -> genProvisioningProfileFree <AppleID> <Password> <Device UUID> "
}
#Main script start
#Verify arguments
if [ $# -lt "2" ]
then
printf "${RED}%s${NORMAL}\n" "Usage: "$0" <IPA File> <Dylib File>"
exit 1
fi
ipaFile="$1"
dylibFile="$2"
dylibName=$(basename "$dylibFile")
ipaFilename=$(basename -s .ipa "$ipaFile")
ipaDirname=$(dirname "$ipaFile")
#Set up Logging
if [ "$logEnabled" = true ]
then
debugDir=$(pwd)/"iInject.log"
printf "${GREEN}%s${NORMAL}\n" "Log is going to be saved in ""$debugDir"
else
debugDir="/dev/null"
fi
#Start log
echo `date`" Embedding started" >> "$debugDir" 2>&1
#Verify that provisioning is installed
checkProvisioning
#Making work directory
mkdir "$workDirectory" >> "$debugDir" 2>&1
#Uncompressing IPA file
printf "${NORMAL}%s${NORMAL}\n" "Uncompressing "$ipaFilename" in "$workDirectory" "
unzip $ipaFile -d "$workDirectory"/"$ipaFilename" >> "$debugDir" 2>&1
if [ "$?" -eq "0" ]
then
printf "${GREEN}%s${NORMAL}\n" "File "$ipaFilename" uncompressed correctly in "$workDirectory" "
else
printf "${RED}%s${NORMAL}\n" "Error while uncompressing "$ipaFilename" in "$workDirectory" "
cleanUp
exit 1
fi
workDirectory="$workDirectory"/"$ipaFilename"
if [ "$removePlugIns" = true ]
then
#Checking for PlugIns directory
printf "${NORMAL}%s${NORMAL}\n" "Checking for PlugIns directory"
if [ -d "$workDirectory"/Payload/*/PlugIns ]
then
printf "${NORMAL}%s${NORMAL}\n" "PlugIns directory found, it will be deleted"
echo "rm -rf "$workDirectory"/Payload/*/PlugIns"
rm -rf "$workDirectory"/Payload/*/PlugIns
if [ "$?" -eq "0" ]
then
printf "${GREEN}%s${NORMAL}\n" " "$workDirectory"/Payload/*/PlugIns deleted sucessfully"
else
printf "${RED}%s${NORMAL}\n" "Error while deleting "$workDirectory"/Payload/*/PlugIns"
cleanUp
exit 1
fi
fi
fi
#Getting Binary to be patched
binaryName=`file "$workDirectory"/Payload/*/* | grep -i mach | cut -d ":" -f1 | grep -vi dylib`
numberOfBinaries=`echo "$binaryName" | tr -s "\n" "|" | awk -F'|' '{print NF-1}'`
if [ $numberOfBinaries -gt 1 ]
then
printf "${RED}%s${NORMAL}\n" "To many binaries files in the directory "$workDirectory"/Payload/*/*"
echo "$binaryName"
cleanUp
exit 1
fi
#Patch Binary
printf "${NORMAL}%s${NORMAL}\n" "Patching Binary "$binaryName" "
insert_dylib --strip-codesig --inplace @executable_path/"$dylibName" "$binaryName" >> "$debugDir" 2>&1
if [ "$?" -eq "0" ]
then
printf "${GREEN}%s${NORMAL}\n" "Binary "$binaryName" patched sucessfully"
else
printf "${RED}%s${NORMAL}\n" "Error while patching binary "$binaryName""
cleanUp
exit 1
fi
# Gadget obtention
binaryDirectory=$(dirname "$binaryName")
if [ "$useLocalCopy" = false ]
then
#Download Fridagadget in the right directory
printf "${NORMAL}%s${NORMAL}\n" "Downloading Fridagadget in $binaryDirectory/ "
curl https://build.frida.re/frida/ios/lib/FridaGadget.dylib --output "$binaryDirectory"/FridaGadget.dylib
if [ "$?" -eq "0" ]
then
printf "${GREEN}%s${NORMAL}\n" " Gadget downloaded sucessfully"
else
printf "${RED}%s${NORMAL}\n" "Error while downloading Gadget"
cleanUp
exit 1
fi
else
# Use local copy of the Gadget
printf "${NORMAL}%s${NORMAL}\n" "Coping local gadget $dylibFile to $binaryDirectory/ "
cp "$dylibFile" "$binaryDirectory"/ >> "$debugDir" 2>&1
if [ "$?" -eq "0" ]
then
printf "${GREEN}%s${NORMAL}\n" "Gadget copied sucessfully"
else
printf "${RED}%s${NORMAL}\n" "Error while coping Gadget"
cleanUp
exit 1
fi
fi
#Adjusting direcorties before ziping
currPath=`pwd`
cd "$workDirectory"
#Creating new IPA
printf "${NORMAL}%s${NORMAL}\n" "Creating new IPA file in "$workDirectory"/"$ipaFilename"-patched.ipa"
zip -r "$ipaFilename"-patched.ipa Payload/ >> "$debugDir" 2>&1
if [ "$?" -eq "0" ]
then
printf "${GREEN}%s${NORMAL}\n" ""$workDirectory"/"$ipaFilename"-patched.ipa created sucessfully"
else
printf "${RED}%s${NORMAL}\n" "Error while creating "$workDirectory"/"$ipaFilename"-patched.ipa"
cleanUp
exit 1
fi
#Signing new IPA
printf "${NORMAL}%s${NORMAL}\n" "Signing IPA file "$workDirectory"/"$ipaFilename"-patched.ipa"
isign -v -o "$ipaFilename"-patched-isigned.ipa "$ipaFilename"-patched.ipa >> "$debugDir" 2>&1
if [ "$?" -eq "0" ]
then
printf "${GREEN}%s${NORMAL}\n" ""$workDirectory"/"$ipaFilename"-patched-isigned.ipa created sucessfully"
else
printf "${RED}%s${NORMAL}\n" "Error while signing "$workDirectory"/"$ipaFilename"-patched.ipa"
cleanUp
exit 1
fi
#Installing signed IPA
printf "${NORMAL}%s${NORMAL}\n" "Installing IPA file "$ipaFilename"-patched-isigned.ipa"
ideviceinstaller -i "$ipaFilename"-patched-isigned.ipa | tee -a "$debugDir"
if [ "$?" -eq "0" ]
then
printf "${GREEN}%s${NORMAL}\n" ""$ipaFilename"-patched-isigned.ipa installed sucessfully"
else
printf "${RED}%s${NORMAL}\n" "Error while installing "$ipaFilename"-patched-isigned.ipa"
cleanUp
exit 1
fi
cd "$currPath"
cleanUp
exit 0