Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion objection/console/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,13 @@
},

'reconnect': {
'meta': 'Reconnect to the current device',
'meta': 'Reconnect to the current app',
'exec': None, # handled in the Repl class itself
},

'reconnect_spawn': {
'meta': 'Respawn the current app',

'resume': {
'meta': 'Resume the attached process',
'exec': None
Expand Down
58 changes: 44 additions & 14 deletions objection/console/repl.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,22 +291,52 @@ def handle_reconnect(document: str) -> bool:
:return:
"""

if document.strip() in ('reconnect', 'reset'):

click.secho('Reconnecting...', dim=True)

if document.strip() in ('reconnect', 'reset', 'reconnect_spawn'):
try:
# TODO
# state_connection.a.unload()
#
# agent = OldAgent()
# agent.inject()
# state_connection.a = agent
from .cli import get_agent

click.secho('Not yet implemented!', fg='yellow')

except (frida.ServerNotRunningError, frida.TimedOutError) as e:
click.secho('Failed to reconnect with error: {0}'.format(e), fg='red')
reconnect_spawn = document.strip() == 'reconnect_spawn'
if reconnect_spawn:
click.secho('Performing full-restart...', fg='yellow')
state_connection.spawn = True
state_connection.no_pause = True
else:
click.secho('Performing soft-restart...', fg='yellow')
state_connection.spawn = False

curr_agent = state_connection.agent

# Cleanup current agent (ignore errors if already destroyed)
click.secho('Unloading current agent...', dim=True)
try:
if curr_agent.script:
curr_agent.script.unload()

except (frida.InvalidOperationError, Exception):
pass # Script already destroyed or detached

try:
if curr_agent.session:
curr_agent.session.detach()
except (frida.InvalidOperationError, Exception):
pass # Session already detached

# Need to clear because destructor will attempt to clear script/session again.
curr_agent.script = None
state_connection.agent = None
state_connection.session = None

click.secho(f'Re-attaching to {state_connection.name}...', dim=True)

# Try respawn the agent.
new_agent = get_agent()
state_connection.agent = new_agent

click.secho('Reconnection successful!', fg='green')

except Exception as e:
click.secho(f'Reconnection failed: {e}', fg='red')
click.secho('Ensure the application is running and the device is connected.', dim=True)

return True

Expand Down
21 changes: 18 additions & 3 deletions objection/utils/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,21 @@ def set_target_pid(self):
raise Exception('--uid flag can only be used on Android.')
self.pid = self.device.spawn(self.config.name, uid=int(self.config.uid))
else:
self.pid = self.device.spawn(self.config.name)
try:
self.pid = self.device.spawn(self.config.name)
except frida.InvalidArgumentError:
pass

# Maybe we have an app name and not identifier
app_list = self.device.enumerate_applications()
app_name_lc = self.config.name.lower()

matching_app = [app for app in app_list if app.name.lower() == app_name_lc]
# Don't care about matching_app[0].pid not in (0, None), if already running we restart anyway.
if len(matching_app) == 1:
debug_print("Found app by name instead of package, spawning.")
self.pid = self.device.spawn(matching_app[0].identifier)

self.resumed = False
else:
# check if the name is actually an integer. this way we can
Expand All @@ -241,11 +255,12 @@ def set_target_pid(self):
pass

if self.pid is None:
# maybe we have an app identifier
# maybe we have an app identifier/package name
app_list = self.device.enumerate_applications()
app_name_lc = self.config.name.lower()
matching_app = [app for app in app_list if app.identifier.lower() == app_name_lc]
if len(matching_app) == 1 and matching_app[0].pid is not None:
if len(matching_app) == 1 and matching_app[0].pid not in (0, None):
debug_print("Found app by package name.")
self.pid = matching_app[0].pid
elif len(matching_app) > 1:
app_list_str = ', '.join([f"{app.identifier}: {app.pid}" for app in matching_app])
Expand Down
Loading