Skip to content
This repository has been archived by the owner on Feb 6, 2021. It is now read-only.

Parse annotations out of smali #25

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion smalisca/core/smalisca_analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
import abc


class AnalysisBase(metaclass=abc.ABCMeta):
class AnalysisBase(abc.ABCMeta):
"""Basic analysis class

Provides abstract methods how to interact with the results.
Expand Down
60 changes: 57 additions & 3 deletions smalisca/modules/module_smali_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,41 @@ def parse_file(self, filename):
with codecs.open(filename, 'r', encoding='utf8') as f:
current_class = None
current_method = None
current_annotation = None
current_call_index = 0

# Read line by line
for l in f.readlines():
if '.class' in l:

if '.annotation' in l:
annotation_class = self.is_annotation(l)
if annotation_class:
current_annotation = {
"class": annotation_class,
"value": "",
}

elif '.end annotation' in l:
if current_method != None:
current_method["annotations"].append(current_annotation)
elif current_class != None:
current_class["annotations"].append(current_annotation)
else:
log.error("Annotation does not have an owner: %s" % annotation_class)

current_annotation = None

elif current_annotation != None:
# TODO: Proper parsing of annotations
stripped_line = l.strip()
if stripped_line == "value = {" or stripped_line == "}":
continue

match = re.search("\"(?P<value>.*)\"", stripped_line)
if match:
current_annotation["value"] += match.group('value')

elif '.class' in l:
match_class = self.is_class(l)
if match_class:
current_class = self.extract_class(match_class)
Expand Down Expand Up @@ -229,6 +259,24 @@ def is_class_method(self, line):
else:
return None

def is_annotation(self, line):
"""Check if line contains an annotation definition

Args:
line (str): Text line to be checked

Returns:
bool: True if line contains annotation information, otherwise False

"""
match = re.search("\.annotation\s+(?P<type>[a-z]+)\s+(?P<class>.*)$", line)
if match:
log.debug("\t\tFound annotation: %s" % match.group('class'))
return match.group('class')
else:
log.debug("\t\tNot annotation: %s" % line)
return None

def is_method_call(self, line):
"""Check [MaÔif the line contains a method call (invoke-*)

Expand Down Expand Up @@ -281,7 +329,10 @@ def extract_class(self, data):
'const-strings': [],

# Methods
'methods': []
'methods': [],

# Annotations
'annotations': [],
}

return c
Expand Down Expand Up @@ -385,7 +436,10 @@ def extract_class_method(self, data):
'type': " ".join(method_info[:-1]),

# Calls
'calls': []
'calls': [],

# Annotations
'annotations': [],
}

return m
Expand Down