mirror of https://github.com/pwndbg/pwndbg.git
refactored wrapper (#280)
* added command got to display status of the got table Signed-off-by: degrigis <degrigis@gmail.com> * return when checksec is not available and added decorator OnlyWhenRunning Signed-off-by: degrigis <degrigis@gmail.com> * removed duplicated code for pie and not pie binaries Signed-off-by: degrigis <degrigis@gmail.com> * inserted support function to get checksec output and performed all requirements check initially Signed-off-by: degrigis <degrigis@gmail.com> * corrected typo Signed-off-by: degrigis <degrigis@gmail.com> * reorganized the command got splitting the code in library routines and moved the checksec internal function in a separate module Signed-off-by: degrigis <degrigis@gmail.com> * handled exception directly inside functions and enhanced code Signed-off-by: degrigis <degrigis@gmail.com> * extracted only column in readelf output and enhanced exception handling Signed-off-by: degrigis <degrigis@gmail.com> * fix exception handling returning subprocess error Signed-off-by: degrigis <degrigis@gmail.com> * removed unused import and reordered Signed-off-by: degrigis <degrigis@gmail.com> * reordered imports Signed-off-by: degrigis <degrigis@gmail.com> * added wrappers module and refactored some code Signed-off-by: degrigis <degrigis@gmail.com> * removed not useful comment Signed-off-by: degrigis <degrigis@gmail.com> * removed unused import Signed-off-by: degrigis <degrigis@gmail.com> * moved comments in docstring Signed-off-by: degrigis <degrigis@gmail.com> * refactored code to use partial functions, simplified code Signed-off-by: degrigis <degrigis@gmail.com> * simplified a loc Signed-off-by: degrigis <degrigis@gmail.com> * capslock char fixed Signed-off-by: degrigis <degrigis@gmail.com> * removed unuseful pwndbg.arch.ptrsize check Signed-off-by: degrigis <degrigis@gmail.com> * refactored code and added the new module wrapper that contains every new wrapper module Signed-off-by: degrigis <degrigis@gmail.com> * used class style decorator for wrapper and improved code style Signed-off-by: degrigis <degrigis@gmail.com> * changed return with print for errors Signed-off-by: degrigis <degrigis@gmail.com> * removed prints debug and statically linked check moved at the top of the got function Signed-off-by: degrigis <degrigis@gmail.com> * refactored OnlyWithCommand decorator Signed-off-by: degrigis <degrigis@gmail.com> * wrappers are OnlyWithFile now Signed-off-by: degrigis <degrigis@gmail.com> * redirected stderr to stdout in subprocess.check_output and memoized the wrappers for readelf/file/checksec Signed-off-by: degrigis <degrigis@gmail.com> * reordered an import Signed-off-by: degrigis <degrigis@gmail.com> * removed pdb Signed-off-by: degrigis <degrigis@gmail.com> * fixed format string and removed desc from got command Signed-off-by: degrigis <degrigis@gmail.com> * consolidated decorators Signed-off-by: degrigis <degrigis@gmail.com> * merging Signed-off-by: degrigis <degrigis@gmail.com> * reordered import for travis Signed-off-by: degrigis <degrigis@gmail.com> * refactored some code Signed-off-by: degrigis <degrigis@gmail.com> * resolve travis complains Signed-off-by: degrigis <degrigis@gmail.com> * docstring for _extract_jumps Signed-off-by: degrigis <degrigis@gmail.com> * fixed isort Signed-off-by: degrigis <degrigis@gmail.com> * f*** isort Signed-off-by: degrigis <degrigis@gmail.com>pull/305/head
parent
5af436a60b
commit
8775073df0
@ -1,33 +0,0 @@
|
||||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
Wrappers to external utilities.
|
||||
"""
|
||||
|
||||
from __future__ import absolute_import
|
||||
from __future__ import division
|
||||
from __future__ import print_function
|
||||
from __future__ import unicode_literals
|
||||
|
||||
import functools
|
||||
import subprocess
|
||||
|
||||
import pwndbg.which
|
||||
|
||||
|
||||
def call_program(progname, *args):
|
||||
program = pwndbg.which.which(progname)
|
||||
|
||||
if not program:
|
||||
raise OSError('Could not find %s command in $PATH.' % progname)
|
||||
|
||||
cmd = [progname] + list(args)
|
||||
|
||||
try:
|
||||
return subprocess.check_output(cmd, stderr=subprocess.STDOUT).decode('utf-8')
|
||||
except Exception as e:
|
||||
raise OSError('Error during execution of %s command: %s' % (progname, e))
|
||||
|
||||
checksec = functools.partial(call_program, 'checksec')
|
||||
readelf = functools.partial(call_program, 'readelf')
|
||||
file = functools.partial(call_program, 'file')
|
||||
@ -0,0 +1,35 @@
|
||||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
from __future__ import absolute_import
|
||||
from __future__ import division
|
||||
from __future__ import print_function
|
||||
from __future__ import unicode_literals
|
||||
|
||||
import functools
|
||||
import subprocess
|
||||
from subprocess import STDOUT
|
||||
|
||||
import pwndbg.commands
|
||||
import pwndbg.which
|
||||
|
||||
|
||||
class OnlyWithCommand(object):
|
||||
def __init__(self, command):
|
||||
self.cmd_name = command
|
||||
self.cmd_path = pwndbg.which.which(command)
|
||||
|
||||
def __call__(self, function):
|
||||
function.cmd_path = self.cmd_path
|
||||
|
||||
@pwndbg.commands.OnlyWithFile
|
||||
@functools.wraps(function)
|
||||
def _OnlyWithCommand(*a,**kw):
|
||||
if self.cmd_path:
|
||||
return function(*a, **kw)
|
||||
else:
|
||||
raise OSError('Could not find command %s in $PATH' % self.cmd_name)
|
||||
return _OnlyWithCommand
|
||||
|
||||
|
||||
def call_cmd(cmd):
|
||||
return subprocess.check_output(cmd, stderr=STDOUT).decode('utf-8')
|
||||
@ -0,0 +1,46 @@
|
||||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
from __future__ import absolute_import
|
||||
from __future__ import division
|
||||
from __future__ import print_function
|
||||
from __future__ import unicode_literals
|
||||
|
||||
import pwndbg.commands
|
||||
import pwndbg.wrappers
|
||||
|
||||
cmd_name = "checksec"
|
||||
|
||||
@pwndbg.wrappers.OnlyWithCommand(cmd_name)
|
||||
def get_raw_out():
|
||||
|
||||
local_path = pwndbg.file.get_file(pwndbg.proc.exe)
|
||||
cmd = [get_raw_out.cmd_path, "--file", local_path]
|
||||
return pwndbg.wrappers.call_cmd(cmd)
|
||||
|
||||
@pwndbg.wrappers.OnlyWithCommand(cmd_name)
|
||||
def relro_status():
|
||||
relro = "No RELRO"
|
||||
|
||||
local_path = pwndbg.file.get_file(pwndbg.proc.exe)
|
||||
cmd = [relro_status.cmd_path, "--file", local_path]
|
||||
out = pwndbg.wrappers.call_cmd(cmd)
|
||||
|
||||
if "Full RELRO" in out:
|
||||
relro = "Full RELRO"
|
||||
elif "Partial RELRO" in out:
|
||||
relro = "Partial RELRO"
|
||||
|
||||
return relro
|
||||
|
||||
@pwndbg.wrappers.OnlyWithCommand(cmd_name)
|
||||
def pie_status():
|
||||
pie = "No PIE"
|
||||
|
||||
local_path = pwndbg.file.get_file(pwndbg.proc.exe)
|
||||
cmd = [pie_status.cmd_path, "--file", local_path]
|
||||
out = pwndbg.wrappers.call_cmd(cmd)
|
||||
|
||||
if "PIE enabled" in out:
|
||||
pie = "PIE enabled"
|
||||
|
||||
return pie
|
||||
@ -0,0 +1,35 @@
|
||||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
from __future__ import absolute_import
|
||||
from __future__ import division
|
||||
from __future__ import print_function
|
||||
from __future__ import unicode_literals
|
||||
|
||||
import pwndbg.wrappers
|
||||
|
||||
cmd_name = "readelf"
|
||||
|
||||
@pwndbg.wrappers.OnlyWithCommand(cmd_name)
|
||||
def get_jmpslots():
|
||||
local_path = pwndbg.file.get_file(pwndbg.proc.exe)
|
||||
cmd = [get_jmpslots.cmd_path, "--relocs", local_path]
|
||||
readelf_out = pwndbg.wrappers.call_cmd(cmd)
|
||||
|
||||
return filter(_extract_jumps, readelf_out.splitlines())
|
||||
|
||||
|
||||
def _extract_jumps(line):
|
||||
'''
|
||||
Checks for records in `readelf --relocs <binary>` which has type e.g. `R_X86_64_JUMP_SLO`
|
||||
NOTE: Because of that we DO NOT display entries that are not writeable (due to FULL RELRO)
|
||||
as they have `R_X86_64_GLOB_DAT` type.
|
||||
|
||||
It might be good to display them seperately in the future.
|
||||
'''
|
||||
try:
|
||||
if "JUMP" in line.split()[2]:
|
||||
return line
|
||||
else:
|
||||
return False
|
||||
except IndexError:
|
||||
return False
|
||||
Loading…
Reference in new issue