File: droidconfig.py

package info (click to toggle)
droidlysis 3.2.1-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid
  • size: 340 kB
  • sloc: python: 1,816; makefile: 3
file content (83 lines) | stat: -rw-r--r-- 3,467 bytes parent folder | download | duplicates (2)
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
import os
import configparser

# ------------------------- DroidLysis Configuration file -----------------

APKTOOL_JAR = '/usr/share/apktool/apktool-lib.jar'
BAKSMALI_JAR = '/usr/share/java/baksmali.jar'
DEX2JAR_CMD = os.path.join(os.path.expanduser('~/.local/share/droidlysis/dex-tools-2.1-SNAPSHOT'), "d2j-dex2jar.sh")
PROCYON_JAR = '/usr/share/java/procyon-decompiler.jar'
INSTALL_DIR = os.path.dirname(__file__)
SQLALCHEMY = 'sqlite:///droidlysis.db' # https://docs.sqlalchemy.org/en/latest/core/engines.html#database-urls
KEYTOOL = os.path.join( "/usr/bin/keytool" )

# ------------------------- Property configuration files -------------------
SMALI_CONFIGFILE = os.path.join(os.path.join(INSTALL_DIR, './conf/smali.conf'))
WIDE_CONFIGFILE= os.path.join(os.path.join(INSTALL_DIR, './conf/wide.conf'))
ARM_CONFIGFILE =  os.path.join(os.path.join(INSTALL_DIR, './conf/arm.conf'))
KIT_CONFIGFILE =  os.path.join(os.path.join(INSTALL_DIR, './conf/kit.conf'))

# ------------------------- Reading *.conf configuration files -----------

class droidconfig:
    def __init__(self, filename, verbose=False):
        assert filename != None, "Filename is invalid"
        assert os.access(filename, os.R_OK) != False, "File {0} is not readable".format(filename)

        self.filename = filename
        self.verbose = verbose
        self.configparser = configparser.RawConfigParser()

        if self.verbose:
            print( "Reading configuration file: '%s'" % (filename))
        self.configparser.read(filename)

    def get_sections(self):
        return self.configparser.sections()

    def get_pattern(self, section):
        return self.configparser.get(section, 'pattern')

    def get_description(self, section):
        try:
            return self.configparser.get(section, 'description')
        except (configparser.NoSectionError, configparser.NoOptionError):
            pass
        return None

    def get_all_regexp(self):
        # reads the config file and returns a list of all patterns for all sections
        # the patterns are concatenated with a |
        # throws NoSectionError, NoOptionError
        allpatterns=''
        for section in self.configparser.sections():
            if allpatterns == '':
                allpatterns = self.configparser.get(section, 'pattern')
            else:
                allpatterns= self.configparser.get(section, 'pattern') + '|' + allpatterns
        return bytes(allpatterns, 'utf-8')

    def match_properties(self, match, properties):
        '''
        Call this when the recursive search has been done to analyze the results
        and understand which properties have been spotted.

        match: returned by droidutil.recursive_search. This is a dictionary
        of matching lines ordered by matching keyword (pattern)

        properties: dictionary of properties where the key is the property name
        and the value will be False/True if set or not
        
        throws NoSessionError, NoOptionError
        '''
        for section in self.configparser.sections():
            pattern_list = self.configparser.get(section, 'pattern').split('|')
            properties[section] = False
            for pattern in pattern_list:
                if match[pattern]:
                    if self.verbose:
                        print( "Setting properties[%s] = True (matches %s)" % (section, pattern))
                    properties[section] = True
                    break