Skip to content

Instantly share code, notes, and snippets.

@eyJhb
Created July 27, 2018 19:37
Show Gist options
  • Save eyJhb/7205f7a882eecd6425b03b11e7291e4b to your computer and use it in GitHub Desktop.
Save eyJhb/7205f7a882eecd6425b03b11e7291e4b to your computer and use it in GitHub Desktop.
Quick script for inserting debug statements in smali code.. Somewhat works!
import os
import re
class debug_inserter(object):
def __init__(self, path_to_files, prefix):
self.path_to_files = path_to_files
self.debug_prefix = prefix
self.debug_statement = """const-string {0}, \"{1} - {2}\"
invoke-static {{0}, {0}}, Landroid/util/Log;->d(Ljava/lang/String;Ljava/lang/String;)I"""
self.re_filename = re.compile("\.smali$")
self.re_method = re.compile("^\.method [a-z]+ ([^ ]+)")
self.re_locals = re.compile("^\s+ \.locals (\d+)")
#self.re_global = re.compile(r"(^\.method [a-z]+ ([^ ]+).*)\s+\.locals (\d)", re.MULTILINE)
self.re_global = re.compile(r"(^\.method ?(public|private|protected)? ?(constructor|static|native)? ([^ \(]+).*)\s+\.locals (\d+)", re.MULTILINE)
def read_files(self):
for root, directories, filenames in os.walk(self.path_to_files):
for filename in filenames:
print("fixing filename - "+filename)
self.fix_file(root+"/"+filename)
def fix_file(self, file_name):
f = open(file_name, "r")
data = f.read()
f.close()
matches = self.re_global.search(data)
if not matches:
return
#print(self.re_global.sub(r"\1 .locals \3+1", data))
f = open(file_name, "w")
f.write(self.re_global.sub(lambda m: test(m, file_name), data))
f.close()
def test(match, file_name):
debug_statement = """const-string v0, \"{1} - {2} - {3}\"
invoke-static {{v0, v0}}, Landroid/util/Log;->d(Ljava/lang/String;Ljava/lang/String;)I"""
method = match.group(1).strip()
name = match.group(4).strip()
local_count = match.group(5).strip()
return method+"\n .locals "+str(int(local_count)+0)+"\n "+debug_statement.format(local_count, "realprefix", file_name, name)
x = debug_inserter("dk.bec.android.mb1.b00019.prod/smali/dk", "debugprefix")
x.read_files()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment