<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Cortex XDR : Run Endpoint Script in Cortex XDR Discussions</title>
    <link>https://live.paloaltonetworks.com/t5/cortex-xdr-discussions/cortex-xdr-run-endpoint-script/m-p/568221#M5648</link>
    <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://live.paloaltonetworks.com/t5/user/viewprofilepage/user-id/171644"&gt;@jourEnMulticast&lt;/a&gt;, thanks for contacting us using the Live Community.&lt;/P&gt;
&lt;P&gt;Can you share the script that you are trying to run on the endpoints? Remove the confidential info from it if necessary.&lt;/P&gt;
&lt;P&gt;Thanks&lt;/P&gt;</description>
    <pubDate>Mon, 04 Dec 2023 14:44:35 GMT</pubDate>
    <dc:creator>jmazzeo</dc:creator>
    <dc:date>2023-12-04T14:44:35Z</dc:date>
    <item>
      <title>Cortex XDR : Run Endpoint Script</title>
      <link>https://live.paloaltonetworks.com/t5/cortex-xdr-discussions/cortex-xdr-run-endpoint-script/m-p/567253#M5610</link>
      <description>&lt;P&gt;Hello Everyone,&lt;/P&gt;
&lt;P&gt;I have an issue with the action "Run Endpoint Script".&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I want to trigger an executable file (.exe) through the "Run Endpoint Script" action from &lt;LI-PRODUCT title="Cortex XDR" id="Cortex_XDR"&gt;&lt;/LI-PRODUCT&gt;. This executable is used to uninstall a specific software (It is not installed with MSI file, the executable file is the only way to uninstall it). I want to use the endpoint script to get the job done in a massive way&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I tried several things (subprocess with commands on my own, got inspired from the Palo Alto default script to launch command, etc.). All of those, when launched on the host itself or from the Live Terminal in the Cortex XDR console worked, ALL.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But, my issue is, when launched from the "Run Endpoint Script" action, it just does not work and&amp;nbsp; the logfile 'cortex-xdr-payload' does not help.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;With subprocess, I tend to have the following command and arg (The path is also not an issue. I also tried to redirect the output to a file. It is well added to the correct path).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;'start /B myExecutableFile.exe /S' -&amp;gt; cmd style&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This executable file requires some elevated privileges but as we launch it through the "Run Endpoint Script" action, we should be OK, because it is launched in SYSTEM context.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;As input in the configuration of the script, I choose "run by entrypoint", my function named "run".&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you for your help !&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Antoine.&lt;/P&gt;
&lt;P style="margin: 0in; font-family: Calibri; font-size: 11.0pt;"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 27 Nov 2023 17:26:31 GMT</pubDate>
      <guid>https://live.paloaltonetworks.com/t5/cortex-xdr-discussions/cortex-xdr-run-endpoint-script/m-p/567253#M5610</guid>
      <dc:creator>jourEnMulticast</dc:creator>
      <dc:date>2023-11-27T17:26:31Z</dc:date>
    </item>
    <item>
      <title>Re: Cortex XDR : Run Endpoint Script</title>
      <link>https://live.paloaltonetworks.com/t5/cortex-xdr-discussions/cortex-xdr-run-endpoint-script/m-p/568221#M5648</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://live.paloaltonetworks.com/t5/user/viewprofilepage/user-id/171644"&gt;@jourEnMulticast&lt;/a&gt;, thanks for contacting us using the Live Community.&lt;/P&gt;
&lt;P&gt;Can you share the script that you are trying to run on the endpoints? Remove the confidential info from it if necessary.&lt;/P&gt;
&lt;P&gt;Thanks&lt;/P&gt;</description>
      <pubDate>Mon, 04 Dec 2023 14:44:35 GMT</pubDate>
      <guid>https://live.paloaltonetworks.com/t5/cortex-xdr-discussions/cortex-xdr-run-endpoint-script/m-p/568221#M5648</guid>
      <dc:creator>jmazzeo</dc:creator>
      <dc:date>2023-12-04T14:44:35Z</dc:date>
    </item>
    <item>
      <title>Re: Cortex XDR : Run Endpoint Script</title>
      <link>https://live.paloaltonetworks.com/t5/cortex-xdr-discussions/cortex-xdr-run-endpoint-script/m-p/568716#M5675</link>
      <description>&lt;P&gt;Hello ! &lt;BR /&gt;&lt;BR /&gt;The code is as follows. I took the liberty of getting some sample codes from default XDR script.&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;LI-CODE lang="python"&gt;import os
import subprocess
import traceback
import psutil
import shlex
import sys
import ctypes

def run():
    if psutil.WINDOWS:
        return run_commands(remove_Software())

def run_commands(os_commands):
    print("op")
    result = dict()

    if psutil.WINDOWS:
        shell_encoding = "cp" + repr(ctypes.windll.kernel32.GetConsoleOutputCP())
    else:
        shell_encoding = "utf-8"

    sys.stdout.write(f"shell_encoding={shell_encoding}\n")

    for details, commands in os_commands.items():
        result[details] = list()
        for command in commands:
            if psutil.WINDOWS:
                args = shlex.split(command, posix=False)
                print(args)
            else:
                # In POSIX env, to run in the context of shell, we pass the command as argument
                # e.g. the process to be execute will be: "/bin/sh -c {command}"
                args = [command]
            try:
                sys.stdout.write(f"Running command &amp;lt;{command}&amp;gt;\n")
                print(os.getcwd())
                with subprocess.Popen(args=args, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
                                      encoding=shell_encoding, text=True, cwd=r"C:\Program Files (x86)\path_software_to_uninstall") as process:
                    stdout_data, stderr_data = process.communicate()
                    if stderr_data:
                        sys.stderr.write(f"stderr: \n{stderr_data}\n")
                    if stdout_data:
                        sys.stdout.write(f"stdout: \n{stdout_data}\n")
                        result[details].extend(stdout_data.splitlines())
            except Exception:
                sys.stderr.write(f"Failed open command: &amp;lt;{command}&amp;gt;, error: {traceback.format_exc()}")
        if len(result[details]) == 0:
            result[details] = None

    return result

def remove_Software():
    return {"remove_Software":["start /B uninst.exe /S &amp;gt; test.log"]}&lt;/LI-CODE&gt;
&lt;P&gt;&lt;BR /&gt;Thanks !&lt;BR /&gt;&lt;BR /&gt;Antoine.&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 06 Dec 2023 17:44:27 GMT</pubDate>
      <guid>https://live.paloaltonetworks.com/t5/cortex-xdr-discussions/cortex-xdr-run-endpoint-script/m-p/568716#M5675</guid>
      <dc:creator>jourEnMulticast</dc:creator>
      <dc:date>2023-12-06T17:44:27Z</dc:date>
    </item>
  </channel>
</rss>

