Skip to content

Commit

Permalink
client: add sysctl
Browse files Browse the repository at this point in the history
  • Loading branch information
doronz88 committed Mar 17, 2022
1 parent 79c26cf commit d73443f
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/rpcclient/rpcclient/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
reply_protocol_message_t, dummy_block_t, SERVER_MAGIC_VERSION
from rpcclient.symbol import Symbol
from rpcclient.symbols_jar import SymbolsJar
from rpcclient.sysctl import Sysctl

tty_support = False
try:
Expand Down Expand Up @@ -77,6 +78,7 @@ def __init__(self, sock, sysname: str, hostname: str, port: int = None):
self.processes = Processes(self)
self.network = Network(self)
self.lief = Lief(self)
self.sysctl = Sysctl(self)

def info(self):
""" print information about current target """
Expand Down
41 changes: 41 additions & 0 deletions src/rpcclient/rpcclient/sysctl.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import struct

from rpcclient.exceptions import BadReturnValueError


class Sysctl:
""" sysctl utils. read man page for sysctl(3) for more details """

def __init__(self, client):
self._client = client

def get_str(self, name: str) -> str:
""" equivalent of: sysctl <name> """
return self.get(name).strip(b'\x00').decode()

def get_int(self, name: str) -> int:
""" equivalent of: sysctl <name> """
return struct.unpack('<I', self.get(name))[0]

def set_int(self, name: str, value: int):
""" equivalent of: sysctl <name> -w value """
self.set(name, struct.pack('<I', value))

def set_str(self, name: str, value: str):
""" equivalent of: sysctl <name> -w value """
self.set(name, value.encode() + b'\x00')

def set(self, name: str, value: bytes):
""" equivalent of: sysctl <name> -w value """
if self._client.symbols.sysctlbyname(name, 0, 0, value, len(value)):
raise BadReturnValueError(f'sysctl() failed: {self._client.last_error}')

def get(self, name: str) -> bytes:
""" equivalent of: sysctl <name> """
oldval_len = 1024
with self._client.safe_malloc(8) as p_oldval_len:
p_oldval_len[0] = oldval_len
with self._client.safe_malloc(oldval_len) as oldval:
if self._client.symbols.sysctlbyname(name, oldval, p_oldval_len, 0, 0):
raise BadReturnValueError(f'sysctl() failed: {self._client.last_error}')
return oldval.peek(p_oldval_len[0])

0 comments on commit d73443f

Please sign in to comment.