Skip to content

Conversation

VakarisZ
Copy link
Contributor

@VakarisZ VakarisZ commented Nov 23, 2021

Agent will now handle interrupt and break signals on linux and windows

What does this PR do?

Fixes #1594

Handles SIGINT and SIGTERM for Linux
Handles SIGBREAK and window close event for Windows

PR Checklist

  • Have you added an explanation of what your changes do and why you'd like to include them?
  • Is the TravisCI build passing?
  • Was the CHANGELOG.md updated to reflect the changes?
  • Was the documentation framework updated to reflect the changes?

Testing Checklist

  • Added relevant unit tests?
  • Have you successfully tested your changes locally? Elaborate:

    Tested by stopping agent with ctrl+c and kill -15 on Linux
    Tested by stopping agent with ctrl+c and by closing window on Windows

  • If applicable, add screenshots or log transcripts of the feature working

term_windows

@codecov
Copy link

codecov bot commented Nov 23, 2021

Codecov Report

Merging #1627 (2674e87) into 1593-i-master (e00fd64) will increase coverage by 0.61%.
The diff coverage is 0.00%.

❗ Current head 2674e87 differs from pull request most recent head 3f7c4a8. Consider uploading reports for the commit 3f7c4a8 to get more accurate results
Impacted file tree graph

@@                Coverage Diff                @@
##           1593-i-master    #1627      +/-   ##
=================================================
+ Coverage          44.08%   44.69%   +0.61%     
=================================================
  Files                468      461       -7     
  Lines              13642    13414     -228     
=================================================
- Hits                6014     5996      -18     
+ Misses              7628     7418     -210     
Impacted Files Coverage Δ
monkey/infection_monkey/monkey.py 0.00% <0.00%> (ø)
monkey/infection_monkey/utils/signal_handler.py 0.00% <0.00%> (ø)
...ey/infection_monkey/telemetry/post_breach_telem.py 65.21% <0.00%> (-5.16%) ⬇️
monkey/monkey_island/cc/app.py 79.54% <0.00%> (-0.61%) ⬇️
monkey/infection_monkey/telemetry/exploit_telem.py 100.00% <0.00%> (ø)
monkey/infection_monkey/i_master.py
monkey/infection_monkey/master/control_channel.py
...nd/cc/resources/monkey_control/stop_agent_check.py
...key_island/cc/resources/propagation_credentials.py
monkey/infection_monkey/i_control_channel.py
... and 5 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update e00fd64...3f7c4a8. Read the comment docs.

@mssalvatore mssalvatore changed the base branch from develop to 1593-i-master November 23, 2021 16:48
@mssalvatore mssalvatore marked this pull request as ready for review November 23, 2021 18:29
import win32api

signal.signal(signal.SIGBREAK, stop_signal_handler)
win32api.SetConsoleCtrlHandler(stop_signal_handler, True)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Based on this it has a 5s timeout. I think it's worth noting it in the code.

def __init__(self, master: IMaster):
self._master = master

def __call__(self, signum, __=None):
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what's the point of specifying the default argument for __? Also, no point in using double underscore, we should use _.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As a guideline I suggest using _ for one unused argument and *_ if there are many

def __init__(self, master: IMaster):
self._master = master

def __call__(self, signum, __=None):
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should be wary of using the __call__. It's meant to hide the state from the caller, which is unnecessary in this case. Generally, it's better to have

signal.signal(signal.SIGINT, stop_signal_handler.handle_signal)

because this way it's clear that signal.signal expects to get a function. As it is right now, it looks like it expects an object of class StopSignalHandler until you actually read the StopSignalHandler or signal.signal

VakarisZ and others added 6 commits November 24, 2021 10:52
Agent will now handle interrupt and break signals on linux and windows
The call to input() was used to pause the execution of the agent while
testing the new signal handlers. It is no longer needed.
…5s after CTRL_CLOSE_EVENT signal

The comment will warn us that in case that particular signal is raised, the cleanup shouldn't take longer than 5s
@VakarisZ VakarisZ force-pushed the 1594-signal-handlers branch from 91bb2ec to 3f7c4a8 Compare November 24, 2021 08:54
@VakarisZ VakarisZ merged commit 474e1ad into 1593-i-master Nov 24, 2021
@VakarisZ VakarisZ deleted the 1594-signal-handlers branch November 24, 2021 08:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Implement signal handlers
2 participants