-
Notifications
You must be signed in to change notification settings - Fork 8
[docs] Palo Alto Cortex executor (#256) #251
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
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
2c0ee63
[docs] Palo Alto Cortex executor
damgouj 8d9121c
[docs] Gael comments
damgouj 1de6e01
[docs] Gael comments
damgouj a4404ef
[docs] Gael comments
damgouj ed99db3
[docs] feat(executors): add documentation on Palo Alto Cortex executo…
impolitepanda File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+84.2 KB
docs/deployment/assets/paloaltocortex-windows-script-inputs-outputs.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| import os | ||
|
|
||
| def run(command): | ||
| exit_code = os.system("echo " + command + " | base64 -d | sh") | ||
| print("Exit code:", exit_code) | ||
|
|
||
| if __name__ == "__main__": | ||
| run(command) |
155 changes: 155 additions & 0 deletions
155
docs/deployment/assets/paloaltocortex_subprocessor_windows.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,155 @@ | ||
| import os | ||
| import subprocess | ||
| import sys | ||
| import traceback | ||
| import ctypes | ||
| import re | ||
|
|
||
| PtyProcess = None | ||
| try: | ||
| from winpty import PtyProcess | ||
| except: | ||
| PtyProcess = None | ||
|
|
||
|
|
||
| def ensure_console(): | ||
| """Ensure the current process has a valid console. | ||
|
|
||
| When launched from Palo Alto's agent, the process may not have | ||
| a console attached, which causes child processes (especially cmd.exe) | ||
| to fail when they try to inherit console handles. | ||
| """ | ||
| kernel32 = ctypes.windll.kernel32 | ||
| if not kernel32.AttachConsole(-1): | ||
| kernel32.AllocConsole() | ||
| hwnd = kernel32.GetConsoleWindow() | ||
| if hwnd: | ||
| ctypes.windll.user32.ShowWindow(hwnd, 0) | ||
|
|
||
|
|
||
| def remove_control_characters(text): | ||
| """Remove all the control characters from a string""" | ||
| text = re.sub(r"\x1b\[\??(\d;?)*\w", '', text) | ||
| text = re.sub(r"\x1b\]0;.*\x07", '\r\n', text) | ||
| text = re.sub("\r\n>> ", '', text) | ||
| return text | ||
|
|
||
|
|
||
| def remove_leading_cmd(text): | ||
| """Removes leading "cmd" or "cmd.exe" from the text if present. | ||
|
|
||
| :param text: a string | ||
| :type text: str | ||
| :rtype: str | ||
| """ | ||
| if text.startswith("cmd.exe"): | ||
| return text[7:] | ||
| elif text.startswith("cmd"): | ||
| return text[3:] | ||
| return text | ||
|
|
||
|
|
||
| def is_direct_executable(command): | ||
| """Check if the command starts with a known executable.""" | ||
| first_token = command.strip().split()[0].lower() | ||
| return first_token in [ | ||
| "powershell.exe", "powershell", | ||
| "cmd.exe", "cmd", | ||
| "pwsh.exe", "pwsh" | ||
| ] | ||
|
|
||
|
|
||
| def run(commands_list): | ||
| """Runs a list of commands via winpty, falling back to subprocess. | ||
|
|
||
| :param commands_list: list of commands | ||
| :type commands_list: list | ||
| :return: commands output dict - {<command>: <output>} | ||
| :rtype: dict | ||
| """ | ||
| ensure_console() | ||
|
|
||
| result = {} | ||
| for command in commands_list: | ||
| sys.stdout.write(f"Running command <{command[:100]}>...\n") | ||
| args = command | ||
| try: | ||
| env = os.environ.copy() | ||
|
|
||
| if 'COMSPEC' not in env: | ||
| env['COMSPEC'] = os.path.expandvars("%SYSTEMROOT%\\System32\\cmd.exe") | ||
| if 'SystemRoot' not in env: | ||
| env['SystemRoot'] = os.environ.get('SystemRoot', 'C:\\Windows') | ||
|
|
||
| if is_direct_executable(command): | ||
| # Direct executable call (e.g., powershell.exe -encodedCommand ...) | ||
| # Run without shell wrapping or winpty | ||
| with subprocess.Popen( | ||
| args=args, | ||
| shell=False, | ||
| stdout=subprocess.PIPE, | ||
| stderr=subprocess.PIPE, | ||
| env=env, | ||
| ) as process: | ||
| stdout, stderr = process.communicate(timeout=300) | ||
| if stderr: | ||
| sys.stderr.write(f"stderr: \n{stderr.decode('utf-8', errors='replace')}\n") | ||
| if stdout: | ||
| sys.stdout.write(f"stdout: \n{stdout.decode('utf-8', errors='replace')}\n") | ||
| result[command] = stdout.decode('utf-8', errors='replace').splitlines() | ||
| else: | ||
| result[command] = None | ||
| elif PtyProcess: | ||
| command = remove_leading_cmd(command) | ||
| command_prompt_path = os.path.expandvars("%SYSTEMROOT%\\System32\\cmd.exe") | ||
| proc = PtyProcess.spawn(command_prompt_path, cwd="c:\\") | ||
| proc.write("cls\r\n") | ||
| proc.read() | ||
| proc.write(command + '\r\n') | ||
| proc.write('exit\r\n') | ||
| stdout = "" | ||
| while proc.isalive(): | ||
| buff = proc.read(32768) | ||
| stdout += buff | ||
| if proc.eof(): | ||
| break | ||
| sys.stdout.write(f"stdout: {stdout}") | ||
| stdout = remove_control_characters(stdout) | ||
| stdout = stdout.split(command, 1)[1] | ||
| stdout = stdout.split(":\\>exit", 1)[0][:-1] | ||
| lines = stdout.split("\r\n") | ||
| lines = [line.strip() for line in lines if line.strip()] | ||
| result[command] = lines | ||
| proc.terminate() | ||
| else: | ||
| sys.stderr.write( | ||
| "WARNING: winpty not available, falling back to subprocess\n" | ||
| ) | ||
| with subprocess.Popen( | ||
| args=args, | ||
| shell=True, | ||
| stdin=subprocess.PIPE, | ||
| stdout=subprocess.PIPE, | ||
| stderr=subprocess.PIPE, | ||
| env=env, | ||
| creationflags=subprocess.CREATE_NEW_CONSOLE, | ||
| ) as process: | ||
| stdout, stderr = process.communicate(timeout=300) | ||
| if stderr: | ||
| sys.stderr.write(f"stderr: \n{stderr.decode('utf-8', errors='replace')}\n") | ||
| if stdout: | ||
| sys.stdout.write(f"stdout: \n{stdout.decode('utf-8', errors='replace')}\n") | ||
| result[command] = stdout.decode('utf-8', errors='replace').splitlines() | ||
| else: | ||
| result[command] = None | ||
| except subprocess.TimeoutExpired: | ||
| sys.stderr.write(f"Command timed out: <{command[:100]}>...\n") | ||
| result[command] = None | ||
| except Exception: | ||
| sys.stderr.write(f"Failed open command: <{command[:100]}>, error: {traceback.format_exc()}") | ||
|
|
||
| return result | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| results = run(commands_list) |
File renamed without changes.
Dimfacion marked this conversation as resolved.
Show resolved
Hide resolved
|
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.