Skip to content

Commit

Permalink
Wrap all file accesses in a with statement to ensure they are closed …
Browse files Browse the repository at this point in the history
…properly
  • Loading branch information
Robert Grosse committed Oct 29, 2014
1 parent 4eb3130 commit 4c136ed
Show file tree
Hide file tree
Showing 50 changed files with 336 additions and 338 deletions.
11 changes: 6 additions & 5 deletions ag-st/ag.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
from androguard.core.analysis import ganalysis
from androguard.decompiler import decompiler
from androguard.core import androconf
from androguard.util import read

AG_DEX_VIEW = {}
AG_APK_VIEW = {}
Expand Down Expand Up @@ -527,19 +528,19 @@ def run(self):

ret = androconf.is_android(filename)
if ret == "APK":
at = AnalyseAPKThread(self.window.new_file(), filename, open(filename, "rb").read())
at = AnalyseAPKThread(self.window.new_file(), filename, read(filename))
at.run()
elif ret == "DEX" or ret == "DEY":
at = AnalyseDexThread(self.window.new_file(), filename, open(filename, "rb").read())
at = AnalyseDexThread(self.window.new_file(), filename, read(filename))
at.run()
elif ret == "AXML":
at = AnalyseAXMLSimpleThread(self.window.new_file(), filename, open(filename, "rb").read())
at = AnalyseAXMLSimpleThread(self.window.new_file(), filename, read(filename))
at.run()
elif ret == "ARSC":
at = AnalyseARSCThread(self.window.new_file(), filename, open(filename, "rb").read())
at = AnalyseARSCThread(self.window.new_file(), filename, read(filename))
at.run()

#thread = AnalyseThread(self.window.new_file(), filename, open(filename, "rb").read())
#thread = AnalyseThread(self.window.new_file(), filename, read(filename))
#thread.start()
#ThreadProgress(thread,
# "Analysing app ...",
Expand Down
3 changes: 2 additions & 1 deletion androarsc.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

from androguard.core import androconf
from androguard.core.bytecodes import apk
from androguard.util import read


option_0 = { 'name' : ('-i', '--input'), 'help' : 'filename input (APK or android resources(arsc))', 'nargs' : 1 }
Expand All @@ -46,7 +47,7 @@ def main(options, arguments):
a = apk.APK(options.input)
arscobj = a.get_android_resources()
elif ret_type == "ARSC":
arscobj = apk.ARSCParser(open(options.input, "rb").read())
arscobj = apk.ARSCParser(read(options.input))
else:
print "Unknown file type"
return
Expand Down
3 changes: 2 additions & 1 deletion androaxml.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

from androguard.core import androconf
from androguard.core.bytecodes import apk
from androguard.util import read


option_0 = { 'name' : ('-i', '--input'), 'help' : 'filename input (APK or android\'s binary xml)', 'nargs' : 1 }
Expand All @@ -42,7 +43,7 @@ def main(options, arguments) :
a = apk.APK(options.input)
buff = a.get_android_manifest_xml().toprettyxml(encoding="utf-8")
elif ".xml" in options.input:
ap = apk.AXMLPrinter(open(options.input, "rb").read())
ap = apk.AXMLPrinter(read(options.input))
buff = minidom.parseString(ap.get_buff()).toprettyxml(encoding="utf-8")
else:
print "Unknown file type"
Expand Down
3 changes: 2 additions & 1 deletion androcsign.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import sys

from androguard.core import androconf
from androguard.util import read

sys.path.append("./elsim/")
from elsim.elsign import dalvik_elsign
Expand All @@ -39,7 +40,7 @@
def main(options, arguments) :
s = dalvik_elsign.CSignature(pcs=dalvik_elsign.PublicCSignature)
if options.input != None :
ret = s.add_file( open( options.input, "rb" ).read() )
ret = s.add_file( read( options.input) )
if ret != None and options.output != None :
s.add_indb( ret, options.output )

