Skip to content

Commit

Permalink
Correct a few issues raised by CodeQL
Browse files Browse the repository at this point in the history
  • Loading branch information
LRGH committed Mar 18, 2024
1 parent 000d6bb commit 17e6fd7
Show file tree
Hide file tree
Showing 26 changed files with 224 additions and 160 deletions.
1 change: 0 additions & 1 deletion elfcli
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import sys
import code
#import elfesteem.elf
import readline
import rlcompleter
import argparse


Expand Down
6 changes: 5 additions & 1 deletion elfesteem/binary.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,11 @@ def __init__(self, raw):
if __name__ == "__main__":
for file in sys.argv[1:]:
print("File: %s"%file)
raw = open(file, 'rb').read()
fd = open(file, 'rb')
try:
raw = fd.read()
finally:
fd.close()
e = BINARY(raw)
print(" container %s" % e.container)
print(" architecture %s" % e.architecture)
Expand Down
2 changes: 1 addition & 1 deletion elfesteem/cstruct.py
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ def __repr__(self):

# Method that defines constants (as in .h headers) and tables that
# can recover the constant's name from its value.
def Constants(globs = {}, table = {},
def Constants(globs = None, table = None,
name = None, prefix = None,
no_name = (), **kargs):
if prefix is None:
Expand Down
19 changes: 10 additions & 9 deletions elfesteem/elf_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def __new__(cls, name, bases, dct):
class Section(SectionBase):
sht = None
def create(cls, parent, shstr=None):
if shstr == None:
if shstr is None:
sh = None
else:
sh = elf.Shdr(parent = None, content = shstr, sex = parent.sex,
Expand Down Expand Up @@ -247,9 +247,6 @@ def add_name(self, name):
def mod_name(self, idx, name):
name = name_to_bytes(name)
n = self.content[idx:self.content.find(data_null, idx)]
data = self.content
if type(data) != str: data = data.pack()
data = data[:idx]+name+data[idx+len(n):]
dif = len(name) - len(n)
if dif != 0:
for sh in self.parent.shlist:
Expand Down Expand Up @@ -560,7 +557,7 @@ def __init__(self, x):
self.parent = x

def get_rvaitem(self, item, section = None):
if item.stop == None:
if item.stop is None:
s = self.parent.getsectionbyvad(item.start, section)
return [(s, item.start-s.addr)]

Expand Down Expand Up @@ -775,7 +772,7 @@ def elf_set_offsets(self):
+ [ ".shstrtab", None, ".symtab", ".strtab"] \
+ [ ".rel"+name for name in section_layout ]
for name in section_layout:
if name == None:
if name is None:
pos = ((pos + 3)//4)*4
self.Ehdr.shoff = pos
self.Ehdr.shentsize = self.sh._shstrtab.sh.bytelen
Expand Down Expand Up @@ -967,10 +964,14 @@ def is_in_virt_address(self, ad):
_content = property(lambda _:_.content)

if __name__ == "__main__":
import rlcompleter,readline,pdb
from pprint import pprint as pp
import readline
readline.parse_and_bind("tab: complete")

e = ELF(open("/bin/ls").read())
fd = open("/bin/ls")
try:
raw = fd.read()
finally:
fd.close()
e = ELF(raw)
print (repr(e))
#o = ELF(open("/tmp/svg-main.o").read())
14 changes: 7 additions & 7 deletions elfesteem/jclass_init.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
#! /usr/bin/env python

import struct, array
from strpatchwork import StrPatchwork
import struct
from new_cstruct import CStruct
import logging
from collections import defaultdict
from pprint import pprint as pp
log = logging.getLogger("classparse")
console_handler = logging.StreamHandler()
console_handler.setFormatter(logging.Formatter("%(levelname)-5s: %(message)s"))
Expand All @@ -19,7 +16,7 @@ def gensapce(lvl):
return ' '*lvl

def out_attrs(o, lvl = None):
if lvl == None:
if lvl is None:
lvl = 0
out = ""
if not isinstance(o, list):
Expand Down Expand Up @@ -593,6 +590,9 @@ def add_fieldref(self, name, typetype, typename):

if __name__ == "__main__":
import sys
from pprint import pprint as pp
data = open(sys.argv[1]).read()
fd = open(sys.argv[1])
try:
data = fd.read()
finally:
fd.close()
e = JCLASS(data)
8 changes: 4 additions & 4 deletions elfesteem/macho/init.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ def unpack(self, c, o):
inverse = intervals.Intervals().add(0,farch.size)
for j in e.interval.ranges:
inverse.delete(j.start,j.stop)
if not self.parent.interval == None:
if not self.parent.interval is None:
for j in inverse.ranges:
if not self.parent.interval.contains(farch.offset+j.start,farch.offset+j.stop):
raise ValueError("This part of file has already been parsed")
Expand Down Expand Up @@ -173,7 +173,7 @@ def __setitem__(self, item, data):
def get_rvaitem(self, item, section = None):
if item.step != None:
raise ValueError("pas de step")
if item.stop == None:
if item.stop is None:
s = self.parent.getsectionbyvad(item.start, section = section)
if not s:
raise ValueError('unknown rva address! 0x%x'%item.start)
Expand Down Expand Up @@ -476,7 +476,7 @@ def add(self, *args, **kargs):
if 'content' in kargs :
nwsh = Section(parent=sectionHeader(parent=self.load),
content=kargs['content'])
if not nwlc.segname==None:
if not nwlc.segname is None:
nwsh.parent.segname = nwlc.segname
self.add(nwlc)
self.add(nwsh)
Expand Down Expand Up @@ -507,7 +507,7 @@ def incompletedPosVal(self):
return result

def checkParsedCompleted(self, **kargs):
if self.interval == None :
if self.interval is None :
raise ValueError("No interval argument in macho_init call")
result = []
for i in self.interval :
Expand Down
3 changes: 1 addition & 2 deletions elfesteem/macho/loaders.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,6 @@ def otool(self, llvm=False):
# Output similar to llvm-otool (depending on llvm version)
# Cf. https://opensource.apple.com/source/cctools/cctools-895/otool/ofile_print.c
# and others
res = []
import time
lc_value = []
shift = 1
Expand Down Expand Up @@ -1565,7 +1564,7 @@ class data_in_code_command(CStruct):
######################################################################

def isOffsetChangeable(offset, min_offset):
return (min_offset == None or offset >= min_offset) and offset != 0
return (min_offset is None or offset >= min_offset) and offset != 0

class LoadCommands(CBase):
def unpack(self, c, o):
Expand Down
33 changes: 24 additions & 9 deletions elfesteem/macho/sections.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import struct
from elfesteem.macho.common import *
from elfesteem.cstruct import Constants, CBase, CData, CString, CArray, CStructWithStrTable
from elfesteem.cstruct import Constants, CBase, CString, CArray, CStructWithStrTable
from elfesteem.strpatchwork import StrPatchwork

import sys
Expand Down Expand Up @@ -528,8 +528,9 @@ class bind_opcode(bind_entry):
_fields = [ ("val", "u08") ]
def apply(self):
self.parent.index = self.index + self.bytelen
del bind_opcode

class bind_opcode(bind_entry):
class bind_opcode_libord(bind_entry):
opcode = BIND_OPCODE_SET_DYLIB_ORDINAL_IMM
_fields = [ ("val", "u08") ]
libord = property(lambda _: _.imm)
Expand All @@ -538,17 +539,20 @@ def __str__(self):
def apply(self):
self.parent.libord = int(self.libord)

class bind_opcode(bind_opcode):
class bind_opcode(bind_opcode_libord):
opcode = BIND_OPCODE_SET_DYLIB_ORDINAL_ULEB
_fields = [ ("val", "u08"), ("libord", Uleb128) ]
del bind_opcode

class bind_opcode(bind_opcode):
class bind_opcode(bind_opcode_libord):
opcode = BIND_OPCODE_SET_DYLIB_SPECIAL_IMM
_fields = [ ("val", "u08") ]
def libord(self):
if self.imm: return self.imm | BIND_OPCODE_MASK
else: return 0
libord = property(libord)
del bind_opcode
del bind_opcode_libord

class bind_opcode(bind_entry):
opcode = BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM
Expand All @@ -561,6 +565,7 @@ def apply(self):
self.parent.weak_import = " (weak import)"
else:
self.parent.weak_import = ""
del bind_opcode

class bind_opcode(bind_entry):
opcode = BIND_OPCODE_SET_TYPE_IMM
Expand All @@ -574,6 +579,7 @@ def apply(self):
BIND_TYPE_TEXT_ABSOLUTE32: "text abs32",
BIND_TYPE_TEXT_PCREL32: "text rel32",
}.get(self.imm,"!!unknown!!")
del bind_opcode

class bind_opcode(bind_entry):
opcode = BIND_OPCODE_SET_ADDEND_SLEB
Expand All @@ -582,6 +588,7 @@ def __str__(self):
return bind_entry.__str__(self) + '(%d)' % int(self.addend)
def apply(self):
self.parent.addend = self.addend
del bind_opcode

class bind_opcode(bind_entry):
opcode = BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB
Expand All @@ -598,6 +605,7 @@ def apply(self):
else:
self.parent.seg = None
self.parent.addr = None
del bind_opcode

class bind_opcode(bind_entry):
opcode = BIND_OPCODE_ADD_ADDR_ULEB
Expand All @@ -608,6 +616,7 @@ def apply(self):
if not hasattr(self.parent, 'addr'): raise ValueError
self.parent.addr += int(self.addr)
self.parent.addr &= mask64
del bind_opcode

class bind_opcode(bind_entry):
opcode = BIND_OPCODE_DO_BIND
Expand All @@ -618,6 +627,7 @@ def apply(self):
if not hasattr(self.parent, 'addr'): raise ValueError
self.parent._info.append(self.parent.cls(self))
self.parent.addr += self.wsize//8
del bind_opcode

class bind_opcode(bind_entry):
opcode = BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB
Expand All @@ -629,6 +639,7 @@ def apply(self):
self.parent._info.append(self.parent.cls(self))
self.parent.addr += self.wsize//8 + int(self.addr)
self.parent.addr &= mask64
del bind_opcode

class bind_opcode(bind_entry):
opcode = BIND_OPCODE_DO_BIND_ADD_ADDR_IMM_SCALED
Expand All @@ -639,6 +650,7 @@ def __str__(self):
def apply(self):
self.parent._info.append(self.parent.cls(self))
self.parent.addr += self.add_addr
del bind_opcode

class bind_opcode(bind_entry):
opcode = BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB
Expand All @@ -651,7 +663,6 @@ def apply(self):
for i in range(int(self.count)):
self.parent._info.append(self.parent.cls(self))
self.parent.addr += int(self.skip) + self.wsize//8

del bind_opcode

class DyldArrayBind(DyldArrayGeneric):
Expand Down Expand Up @@ -748,6 +759,7 @@ class rebase_opcode(rebase_entry):
_fields = [ ("val", "u08") ]
def __str__(self):
return rebase_entry.__str__(self) + '()'
del rebase_opcode

class rebase_opcode(rebase_entry):
opcode = REBASE_OPCODE_SET_TYPE_IMM
Expand All @@ -761,6 +773,7 @@ def apply(self):
REBASE_TYPE_TEXT_ABSOLUTE32: "text abs32",
REBASE_TYPE_TEXT_PCREL32: "text rel32",
}.get(self.imm,"!!unknown!!")
del rebase_opcode

class rebase_opcode(rebase_entry):
opcode = REBASE_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB
Expand All @@ -777,6 +790,7 @@ def apply(self):
else:
self.parent.seg = None
self.parent.addr = None
del rebase_opcode

class rebase_opcode(rebase_entry):
opcode = REBASE_OPCODE_ADD_ADDR_ULEB
Expand All @@ -786,6 +800,7 @@ def __str__(self):
def apply(self):
if not hasattr(self.parent, 'addr'): raise ValueError
self.parent.addr += int(self.addr)
del rebase_opcode

class rebase_opcode(rebase_entry):
opcode = REBASE_OPCODE_ADD_ADDR_IMM_SCALED
Expand All @@ -796,6 +811,7 @@ def __str__(self):
def apply(self):
if not hasattr(self.parent, 'addr'): raise ValueError
self.parent.addr += self.add_addr
del rebase_opcode

class rebase_opcode(rebase_entry):
opcode = REBASE_OPCODE_DO_REBASE_IMM_TIMES
Expand All @@ -807,6 +823,7 @@ def apply(self):
for i in range(self.imm):
self.parent._info.append(self.parent.cls(self))
self.parent.addr += self.wsize//8
del rebase_opcode

class rebase_opcode(rebase_entry):
opcode = REBASE_OPCODE_DO_REBASE_ULEB_TIMES
Expand All @@ -818,6 +835,7 @@ def apply(self):
for i in range(int(self.count)):
self.parent._info.append(self.parent.cls(self))
self.parent.addr += self.wsize//8
del rebase_opcode

class rebase_opcode(rebase_entry):
opcode = REBASE_OPCODE_DO_REBASE_ADD_ADDR_ULEB
Expand All @@ -829,6 +847,7 @@ def apply(self):
if not hasattr(self.parent, 'addr'): raise ValueError
self.parent._info.append(self.parent.cls(self))
self.parent.addr += self.add_addr
del rebase_opcode

class rebase_opcode(rebase_entry):
opcode = REBASE_OPCODE_DO_REBASE_ULEB_TIMES_SKIPPING_ULEB
Expand All @@ -840,7 +859,6 @@ def apply(self):
for i in range(int(self.count)):
self.parent._info.append(self.parent.cls(self))
self.parent.addr += int(self.skip) + self.wsize//8

del rebase_opcode

class DyldArrayRebase(DyldArrayGeneric):
Expand Down Expand Up @@ -1074,9 +1092,6 @@ def add_name(self, name):
def mod_name(self, idx, name):
name = name_to_bytes(name)
n = self.content[idx:self.content.find(data_null,idx)]
data = self.content
if type(data) != str: data = data.pack()
data = data[:idx]+name+data[idx+len(n):]
dif = len(name) - len(n)
if dif != 0:
for sh in self.parent.shlist:
Expand Down
6 changes: 5 additions & 1 deletion elfesteem/minidump_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,10 @@ def max_addr(self):
if __name__ == "__main__":
for file in sys.argv[1:]:
if len(sys.argv) > 2: print("File: %s"%file)
raw = open(file, 'rb').read()
fd = open(file, 'rb')
try:
raw = fd.read()
finally:
fd.close()
e = Minidump(raw)
print(e.dump())
Loading

0 comments on commit 17e6fd7

Please sign in to comment.