---
title: Using an ESA Rule to run a command on a windows machine
description: A colleague here at RSA posed an interesting problem so I thought I would share with you how I solved it.Imagine the following scenario. A windows mac
---

<https://mydev.netwitness.com/netwitness-security-llc-blog>

# [Using an ESA Rule to run a command on a windows machine](https://mydev.netwitness.com/netwitness-security-llc-blog/usinganesaruletorunacommandonawindowsmachine)

 Written by [DavidWaugh1](https://mydev.netwitness.com/netwitness-security-llc-blog/author/davidwaugh1) | Dec 22, 2016, 5:00:00 AM

A colleague here at RSA posed an interesting problem so I thought I would share with you how I solved it.

Imagine the following scenario. A windows machine has visited a potentially suspicious website. This is detected by an ESA rule and an incident would be created. Normal SOC procedures say that when such an incident happens the ECAT Agent should be installed on the windows machine so that further analysis can take place. It is a busy day in the SOC and unfortunately there is a long time gap between when the agent is actually installed. Can this actually be automated?

The answer is yes and here is how....

First of all, I have a centos 6 server where I will run the commands from. This is to avoid installing any extra packages on the ESA Server that could potentially cause problems.

On the centos 6 server I have installed the openvas-smb package. This provides the winexe linux command.

This is a tool that allows you to run psexec type commands from a linux machine.

\[root@centos6 ~\]# yum install wmi  
Loaded plugins: fastestmirror, refresh-packagekit  
Setting up Install Process  
Loading mirror speeds from cached hostfile  
 \* atomic: mirror1.34sp.com  
 \* base: mirror.steadfast.net  
 \* centosplus: mirror.steadfast.net  
 \* contrib: mirror.steadfast.net  
 \* epel: mirror.ibcp.fr  
 \* extras: mirror.steadfast.net  
 \* updates: mirror.steadfast.net  
Package wmi is obsoleted by openvas-smb, trying to install openvas-smb-1.0.1-1.el6.art.x86\_64 instead  
Resolving Dependencies  
--\> Running transaction check  
---\> Package openvas-smb.x86\_64 0:1.0.1-1.el6.art will be installed  
--\> Processing Dependency: libroken.so.18(HEIMDAL\_ROKEN\_1.0)(64bit) for package: openvas-smb-1.0.1-1.el6.art.x86\_64  
--\> Processing Dependency: libkrb5.so.26(HEIMDAL\_KRB5\_2.0)(64bit) for package: openvas-smb-1.0.1-1.el6.art.x86\_64  
--\> Processing Dependency: libhdb.so.9(HEIMDAL\_HDB\_1.0)(64bit) for package: openvas-smb-1.0.1-1.el6.art.x86\_64  
--\> Processing Dependency: libgssapi.so.3(HEIMDAL\_GSS\_2.0)(64bit) for package: openvas-smb-1.0.1-1.el6.art.x86\_64  
--\> Processing Dependency: libgnutls.so.28(GNUTLS\_1\_4)(64bit) for package: openvas-smb-1.0.1-1.el6.art.x86\_64  
--\> Processing Dependency: libasn1.so.8(HEIMDAL\_ASN1\_1.0)(64bit) for package: openvas-smb-1.0.1-1.el6.art.x86\_64  
--\> Processing Dependency: atomic-gnutls3-gnutls for package: openvas-smb-1.0.1-1.el6.art.x86\_64  
--\> Processing Dependency: atomic-glib2-glib2 for package: openvas-smb-1.0.1-1.el6.art.x86\_64  
--\> Processing Dependency: libwind.so.0()(64bit) for package: openvas-smb-1.0.1-1.el6.art.x86\_64  
--\> Processing Dependency: libroken.so.18()(64bit) for package: openvas-smb-1.0.1-1.el6.art.x86\_64  
--\> Processing Dependency: libkrb5.so.26()(64bit) for package: openvas-smb-1.0.1-1.el6.art.x86\_64  
--\> Processing Dependency: libhx509.so.5()(64bit) for package: openvas-smb-1.0.1-1.el6.art.x86\_64  
--\> Processing Dependency: libheimntlm.so.0()(64bit) for package: openvas-smb-1.0.1-1.el6.art.x86\_64  
--\> Processing Dependency: libhdb.so.9()(64bit) for package: openvas-smb-1.0.1-1.el6.art.x86\_64  
--\> Processing Dependency: libhcrypto.so.4()(64bit) for package: openvas-smb-1.0.1-1.el6.art.x86\_64  
--\> Processing Dependency: libgssapi.so.3()(64bit) for package: openvas-smb-1.0.1-1.el6.art.x86\_64  
--\> Processing Dependency: libgnutls.so.28()(64bit) for package: openvas-smb-1.0.1-1.el6.art.x86\_64  
--\> Processing Dependency: libasn1.so.8()(64bit) for package: openvas-smb-1.0.1-1.el6.art.x86\_64  
--\> Running transaction check  
---\> Package atomic-glib2-glib2.x86\_64 0:2.41.0-2.el6.art will be installed  
---\> Package atomic-gnutls3-gnutls.x86\_64 1:3.1.25-1.el6.art will be installed  
--\> Processing Dependency: libnettle.so.4()(64bit) for package: 1:atomic-gnutls3-gnutls-3.1.25-1.el6.art.x86\_64  
--\> Processing Dependency: libhogweed.so.2()(64bit) for package: 1:atomic-gnutls3-gnutls-3.1.25-1.el6.art.x86\_64  
---\> Package heimdal-libs.x86\_64 0:1.6.0-0.9.20140621gita5adc06.el6 will be installed  
--\> Running transaction check  
---\> Package nettle.x86\_64 0:2.7.1-3.el6.art will be installed  
--\> Finished Dependency Resolution

Dependencies Resolved

\============================================================================================================================================================  
 Package Arch Version Repository Size  
\============================================================================================================================================================  
Installing:  
 openvas-smb x86\_64 1.0.1-1.el6.art atomic 3.3 M  
Installing for dependencies:  
 atomic-glib2-glib2 x86\_64 2.41.0-2.el6.art atomic 2.2 M  
 atomic-gnutls3-gnutls x86\_64 1:3.1.25-1.el6.art atomic 610 k  
 heimdal-libs x86\_64 1.6.0-0.9.20140621gita5adc06.el6 epel 1.0 M  
 nettle x86\_64 2.7.1-3.el6.art atomic 307 k

Transaction Summary  
\============================================================================================================================================================  
Install 5 Package(s)

Total download size: 7.4 M  
Installed size: 28 M  
Is this ok \[y/N\]: y  
Downloading Packages:  
(1/5): atomic-glib2-glib2-2.41.0-2.el6.art.x86\_64.rpm | 2.2 MB 00:02  
(2/5): atomic-gnutls3-gnutls-3.1.25-1.el6.art.x86\_64.rpm | 610 kB 00:00  
(3/5): heimdal-libs-1.6.0-0.9.20140621gita5adc06.el6.x86\_64.rpm | 1.0 MB 00:01  
(4/5): nettle-2.7.1-3.el6.art.x86\_64.rpm | 307 kB 00:00  
(5/5): openvas-smb-1.0.1-1.el6.art.x86\_64.rpm | 3.3 MB 00:01  
\------------------------------------------------------------------------------------------------------------------------------------------------------------  
Total 1.1 MB/s | 7.4 MB 00:06  
Running rpm\_check\_debug  
Running Transaction Test  
Transaction Test Succeeded  
Running Transaction  
Warning: RPMDB altered outside of yum.  
 Installing : atomic-glib2-glib2-2.41.0-2.el6.art.x86\_64 1/5  
 Installing : heimdal-libs-1.6.0-0.9.20140621gita5adc06.el6.x86\_64 2/5  
 Installing : nettle-2.7.1-3.el6.art.x86\_64 3/5  
 Installing : 1:atomic-gnutls3-gnutls-3.1.25-1.el6.art.x86\_64 4/5  
 Installing : openvas-smb-1.0.1-1.el6.art.x86\_64 5/5  
 Verifying : nettle-2.7.1-3.el6.art.x86\_64 1/5  
 Verifying : heimdal-libs-1.6.0-0.9.20140621gita5adc06.el6.x86\_64 2/5  
 Verifying : openvas-smb-1.0.1-1.el6.art.x86\_64 3/5  
 Verifying : atomic-glib2-glib2-2.41.0-2.el6.art.x86\_64 4/5  
 Verifying : 1:atomic-gnutls3-gnutls-3.1.25-1.el6.art.x86\_64 5/5

Installed:  
 openvas-smb.x86\_64 0:1.0.1-1.el6.art

Dependency Installed:  
 atomic-glib2-glib2.x86\_64 0:2.41.0-2.el6.art atomic-gnutls3-gnutls.x86\_64 1:3.1.25-1.el6.art heimdal-libs.x86\_64 0:1.6.0-0.9.20140621gita5adc06.el6  
 nettle.x86\_64 0:2.7.1-3.el6.art

Complete

After installing the package I then created a file called credentials.cfg containing the account I would use to run the commands on my windows target machine.

username=administrator  
password=Twof0rt.  
domain=WAUGH

I was then able to run command from my centos 6 server to a windows machine as follows:

root@centos6 ~\]# winexe -A credentials.cfg //192.168.123.13 'cmd.exe /c echo "hello"'  
"hello"  
You have mail in /var/spool/mail/root  
\[root@centos6 ~\]# winexe -A credentials.cfg //192.168.123.13 'hostname'  
DWAUGH03