Expand Down
12 changes: 4 additions & 8 deletions androdd.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,18 +186,14 @@ def export_apps_to_format(filename, a, output, methods_filter=None, jar=None, de
create_directory(filename_class, output)

current_filename_class = output_name + current_filename_class + ".java"
fd = open(current_filename_class, "w")
fd.write(current_class.get_source())
fd.close()

with open(current_filename_class, "w") as fd:
fd.write(current_class.get_source())
dump_classes.append(method.get_class_name())

print "bytecodes ...",
bytecode_buff = dvm.get_bytecodes_method(vm, vmx, method)
fd = open(filename + ".ag", "w")
fd.write(bytecode_buff)
fd.close()

with open(filename + ".ag", "w") as fd:
fd.write(bytecode_buff)
print


Expand Down
15 changes: 8 additions & 7 deletions androdiff.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from androguard.core.bytecodes import apk, dvm
from androguard.core.analysis import analysis
from androguard.core import androconf
from androguard.util import read

sys.path.append("./elsim")
from elsim import elsim
Expand All @@ -48,29 +49,29 @@ def main(options, arguments) :
details = False
if options.display != None :
details = True

if options.input != None :
ret_type = androconf.is_android( options.input[0] )
if ret_type == "APK" :
a = apk.APK( options.input[0] )
d1 = dvm.DalvikVMFormat( a.get_dex() )
elif ret_type == "DEX" :
d1 = dvm.DalvikVMFormat( open(options.input[0], "rb").read() )
d1 = dvm.DalvikVMFormat( read(options.input[0]) )

dx1 = analysis.VMAnalysis( d1 )

ret_type = androconf.is_android( options.input[1] )
if ret_type == "APK" :
a = apk.APK( options.input[1] )
d2 = dvm.DalvikVMFormat( a.get_dex() )
elif ret_type == "DEX" :
d2 = dvm.DalvikVMFormat( open(options.input[1], "rb").read() )
d2 = dvm.DalvikVMFormat( read(options.input[1]) )

dx2 = analysis.VMAnalysis( d2 )

print d1, dx1, d2, dx2
sys.stdout.flush()

threshold = None
if options.threshold != None :
threshold = float(options.threshold)
Expand Down
10 changes: 4 additions & 6 deletions androdump.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,15 +94,13 @@ def getFilesBuffer(self) :

def dumpMemory(self, base_filename) :
for i in self.data :
fd = open(base_filename + "-" + "0x%x-0x%x" % (i[0].start, i[0].end), "w")
fd.write( i[1] )
fd.close()
with open(base_filename + "-" + "0x%x-0x%x" % (i[0].start, i[0].end), "w") as fd:
fd.write( i[1] )

def dumpFiles(self, base_filename) :
for i in self.data :
fd = open(base_filename + "-" + "0x%x-0x%x" % (i[0].start + i[2], i[0].end), "w")
fd.write( i[1][i[2]:] )
fd.close()
with fd = open(base_filename + "-" + "0x%x-0x%x" % (i[0].start + i[2], i[0].end), "w") as fd:
fd.write( i[1][i[2]:] )

class AndroDump :
def __init__(self, adp) :
Expand Down
11 changes: 6 additions & 5 deletions androgexf.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
# (at your option) any later version.
#
# Androguard is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
Expand All @@ -25,6 +25,7 @@
from androguard.core.bytecodes import apk, dvm
from androguard.core.analysis import analysis, ganalysis
from androguard.core import androconf
from androguard.util import read

option_0 = { 'name' : ('-i', '--input'), 'help' : 'filename input (dex, apk)', 'nargs' : 1 }
option_1 = { 'name' : ('-o', '--output'), 'help' : 'filename output of the gexf', 'nargs' : 1 }
Expand All @@ -34,7 +35,7 @@
def main(options, arguments) :
if options.input != None and options.output != None :
ret_type = androconf.is_android( options.input )

vm = None
a = None
if ret_type == "APK" :
Expand All @@ -45,7 +46,7 @@ def main(options, arguments) :
print "INVALID APK"
elif ret_type == "DEX" :
try :
vm = dvm.DalvikVMFormat( open(options.input, "rb").read() )
vm = dvm.DalvikVMFormat( read(options.input) )
except Exception, e :
print "INVALID DEX", e

Expand All @@ -62,7 +63,7 @@ def main(options, arguments) :
del option['name']
parser.add_option(*param, **option)


options, arguments = parser.parse_args()
sys.argv[:] = arguments
main(options, arguments)
main(options, arguments)
5 changes: 3 additions & 2 deletions androguard/core/analysis/analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from androguard.core.androconf import error, warning, debug, is_ascii_problem
from androguard.core.bytecodes import jvm, dvm
from androguard.core.bytecodes.api_permissions import DVM_PERMISSIONS_BY_PERMISSION, DVM_PERMISSIONS_BY_ELEMENT
from androguard.util import read

class ContextField :
def __init__(self, mode) :
Expand Down Expand Up @@ -2351,7 +2352,7 @@ class VMAnalysis(object):
:type _vm: a :class:`DalvikVMFormat` object
:Example:
VMAnalysis( DalvikVMFormat( open("toto.dex", "r").read() ) )
VMAnalysis( DalvikVMFormat( read("toto.dex", binary=False) ) )
"""
def __init__(self, _vm) :
self.__vm = _vm
Expand Down Expand Up @@ -2501,7 +2502,7 @@ class uVMAnalysis(VMAnalysis) :
:type _vm: a :class:`DalvikVMFormat` object
:Example:
uVMAnalysis( DalvikVMFormat( open("toto.dex", "r").read() ) )
uVMAnalysis( DalvikVMFormat( read("toto.dex", binary=False) ) )
"""
def __init__(self, vm) :
self.vm = vm
Expand Down
3 changes: 2 additions & 1 deletion androguard/core/analysis/auto.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from androguard.core.bytecodes import apk, dvm
from androguard.core.analysis import analysis
from androguard.core.androconf import debug
from androguard.util import read


class AndroAuto(object):
Expand Down Expand Up @@ -354,5 +355,5 @@ def fetcher(self, q):
if real_filename[-1] != "/":
real_filename += "/"
real_filename += f
q.put((real_filename, open(real_filename, "rb").read()))
q.put((real_filename, read(real_filename)))
return False
14 changes: 5 additions & 9 deletions androguard/core/androconf.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,14 +207,11 @@ def is_android(filename) :
if not filename:
return None

fd = open( filename, "r")
val = None
with open(filename, "r") as fd:
f_bytes = fd.read(7)
val = is_android_raw( f_bytes )

f_bytes = fd.read(7)

val = is_android_raw( f_bytes )

fd.close()
return val

def is_android_raw(raw):
Expand Down Expand Up @@ -279,9 +276,8 @@ def set_options(key, value) :
CONF[ key ] = value

def save_to_disk(buff, output) :
fd = open(output, "w")
fd.write(buff)
fd.close()
with open(output, "w") as fd:
fd.write(buff)

def rrmdir( directory ):
for root, dirs, files in os.walk(directory, topdown=False):
Expand Down
9 changes: 5 additions & 4 deletions androguard/core/androgen.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from androguard.core.bytecodes import apk
from androguard.core.analysis import analysis
from androguard.core.analysis import ganalysis
from androguard.util import read

class BC :
def __init__(self, bc) :
Expand Down Expand Up @@ -92,7 +93,7 @@ def __init__(self, files, raw=False) :

self.__orig_raw = {}
for i in self.__files :
self.__orig_raw[ i ] = open(i, "rb").read()
self.__orig_raw[ i ] = read(i)

self.__bc = []
self._analyze()
Expand All @@ -112,12 +113,12 @@ def _analyze(self) :
x = apk.APK( i )
bc = dvm.DalvikVMFormat( x.get_dex() )
elif ret_type == "DEX" :
bc = dvm.DalvikVMFormat( open(i, "rb").read() )
bc = dvm.DalvikVMFormat( read(i) )
elif ret_type == "DEY" :
bc = dvm.DalvikOdexVMFormat( open(i, "rb").read() )
bc = dvm.DalvikOdexVMFormat( read(i) )
elif ret_type == "ELF" :
from androguard.core.binaries import elf
bc = elf.ELF( open(i, "rb").read() )
bc = elf.ELF( read(i) )
else :
raise( "Unknown format" )

Expand Down
5 changes: 2 additions & 3 deletions androguard/core/bytecode.py
Original file line number Diff line number Diff line change
Expand Up @@ -719,10 +719,9 @@ def set_buff(self, buff) :
self.__buff = buff

def save(self, filename) :
fd = open(filename, "w")
buff = self._save()
fd.write( buff )
fd.close()
with open(filename, "w") as fd:
fd.write( buff )

def FormatClassToJava(input) :
"""
Expand Down
9 changes: 4 additions & 5 deletions androguard/core/bytecodes/apk.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from androguard.core import bytecode
from androguard.core import androconf
from androguard.core.bytecodes.dvm_permissions import DVM_PERMISSIONS
from androguard.util import read

import StringIO
from struct import pack, unpack
Expand All @@ -42,7 +43,7 @@
ZIPMODULE = 0
# UNLOCK : change it with your valid key !
try :
CHILKAT_KEY = open("key.txt", "rb").read()
CHILKAT_KEY = read("key.txt")
except Exception :
CHILKAT_KEY = "testme"

Expand Down Expand Up @@ -147,7 +148,7 @@ class APK(object):
:Example:
APK("myfile.apk")
APK(open("myfile.apk", "rb").read(), raw=True)
APK(read("myfile.apk"), raw=True)
"""
def __init__(self, filename, raw=False, mode="r", magic_file=None, zipmodule=ZIPMODULE):
self.filename = filename
Expand All @@ -169,9 +170,7 @@ def __init__(self, filename, raw=False, mode="r", magic_file=None, zipmodule=ZIP
if raw == True:
self.__raw = filename
else:
fd = open(filename, "rb")
self.__raw = fd.read()
fd.close()
self.__raw = read(filename)

self.zipmodule = zipmodule

Expand Down
Loading

0 comments on commit 4c136ed

Please sign in to comment.