| 1 |
/* |
| 2 |
* Copyright (C) 2010 Ryszard Wiśniewski <[email protected]> |
| 3 |
* Copyright (C) 2010 Connor Tumbleson <[email protected]> |
| 4 |
* |
| 5 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 |
* you may not use this file except in compliance with the License. |
| 7 |
* You may obtain a copy of the License at |
| 8 |
* |
| 9 |
* https://www.apache.org/licenses/LICENSE-2.0 |
| 10 |
* |
| 11 |
* Unless required by applicable law or agreed to in writing, software |
| 12 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 13 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 |
* See the License for the specific language governing permissions and |
| 15 |
* limitations under the License. |
| 16 |
*/ |
| 17 |
package brut.androlib.res.xml; |
| 18 |
|
| 19 |
import brut.androlib.AndrolibException; |
| 20 |
import org.w3c.dom.*; |
| 21 |
import org.xml.sax.SAXException; |
| 22 |
|
| 23 |
import javax.xml.parsers.DocumentBuilder; |
| 24 |
import javax.xml.parsers.DocumentBuilderFactory; |
| 25 |
import javax.xml.parsers.ParserConfigurationException; |
| 26 |
import javax.xml.transform.Transformer; |
| 27 |
import javax.xml.transform.TransformerException; |
| 28 |
import javax.xml.transform.TransformerFactory; |
| 29 |
import javax.xml.transform.dom.DOMSource; |
| 30 |
import javax.xml.transform.stream.StreamResult; |
| 31 |
import javax.xml.xpath.*; |
| 32 |
import java.io.File; |
| 33 |
import java.io.FileInputStream; |
| 34 |
import java.io.IOException; |
| 35 |
import java.util.logging.Logger; |
| 36 |
|
| 37 |
public final class ResXmlPatcher { |
| 38 |
|
| 39 |
/** |
| 40 |
* Removes "debug" tag from file |
| 41 |
* |
| 42 |
* @param file AndroidManifest file |
| 43 |
* @throws AndrolibException Error reading Manifest file |
| 44 |
*/ |
| 45 |
public static void removeApplicationDebugTag(File file) throws AndrolibException { |
| 46 |
if (file.exists()) { |
| 47 |
try { |
| 48 |
Document doc = loadDocument(file); |
| 49 |
Node application = doc.getElementsByTagName("application").item(0); |
| 50 |
|
| 51 |
// load attr |
| 52 |
NamedNodeMap attr = application.getAttributes(); |
| 53 |
Node debugAttr = attr.getNamedItem("android:debuggable"); |
| 54 |
|
| 55 |
// remove application:debuggable |
| 56 |
if (debugAttr != null) { |
| 57 |
attr.removeNamedItem("android:debuggable"); |
| 58 |
} |
| 59 |
|
| 60 |
saveDocument(file, doc); |
| 61 |
|
| 62 |
} catch (SAXException | ParserConfigurationException | IOException | TransformerException ignored) { |
| 63 |
} |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* Sets "debug" tag in the file to true |
| 69 |
* |
| 70 |
* @param file AndroidManifest file |
| 71 |
*/ |
| 72 |
public static void setApplicationDebugTagTrue(File file) { |
| 73 |
if (file.exists()) { |
| 74 |
try { |
| 75 |
Document doc = loadDocument(file); |
| 76 |
Node application = doc.getElementsByTagName("application").item(0); |
| 77 |
|
| 78 |
// load attr |
| 79 |
NamedNodeMap attr = application.getAttributes(); |
| 80 |
Node debugAttr = attr.getNamedItem("android:debuggable"); |
| 81 |
|
| 82 |
if (debugAttr == null) { |
| 83 |
debugAttr = doc.createAttribute("android:debuggable"); |
| 84 |
attr.setNamedItem(debugAttr); |
| 85 |
} |
| 86 |
|
| 87 |
// set application:debuggable to 'true |
| 88 |
debugAttr.setNodeValue("true"); |
| 89 |
|
| 90 |
saveDocument(file, doc); |
| 91 |
|
| 92 |
} catch (SAXException | ParserConfigurationException | IOException | TransformerException ignored) { |
| 93 |
} |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Sets the value of the network security config in the AndroidManifest file |
| 99 |
* |
| 100 |
* @param file AndroidManifest file |
| 101 |
*/ |
| 102 |
public static void setNetworkSecurityConfig(File file) { |
| 103 |
if (file.exists()) { |
| 104 |
try { |
| 105 |
Document doc = loadDocument(file); |
| 106 |
Node application = doc.getElementsByTagName("application").item(0); |
| 107 |
|
| 108 |
// load attr |
| 109 |
NamedNodeMap attr = application.getAttributes(); |
| 110 |
Node netSecConfAttr = attr.getNamedItem("android:networkSecurityConfig"); |
| 111 |
|
| 112 |
if (netSecConfAttr == null) { |
| 113 |
// there is not an already existing network security configuration |
| 114 |
netSecConfAttr = doc.createAttribute("android:networkSecurityConfig"); |
| 115 |
attr.setNamedItem(netSecConfAttr); |
| 116 |
} |
| 117 |
|
| 118 |
// whether it already existed or it was created now set it to the proper value |
| 119 |
netSecConfAttr.setNodeValue("@xml/network_security_config"); |
| 120 |
|
| 121 |
saveDocument(file, doc); |
| 122 |
|
| 123 |
} catch (SAXException | ParserConfigurationException | IOException | TransformerException ignored) { |
| 124 |
} |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
/** |
| 129 |
* Creates a modified network security config file that is more permissive |
| 130 |
* |
| 131 |
* @param file network security config file |
| 132 |
* @throws TransformerException XML file could not be edited |
| 133 |
* @throws IOException XML file could not be located |
| 134 |
* @throws SAXException XML file could not be read |
| 135 |
* @throws ParserConfigurationException XML nodes could be written |
| 136 |
*/ |
| 137 |
public static void modNetworkSecurityConfig(File file) |
| 138 |
throws ParserConfigurationException, TransformerException, IOException, SAXException { |
| 139 |
|
| 140 |
DocumentBuilderFactory documentFactory = DocumentBuilderFactory.newInstance(); |
| 141 |
DocumentBuilder documentBuilder = documentFactory.newDocumentBuilder(); |
| 142 |
Document document = documentBuilder.newDocument(); |
| 143 |
|
| 144 |
Element root = document.createElement("network-security-config"); |
| 145 |
document.appendChild(root); |
| 146 |
Element baseConfig = document.createElement("base-config"); |
| 147 |
root.appendChild(baseConfig); |
| 148 |
Element trustAnchors = document.createElement("trust-anchors"); |
| 149 |
baseConfig.appendChild(trustAnchors); |
| 150 |
|
| 151 |
Element certSystem = document.createElement("certificates"); |
| 152 |
Attr attrSystem = document.createAttribute("src"); |
| 153 |
attrSystem.setValue("system"); |
| 154 |
certSystem.setAttributeNode(attrSystem); |
| 155 |
trustAnchors.appendChild(certSystem); |
| 156 |
|
| 157 |
Element certUser = document.createElement("certificates"); |
| 158 |
Attr attrUser = document.createAttribute("src"); |
| 159 |
attrUser.setValue("user"); |
| 160 |
certUser.setAttributeNode(attrUser); |
| 161 |
trustAnchors.appendChild(certUser); |
| 162 |
|
| 163 |
saveDocument(file, document); |
| 164 |
} |
| 165 |
|
| 166 |
/** |
| 167 |
* Any @string reference in a provider value in AndroidManifest.xml will break on |
| 168 |
* build, thus preventing the application from installing. This is from a bug/error |
| 169 |
* in AOSP where public resources cannot be part of an authorities attribute within |
| 170 |
* a provider tag. |
| 171 |
* |
| 172 |
* This finds any reference and replaces it with the literal value found in the |
| 173 |
* res/values/strings.xml file. |
| 174 |
* |
| 175 |
* @param file File for AndroidManifest.xml |
| 176 |
*/ |
| 177 |
public static void fixingPublicAttrsInProviderAttributes(File file) { |
| 178 |
boolean saved = false; |
| 179 |
if (file.exists()) { |
| 180 |
try { |
| 181 |
Document doc = loadDocument(file); |
| 182 |
XPath xPath = XPathFactory.newInstance().newXPath(); |
| 183 |
XPathExpression expression = xPath.compile("/manifest/application/provider"); |
| 184 |
|
| 185 |
Object result = expression.evaluate(doc, XPathConstants.NODESET); |
| 186 |
NodeList nodes = (NodeList) result; |
| 187 |
|
| 188 |
for (int i = 0; i < nodes.getLength(); i++) { |
| 189 |
Node node = nodes.item(i); |
| 190 |
NamedNodeMap attrs = node.getAttributes(); |
| 191 |
Node provider = attrs.getNamedItem("android:authorities"); |
| 192 |
|
| 193 |
if (provider != null) { |
| 194 |
saved = isSaved(file, saved, provider); |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
// android:scheme |
| 199 |
xPath = XPathFactory.newInstance().newXPath(); |
| 200 |
expression = xPath.compile("/manifest/application/activity/intent-filter/data"); |
| 201 |
|
| 202 |
result = expression.evaluate(doc, XPathConstants.NODESET); |
| 203 |
nodes = (NodeList) result; |
| 204 |
|
| 205 |
for (int i = 0; i < nodes.getLength(); i++) { |
| 206 |
Node node = nodes.item(i); |
| 207 |
NamedNodeMap attrs = node.getAttributes(); |
| 208 |
Node provider = attrs.getNamedItem("android:scheme"); |
| 209 |
|
| 210 |
if (provider != null) { |
| 211 |
saved = isSaved(file, saved, provider); |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
if (saved) { |
| 216 |
saveDocument(file, doc); |
| 217 |
} |
| 218 |
|
| 219 |
} catch (SAXException | ParserConfigurationException | IOException | |
| 220 |
XPathExpressionException | TransformerException ignored) { |
| 221 |
} |
| 222 |
} |
| 223 |
} |
| 224 |
|
| 225 |
/** |
| 226 |
* Checks if the replacement was properly made to a node. |
| 227 |
* |
| 228 |
* @param file File we are searching for value |
| 229 |
* @param saved boolean on whether we need to save |
| 230 |
* @param provider Node we are attempting to replace |
| 231 |
* @return boolean |
| 232 |
*/ |
| 233 |
private static boolean isSaved(File file, boolean saved, Node provider) { |
| 234 |
String reference = provider.getNodeValue(); |
| 235 |
String replacement = pullValueFromStrings(file.getParentFile(), reference); |
| 236 |
|
| 237 |
if (replacement != null) { |
| 238 |
provider.setNodeValue(replacement); |
| 239 |
saved = true; |
| 240 |
} |
| 241 |
return saved; |
| 242 |
} |
| 243 |
|
| 244 |
/** |
| 245 |
* Finds key in strings.xml file and returns text value |
| 246 |
* |
| 247 |
* @param directory Root directory of apk |
| 248 |
* @param key String reference (ie @string/foo) |
| 249 |
* @return String|null |
| 250 |
*/ |
| 251 |
public static String pullValueFromStrings(File directory, String key) { |
| 252 |
if (key == null || ! key.contains("@")) { |
| 253 |
return null; |
| 254 |
} |
| 255 |
|
| 256 |
File file = new File(directory, "/res/values/strings.xml"); |
| 257 |
key = key.replace("@string/", ""); |
| 258 |
|
| 259 |
if (file.exists()) { |
| 260 |
try { |
| 261 |
Document doc = loadDocument(file); |
| 262 |
XPath xPath = XPathFactory.newInstance().newXPath(); |
| 263 |
XPathExpression expression = xPath.compile("/resources/string[@name=" + '"' + key + "\"]/text()"); |
| 264 |
|
| 265 |
Object result = expression.evaluate(doc, XPathConstants.STRING); |
| 266 |
|
| 267 |
if (result != null) { |
| 268 |
return (String) result; |
| 269 |
} |
| 270 |
|
| 271 |
} catch (SAXException | ParserConfigurationException | IOException | XPathExpressionException ignored) { |
| 272 |
} |
| 273 |
} |
| 274 |
|
| 275 |
return null; |
| 276 |
} |
| 277 |
|
| 278 |
/** |
| 279 |
* Finds key in integers.xml file and returns text value |
| 280 |
* |
| 281 |
* @param directory Root directory of apk |
| 282 |
* @param key Integer reference (ie @integer/foo) |
| 283 |
* @return String|null |
| 284 |
*/ |
| 285 |
public static String pullValueFromIntegers(File directory, String key) { |
| 286 |
if (key == null || ! key.contains("@")) { |
| 287 |
return null; |
| 288 |
} |
| 289 |
|
| 290 |
File file = new File(directory, "/res/values/integers.xml"); |
| 291 |
key = key.replace("@integer/", ""); |
| 292 |
|
| 293 |
if (file.exists()) { |
| 294 |
try { |
| 295 |
Document doc = loadDocument(file); |
| 296 |
XPath xPath = XPathFactory.newInstance().newXPath(); |
| 297 |
XPathExpression expression = xPath.compile("/resources/integer[@name=" + '"' + key + "\"]/text()"); |
| 298 |
|
| 299 |
Object result = expression.evaluate(doc, XPathConstants.STRING); |
| 300 |
|
| 301 |
if (result != null) { |
| 302 |
return (String) result; |
| 303 |
} |
| 304 |
|
| 305 |
} catch (SAXException | ParserConfigurationException | IOException | XPathExpressionException ignored) { |
| 306 |
} |
| 307 |
} |
| 308 |
|
| 309 |
return null; |
| 310 |
} |
| 311 |
|
| 312 |
/** |
| 313 |
* Removes attributes like "versionCode" and "versionName" from file. |
| 314 |
* |
| 315 |
* @param file File representing AndroidManifest.xml |
| 316 |
*/ |
| 317 |
public static void removeManifestVersions(File file) { |
| 318 |
if (file.exists()) { |
| 319 |
try { |
| 320 |
Document doc = loadDocument(file); |
| 321 |
Node manifest = doc.getFirstChild(); |
| 322 |
NamedNodeMap attr = manifest.getAttributes(); |
| 323 |
Node vCode = attr.getNamedItem("android:versionCode"); |
| 324 |
Node vName = attr.getNamedItem("android:versionName"); |
| 325 |
|
| 326 |
if (vCode != null) { |
| 327 |
attr.removeNamedItem("android:versionCode"); |
| 328 |
} |
| 329 |
if (vName != null) { |
| 330 |
attr.removeNamedItem("android:versionName"); |
| 331 |
} |
| 332 |
saveDocument(file, doc); |
| 333 |
|
| 334 |
} catch (SAXException | ParserConfigurationException | IOException | TransformerException ignored) { |
| 335 |
} |
| 336 |
} |
| 337 |
} |
| 338 |
|
| 339 |
/** |
| 340 |
* Replaces package value with passed packageOriginal string |
| 341 |
* |
| 342 |
* @param file File for AndroidManifest.xml |
| 343 |
* @param packageOriginal Package name to replace |
| 344 |
*/ |
| 345 |
public static void renameManifestPackage(File file, String packageOriginal) { |
| 346 |
try { |
| 347 |
Document doc = loadDocument(file); |
| 348 |
|
| 349 |
// Get the manifest line |
| 350 |
Node manifest = doc.getFirstChild(); |
| 351 |
|
| 352 |
// update package attribute |
| 353 |
NamedNodeMap attr = manifest.getAttributes(); |
| 354 |
Node nodeAttr = attr.getNamedItem("package"); |
| 355 |
nodeAttr.setNodeValue(packageOriginal); |
| 356 |
saveDocument(file, doc); |
| 357 |
|
| 358 |
} catch (SAXException | ParserConfigurationException | IOException | TransformerException ignored) { |
| 359 |
} |
| 360 |
} |
| 361 |
|
| 362 |
/** |
| 363 |
* |
| 364 |
* @param file File to load into Document |
| 365 |
* @return Document |
| 366 |
* @throws IOException |
| 367 |
* @throws SAXException |
| 368 |
* @throws ParserConfigurationException |
| 369 |
*/ |
| 370 |
private static Document loadDocument(File file) |
| 371 |
throws IOException, SAXException, ParserConfigurationException { |
| 372 |
|
| 373 |
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); |
| 374 |
docFactory.setFeature(FEATURE_DISABLE_DOCTYPE_DECL, true); |
| 375 |
docFactory.setFeature(FEATURE_LOAD_DTD, false); |
| 376 |
|
| 377 |
try { |
| 378 |
docFactory.setAttribute(ACCESS_EXTERNAL_DTD, " "); |
| 379 |
docFactory.setAttribute(ACCESS_EXTERNAL_SCHEMA, " "); |
| 380 |
} catch (IllegalArgumentException ex) { |
| 381 |
LOGGER.warning("JAXP 1.5 Support is required to validate XML"); |
| 382 |
} |
| 383 |
|
| 384 |
DocumentBuilder docBuilder = docFactory.newDocumentBuilder(); |
| 385 |
// Not using the parse(File) method on purpose, so that we can control when |
| 386 |
// to close it. Somehow parse(File) does not seem to close the file in all cases. |
| 387 |
try (FileInputStream inputStream = new FileInputStream(file)) { |
| 388 |
return docBuilder.parse(inputStream); |
| 389 |
} |
| 390 |
} |
| 391 |
|
| 392 |
/** |
| 393 |
* |
| 394 |
* @param file File to save Document to (ie AndroidManifest.xml) |
| 395 |
* @param doc Document being saved |
| 396 |
* @throws IOException |
| 397 |
* @throws SAXException |
| 398 |
* @throws ParserConfigurationException |
| 399 |
* @throws TransformerException |
| 400 |
*/ |
| 401 |
private static void saveDocument(File file, Document doc) |
| 402 |
throws IOException, SAXException, ParserConfigurationException, TransformerException { |
| 403 |
|
| 404 |
TransformerFactory transformerFactory = TransformerFactory.newInstance(); |
| 405 |
Transformer transformer = transformerFactory.newTransformer(); |
| 406 |
DOMSource source = new DOMSource(doc); |
| 407 |
StreamResult result = new StreamResult(file); |
| 408 |
transformer.transform(source, result); |
| 409 |
} |
| 410 |
|
| 411 |
private static final String ACCESS_EXTERNAL_DTD = "http://javax.xml.XMLConstants/property/accessExternalDTD"; |
| 412 |
private static final String ACCESS_EXTERNAL_SCHEMA = "http://javax.xml.XMLConstants/property/accessExternalSchema"; |
| 413 |
private static final String FEATURE_LOAD_DTD = "http://apache.org/xml/features/nonvalidating/load-external-dtd"; |
| 414 |
private static final String FEATURE_DISABLE_DOCTYPE_DECL = "http://apache.org/xml/features/disallow-doctype-decl"; |
| 415 |
|
| 416 |
private static final Logger LOGGER = Logger.getLogger(ResXmlPatcher.class.getName()); |
| 417 |
} |