This now gives us the capability of running commands from our Centos 6 machine onto a windows machine.

The next part of the puzzle was to allow the ESA Server to be able to connect to our Centos 6 Server to be able to run commands. We set up passwordless SSH keys to do this. The important thing here is that when scripts run on the ESA Server, they are run under the user "notification" so we have to set this up as follows:

SSH onto the ESA Server as root, then type sudo su notification

This switches us to the user notification.

\[notification@rsaesa tmp\]$ whoami  
notification  
\[notification@rsaesa tmp\]$ cd ~  
\[notification@rsaesa .ssh\]$ ssh-keygen  
Generating public/private rsa key pair.  
Enter file in which to save the key (/home/notification/.ssh/id\_rsa):  
Enter passphrase (empty for no passphrase):  
Enter same passphrase again:  
Your identification has been saved in /home/notification/.ssh/id\_rsa.  
Your public key has been saved in /home/notification/.ssh/id\_rsa.pub.  
The key fingerprint is:  
d9:ed:61:48:db:c1:aa:75:d5:b7:64:3f:ac:1c:c4:2b notification@rsaesa  
The key's randomart image is:  
+--\[ RSA 2048\]----+  
| |  
| .. . |  
| . oo.oo|  
| + \*.o= +|  
| S \*E\*o +.|  
| o +o.o .|  
| . .o |  
| |  
| |  
+-----------------+  
\[notification@rsaesa .ssh\]$ ssh-copy-id root@centos6.waugh.local  
root@centos6.waugh.local's password:  
Now try logging into the machine, with "ssh 'root@centos6.waugh.local'", and check in:

.ssh/authorized\_keys

to make sure we haven't added extra keys that you weren't expecting.

\[notification@rsaesa .ssh\]$ ssh root@centos6.waugh.local "winexe -A credentials.cfg //192.168.123.233 'hostname'"  
WIN732  
\[notification@rsaesa .ssh\]$ exit

Here we can see that we can run a command on the ESA Server, which will run a command on our Centos 6 Server which will then actually run a command on our target windows machine.

We now define our ESA Rule. My ESA Rule fires whenever a suspiciousIP is detected.

My ESA Script is as follows (Attached as file ESAScript to import into the Global Notifications Page). Note this is python so indentations matter. Please import the file into your system to see the format of the script.

#!/usr/bin/env python  
from smtplib import SMTP  
import subprocess  
import datetime  
import json  
import sys

def dispatch(alert):  
 """  
 The default dispatch just prints the 'last' alert to /tmp/esa\_alert.json. Alert details  
 are available in the Python hash passed to this method e.g. alert\['id'\], alert\['severity'\],  
 alert\['module\_name'\], alert\['events'\]\[0\], etc.  
 These can be used to implement the external integration required.  
 """  
 with open("/tmp/esa\_alert.json", mode='w') as alert\_file:  
 alert\_file.write(json.dumps(alert, indent=True))

def read():  
 \# Define our metakey tuples  
 \# Each tuple is \["metakey","meta key description, "value"\]  
 metakeys=\[ \["device\_type","Device Type: ",""\], \\  
 \["device\_ip","Device IP: ",""\] , \\  
 \["device\_class","Device Class: ",""\] , \\  
 \["ip\_src","Source IP: ",""\] , \\  
 \["ip\_dst","Dest IP: ",""\] , \\  
 \["ip\_srcport","Source Port: ",""\], \\  
 \["ip\_dstport","Dest Port: ",""\] , \\  
 \["ec\_activity","Activity: ",""\] ,\\  
 \["ec\_subject","Subject: ",""\] ,\\  
 \["ec\_theme", "Theme: ",""\] ,\\  
 \["event\_description", "Description: ",""\] ,\\  
 \["event\_cat\_name","Category: ",""\] ,\\  
 \["msg\_id", "Message ID: ",""\] ,\\  
 \["event\_source\_id", "Concentrator: ",""\] \\  
 \]

