Skip to content

Commit

Permalink
Info commands should give device info too
Browse files Browse the repository at this point in the history
  • Loading branch information
alterakey committed Mar 26, 2024
1 parent 16e5bf5 commit 350abe5
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 28 deletions.
21 changes: 7 additions & 14 deletions trueseeing/app/cmd/info.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,36 +20,32 @@ def create(helper: CommandHelper) -> Command:

def get_commands(self) -> CommandMap:
return {
'i':dict(e=self._info, n='i[i][i]', d='print info (ii: overall, iii: detailed)'),
'i':dict(e=self._info, n='i[i]', d='print info (ii: more info)'),
'ii':dict(e=self._info2),
'iii':dict(e=self._info3),
}

def _read_field(self, x: Any) -> Any:
boolmap = {True:'yes',False:'no','true':'yes','false':'no',1:'yes',0:'no', None:'?'}
return boolmap.get(x, x)

async def _info(self, args: deque[str], level: int = 0) -> None:
async def _info(self, args: deque[str], extended: bool = False) -> None:
target = self._helper.require_target()

_ = args.popleft()

analysisguidemap = {
0: 'try ii for more info',
1: 'try iii for more info',
2: 'try iii for more info',
0: 'try a;i for more info',
1: 'try a;i for more info',
2: 'try aa;i for more info',
3: 'try aaa;i for more info',
}

context = self._helper.get_context()
analyzed = context.get_analysis_level()
if analyzed < level:
await context.analyze(level=level)
analyzed = level

ui.info(f'info on {target}')

async for m in context._get_info():
async for m in context._get_info(extended):
for k, v in m.items():
if v is None:
continue
Expand All @@ -65,7 +61,4 @@ async def _info(self, args: deque[str], level: int = 0) -> None:
))

async def _info2(self, args: deque[str]) -> None:
return await self._info(args, level=1)

async def _info3(self, args: deque[str]) -> None:
return await self._info(args, level=3)
return await self._info(args, extended=True)
24 changes: 22 additions & 2 deletions trueseeing/core/android/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,8 +202,8 @@ def _addr_ceil(nr: int) -> int:
def get_package_name(self) -> str:
return self.parsed_manifest().attrib['package'] # type: ignore[no-any-return]

async def _get_info(self) -> AsyncIterator[ContextInfo]:
async for m in super()._get_info():
async def _get_info(self, extended: bool) -> AsyncIterator[ContextInfo]:
async for m in super()._get_info(extended):
yield m

level = self.get_analysis_level()
Expand Down Expand Up @@ -260,6 +260,26 @@ async def _get_info(self) -> AsyncIterator[ContextInfo]:
for nr, in c.execute('select count(1) from map where method is not null'):
yield dict(methods='{}'.format(nr))

if extended:
from subprocess import CalledProcessError
from trueseeing.core.android.device import AndroidDevice
dev = AndroidDevice()
try:
build = (await dev.invoke_adb('shell getprop ro.build.fingerprint', catch_stderr=True)).rstrip()
yield {
'device?': 'yes ({})'.format(build),
}
except CalledProcessError as e:
cause = e.stderr.decode().splitlines()[-1]
if cause.startswith('adb: no '):
yield {
'device?': 'no',
}
else:
yield {
'device?': '? ({})'.format(cause)
}

def parsed_manifest(self, patched: bool = False) -> Any:
return self.store().query().file_get_xml('AndroidManifest.xml', patched=patched)

Expand Down
14 changes: 7 additions & 7 deletions trueseeing/core/android/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,29 @@
from trueseeing.core.ui import ui

if TYPE_CHECKING:
from typing import Optional, AsyncIterator
from typing import Optional, AsyncIterator, Any

class AndroidDevice:
def __init__(self) -> None:
pass

async def invoke_adb(self, cmd: str) -> str:
async def invoke_adb(self, cmd: str, **kwargs: Any) -> str:
self._require_adb()
line = self._get_adb_cmdline(cmd)
ui.debug("invoking: {line}")
return await invoke(line)
return await invoke(line, **kwargs)

async def invoke_adb_passthru(self, cmd: str) -> None:
async def invoke_adb_passthru(self, cmd: str, **kwargs: Any) -> None:
self._require_adb()
line = self._get_adb_cmdline(cmd)
ui.debug("invoking: {line}")
await invoke_passthru(line)
await invoke_passthru(line, **kwargs)

async def invoke_adb_streaming(self, cmd: str) -> AsyncIterator[bytes]:
async def invoke_adb_streaming(self, cmd: str, **kwargs: Any) -> AsyncIterator[bytes]:
self._require_adb()
line = self._get_adb_cmdline(cmd)
ui.debug("invoking: {line}")
async for l in invoke_streaming(line):
async for l in invoke_streaming(line, **kwargs):
yield l

def _get_adb_cmdline(self, cmd: str) -> str:
Expand Down
2 changes: 1 addition & 1 deletion trueseeing/core/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ async def _recheck_schema(self) -> None: ...
@abstractmethod
async def _analyze(self, level: int) -> None: ...

async def _get_info(self) -> AsyncIterator[ContextInfo]:
async def _get_info(self, extended: bool) -> AsyncIterator[ContextInfo]:
yield dict(path=self._path)
size = self.size_of()
if size:
Expand Down
8 changes: 4 additions & 4 deletions trueseeing/core/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@ def require_in_path(cmd: str, cmdline: str) -> None:
except CalledProcessError:
ui.fatal('not found: {cmd}')

async def invoke(as_: str, redir_stderr: bool = False) -> str:
async def invoke(as_: str, redir_stderr: bool = False, catch_stderr: bool = False) -> str:
from asyncio import create_subprocess_shell
from subprocess import PIPE, STDOUT
p = await create_subprocess_shell(as_, stdout=PIPE, stderr=(STDOUT if redir_stderr else None))
out, _ = await p.communicate()
_check_return_code(p, as_, out, None)
p = await create_subprocess_shell(as_, stdout=PIPE, stderr=(STDOUT if redir_stderr else (PIPE if catch_stderr else None)))
out, err = await p.communicate()
_check_return_code(p, as_, out, err)
return out.decode('UTF-8')

async def invoke_passthru(as_: str, nocheck: bool = False) -> None:
Expand Down

0 comments on commit 350abe5

Please sign in to comment.