Skip to content

Instantly share code, notes, and snippets.

@pich4ya
Last active October 6, 2023 19:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pich4ya/69bdef8244df3d542577bcd91cdc7210 to your computer and use it in GitHub Desktop.
Save pich4ya/69bdef8244df3d542577bcd91cdc7210 to your computer and use it in GitHub Desktop.
This script can be used to replace strings.xml values in a target Java file
# @author Pichaya Morimoto (p.morimoto@sth.sh)
# I tried all jadx options for debof, but it did not work.
# This script can be used to replace strings.xml values in a target Java file
# if (!Intrinsics.areEqual(param, context.getString(R.string.m1))) { -> if (!Intrinsics.areEqual(param, "monday")) {
# Prompted and modified with ChatGPT for FlareOn 10's ItsOnFire
# Usage: python apk_getString_R_string.py resources/res/values/strings.xml sources/com/secure/itsonfire/MessageWorker.java
# Usage: python apk_getString_R_string.py resources/res/values/strings.xml sources
import xml.etree.ElementTree as ET
import re
import sys
import os
def parse_xml(xml_file):
tree = ET.parse(xml_file)
root = tree.getroot()
data = {}
for string_elem in root.findall('string'):
name = string_elem.get('name')
value = string_elem.text
data[name] = value
return data
def remove_lines_from_file(java_file):
with open(java_file, 'r') as f:
content = f.readlines()
new_content = [line for line in content if "Intrinsics.checkNotNullExpressionValue" not in line]
with open(java_file, 'w') as f:
f.writelines(new_content)
def replace_java_patterns(java_file, data):
with open(java_file, 'r') as f:
content = f.read()
for key, value in data.items():
pattern = r'context\.getString\(R\.string\.{}\)'
replacement = '"{}"'.format(value)
content = re.sub(pattern.format(key), replacement, content)
pattern = r'getString\(R\.string\.{}\)'
replacement = '"{}"'.format(value)
content = re.sub(pattern.format(key), replacement, content)
with open(java_file, 'w') as f:
f.write(content)
def process_directory(directory, data):
for root, dirs, files in os.walk(directory):
for file in files:
if file.endswith('.java'):
remove_lines_from_file(os.path.join(root, file))
replace_java_patterns(os.path.join(root, file), data)
if __name__ == "__main__":
if len(sys.argv) != 3:
print("Usage: python apk_getString_R_string.py resources/res/values/strings.xml sources")
sys.exit(1)
xml_filename = sys.argv[1]
path_or_directory = sys.argv[2]
data = parse_xml(xml_filename)
if os.path.isdir(path_or_directory):
process_directory(path_or_directory, data)
else:
replace_java_patterns(path_or_directory, data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment