Skip to content

Commit

Permalink
Fixed some minor issues
Browse files Browse the repository at this point in the history
  • Loading branch information
dhondta committed Jan 23, 2024
1 parent f0fc572 commit 9a266b6
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/peid/VERSION.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.1.0
2.1.1
2 changes: 2 additions & 0 deletions src/peid/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ def identify_packer(*paths, db=None, ep_only=True, sec_start_only=False, match_a
:return: return the matching packers
"""
db, results = SignaturesTree(db, logger=logger), []
if logger:
logger.debug(f"ep_only={ep_only}, sec_start_only={sec_start_only}, match_all={match_all}")
for path in paths:
results.append((path, db.match(path, ep_only, sec_start_only, match_all)))
return results
Expand Down
7 changes: 3 additions & 4 deletions src/peid/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@ def _parser(name, description, examples):
def _setup(parser):
args = parser.parse_args()
if hasattr(args, "verbose"):
logging.basicConfig()
logging.basicConfig(level=[logging.INFO, logging.DEBUG][args.verbose])
args.logger = logging.getLogger("peid")
args.logger.setLevel([logging.INFO, logging.DEBUG][args.verbose])
return args


Expand Down Expand Up @@ -55,12 +54,12 @@ def main():
opt.add_argument("-m", "--match-once", action="store_true", help="match only one signature")
grp.add_argument("-s", "--section-start-only", dest="sec_start_only", action="store_true",
help="consider only signatures from section starts (default: False)")
opt.add_argument("-v", "--version", action="store_true", help="include the version in the result")
opt.add_argument("--version", action="store_true", help="include the version in the result")
extra = parser.add_argument_group("extra arguments")
extra.add_argument("-b", "--benchmark", action="store_true",
help="enable benchmarking, output in seconds (default: False)")
extra.add_argument("-h", "--help", action="help", help="show this help message and exit")
extra.add_argument("--verbose", action="store_true", help="display debug information (default: False)")
extra.add_argument("-v", "--verbose", action="store_true", help="display debug information (default: False)")
args = _setup(parser)
# execute the tool
if args.benchmark:
Expand Down
18 changes: 16 additions & 2 deletions src/peid/pe.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
# -*- coding: UTF-8 -*-
import builtins
from functools import lru_cache, wraps
from os.path import getsize


__all__ = ["PE"]


class MalformedPE(ValueError):
__module__ = "builtins"
builtins.MalformedPE = MalformedPE


class PE:
def __init__(self, path, logger=None):
self.path, self.size, self.logger = path, getsize(path), logger
Expand Down Expand Up @@ -61,9 +67,17 @@ def entrypoint_offset(self):
# EP is at byte 40 of the PE header (when image file)
self.__fd.seek(self.pe_offset + 40)
ep = int.from_bytes(self.__fd.read(4), "little")
if self.logger:
self.logger.debug(f"Entry point: 0x{ep:08x}")
for vsize, vaddr, rsize, raddr in self.itersections():
if vaddr <= ep < vaddr + rsize:
return raddr + ep - vaddr
if vaddr <= ep < vaddr + vsize:
o = raddr + ep - vaddr
if self.logger:
self.logger.debug(f"Entry point offset: {o}")
return o
self.__fd.seek(0)
c = self.__fd.read()
raise MalformedPE(f"Entry point (0x{ep:08x}) offset is outside sections (file size: 0x{len(c):08x})")

@property
def sections_offsets(self):
Expand Down

0 comments on commit 9a266b6

Please sign in to comment.