\# Keys we want in our Subject  
 subj\_metakeys=\[ \["device\_type","Device Type: ","" \] , \\  
 \["msg\_id", "Message ID: ",""\] \]

 #Parameter  
 sa\_server = '192.168.123.4'  
 brokerid = '6'  
 smtp\_server = '192.168.123.27'  
 smtp\_port = '25'  
 smtp\_user = ''  
 smtp\_pass = ''  
 from\_addr = "RSA Security Analytics \<[RSA@SecurityAnalytics.com](mailto:RSA@SecurityAnalytics.com)\>"  
 to\_addr = \[['securityanalytics@waugh.com](mailto:'securityanalytics@waugh.com)'\]

\# Get data from JSON  
 esa\_alert = json.loads(open('/tmp/esa\_alert.json').read())  
 #Extract Variables (Add as required)

try:  
 module\_name = esa\_alert\["module\_name"\]  
 except KeyError:  
 module\_name = "null"

try:  
 sessionid = str(esa\_alert\["events"\]\[0\]\["sessionid"\])  
 except KeyError:  
 sessionid = "null"  
 try:  
 ip\_src = str(esa\_alert\["events"\]\[0\]\["ip\_src"\])  
 except KeyError:  
 ip\_src = "null"

 #Extract Values for each of our Variables  
 for tuples in metakeys:  
 try:  
 tuples\[2\] = str(esa\_alert\["events"\]\[0\]\[tuples\[0\]\])  
 except KeyError:  
 tuples\[2\] = "null"

#Extract Our Subject Variables  
 for tuples in subj\_metakeys:  
 try:  
 tuples\[2\] = str(esa\_alert\["events"\]\[0\]\[tuples\[0\]\])  
 except KeyError:  
 tuples\[2\] = "null"

try:  
 event\_source\_id =esa\_alert\["events"\]\[0\]\["event\_source\_id"\]  
 except KeyError:  
 event\_source\_id = "null"

#Work out Concentrator ID depending on Event Source  
 if event\_source\_id.startswith( "192.168.123.240" 😞  
 concid=231  
 elif event\_source\_id.startswith( "192.168.123.6" 😞  
 concid=267  
 elif event\_source\_id.startswith( "CONC3" 😞  
 concid=70  
 elif event\_source\_id.startswith( "CONC4" 😞  
 concid=90  
 else:  
 concid=0

 \# Sends Email  
 smtp = SMTP()  
 smtp.set\_debuglevel(0)  
 smtp.connect(smtp\_server,smtp\_port)  
 #smtp.login(smtp\_user,smtp\_pass)

\# Runs command  
 mycommand = "ssh root@centos6.waugh.local \\"winexe -A credentials.cfg //" + ip\_src + " \\'hostname\\'\\""  
 myresult = ""

\# Runs a System Command  
 p = subprocess.Popen(mycommand,shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)  
 for line in p.stdout.readlines():  
 myresult = myresult + line + "\\n",

[View full post](https://mydev.netwitness.com/netwitness-security-llc-blog/usinganesaruletorunacommandonawindowsmachine)

```json
{
  "@context" : "http://schema.org",
  "@type" : "BlogPosting",
  "author" : {
    "@type" : "Person",
    "name" : "DavidWaugh1"
  },
  "dateModified" : "2026-06-18T14:36:21.730Z",
  "datePublished" : "2016-12-22T05:00:00Z",
  "headline" : "Using an ESA Rule to run a command on a windows machine",
  "image" : {
    "@type" : "ImageObject",
    "height" : 60,
    "url" : "/hs/hsstatic/content_shared_assets/static-1.4092/img/default-amp-logo.png",
    "width" : 60
  },
  "mainEntityOfPage" : "https://mydev.netwitness.com/netwitness-security-llc-blog/usinganesaruletorunacommandonawindowsmachine",
  "publisher" : {
    "@type" : "Organization",
    "logo" : {
      "@type" : "ImageObject",
      "height" : 60,
      "url" : "/hs/hsstatic/content_shared_assets/static-1.4092/img/default-amp-logo.png",
      "width" : 60
    },
    "name" : "NetWitness Blogs"
  }
}
```