First Endpoint Telemetry: Elastic Agent and Sysmon

First Endpoint Telemetry: Elastic Agent and Sysmon

Enrolling win11-01 into Fleet with a fail-closed install script, getting Sysmon and Windows event logs into Elastic, and the five problems between “Healthy” and actual data.

The endpoint exists and the SIEM is listening. Time to introduce them.

Every alert a SOC analyst works starts out as a log on some machine. On Windows, the best of those come from two places: the built-in event logs, and Sysmon, a free Sysinternals tool that records process launches, network connections, file and registry changes, and DNS lookups in far more detail than Windows does on its own.

So that’s what we’re building here: Elastic Agent on win11-01 (cloned from the golden image), enrolled through the Fleet Server from last time, shipping Sysmon plus the Security, System, Application and PowerShell logs into Elastic, with Elastic’s prebuilt detection rules running against all of it.

The plan was one script and one run. Fleet called the agent Healthy for a long time before a single event actually showed up.

Why log collection only?

Elastic Agent can also run Elastic Defend, Elastic’s own EDR. I’m not using it on Windows. Elastic Defend registers itself as the Windows antivirus, which pushes Microsoft Defender into passive mode, and later in this project Microsoft Defender for Endpoint is going to be the EDR on these machines.

Real SOCs run one EDR per endpoint. Here that’s MDE, and Elastic’s job is to be the SIEM. So the agent on Windows collects logs and nothing else.

Environment Overview

  • win11-01: The first clone of the golden image, sitting on the Endpoints VLAN.
  • Elastic 9.5.5: Both the stack and the agents. It started out on 9.1.0, and I’ll get to why it didn’t stay there.
  • Windows Endpoints agent policy: Declared in elk/kibana.yml like the Fleet Server policy, so it gets rebuilt along with the stack and is read-only in the UI. Nobody can click it out of sync with the repo.
  • Integrations: system for the Security, System and Application logs plus host metrics, and windows for the Sysmon, PowerShell and Defender channels.
  • Sysmon with sysmon-modular: Sysmon on its own logs nothing useful until you give it a config. sysmon-modular is community-maintained and mapped to MITRE ATT&CK, and I went with its balanced profile so a 150 GB disk doesn’t fill up in a week.
  • Install-LabAgent.ps1: One elevated run per endpoint. I’ll reuse it for every clone after this, and for dc01.

NOTE: The firewall rules from the last post already cover this. The Endpoints VLAN can reach elk01 on TCP 8220 (Fleet Server) and 9200 (Elasticsearch), and nothing else on the Servers VLAN.

Part 1: An install script that fails closed

Before writing the script, I asked the same question as last time: if this went wrong quietly, what would it cost me? It runs as admin, installs things that run as SYSTEM, and tells the agent which certificate authority to trust. A quiet failure means an endpoint I think is monitored and isn’t, or one that trusts an attacker’s server. So the rule was simple: if anything looks off, stop.

ControlHowWhat it stops
Verified downloadsThe agent zip is checked against Elastic’s published SHA-512, Sysmon64.exe must carry a valid Microsoft Authenticode signature, and the Sysmon config is checked against its release’s SHA256SUMSRunning a tampered or truncated binary as SYSTEM
Pinned versionsAgent version and sysmon-modular release are parameters with fixed defaultsTwo endpoints quietly running different configs, so the same detection behaves differently
CA in an admin-only folderca.crt goes to C:\Program Files\LabCA\A standard user swapping in their own CA
Token kept out of the logsAgent installs before Sysmon, and the token is revoked after useThe enrollment token ending up in the SIEM
No secrets in the repoFQDN, token and CA path are required parameters 
Exit codes checked$LASTEXITCODE after every native commandA half-finished install that reports success

That last row is easy to miss. $ErrorActionPreference = 'Stop' only covers PowerShell cmdlets. When a native .exe like elastic-agent.exe fails, PowerShell carries right on to the next line unless you check $LASTEXITCODE yourself.

Two of those rows exist because I got them wrong first. More on that in Part 3.

Part 2: The build

Policy, CA and token

Copy the updated kibana.yml to elk01 and restart Kibana, and Fleet creates the Windows Endpoints policy on its own. Then grab the CA from the stack:

docker compose cp es01:/usr/share/elasticsearch/config/certs/ca/ca.crt .

And in Kibana, Fleet → Enrollment tokens, create a token for Windows Endpoints.

Run the installer

Copy the script and ca.crt to the endpoint, then from an elevated PowerShell:

Set-ExecutionPolicy -Scope Process Bypass
C:\Temp\Install-LabAgent.ps1 -FleetUrl https://<elk01 FQDN>:8220 -EnrollmentToken <token> -CaCert C:\Temp\ca.crt

It enrolls the agent, then installs Sysmon with the sysmon-modular config (or just updates the config if Sysmon’s already there). Afterwards, revoke the token and delete the files from C:\Temp.

Turn on the right detection rules

Elastic ships over 2,000 prebuilt rules. Turning them all on would burn a 2 GB heap on rules for data I don’t have yet: Elastic Defend, cloud providers, Okta. So I installed them all and enabled only the ones tagged for the two sources this endpoint actually sends:

POST kbn:/api/detection_engine/rules/_bulk_action
{
  "action": "enable",
  "query": "alert.attributes.tags:(\"Data Source: Sysmon\" OR \"Data Source: Windows Security Event Logs\")"
}

That came to 155 of 2,059. As more sources come online, the same query with more tags turns on more rules.

Detection rules filtered by the Sysmon and Windows Security data source tags, 155 rules all enabled 155 rules, each one with data to run against.

Part 3: Everything that went wrong

Problem 1: The Sysmon config was a 404

The script’s first run died downloading sysmonconfig.xml from the master branch of sysmon-modular. The project doesn’t commit its built configs anymore. They’re published as GitHub Release assets now, with a SHA256SUMS file next to them.

This one turned out to be a favor. Pulling from master meant every endpoint could get a different config depending on when it was installed. Now the script downloads a pinned release and checks it against the published hash. Same config everywhere, and I know exactly which one.

Problem 2: The CA was somewhere users could write

I caught this one reviewing the script, before it caused any trouble. The first version copied the CA to C:\ProgramData\LabCA\. ProgramData sounds like an admin location, but standard users can create folders in it. A user could create LabCA first, own it, and swap in their own ca.crt later. The agent would then trust whatever server that CA vouched for, including a fake Fleet Server handing out policies.

The fix was moving it to C:\Program Files\LabCA\, which only admins and SYSTEM can write to. The catch is the agent saves the CA’s path when it enrolls, so moving it means uninstalling the agent and enrolling it again.

Problem 3: Sysmon logged the enrollment token

The first version installed Sysmon first and the agent second. Sysmon did exactly what it’s built to do and logged the new process, command line and all:

elastic-agent.exe install ... --enrollment-token <token>

Then the agent came up and shipped that event straight into Elasticsearch. Enrollment tokens can be reused until they’re revoked, so anyone who could read logs in the SIEM could enroll their own machine into my fleet.

The fix is to install the agent first, and always revoke the token afterwards. Reordering alone isn’t enough: re-enrolling a host that already has Sysmon logs the token again, and so will Security event 4688 once dc01’s audit policy starts recording command lines.

Treat anything typed on a command line as something that will end up in the SIEM. Sysmon Event 1 and Security event 4688 record full command lines, so tokens, passwords and API keys passed as arguments get logged right along with everything else.

Problem 4: “Healthy”, but no data

Fleet showed the agent as Healthy. Elasticsearch had nothing. Not one index, not even Fleet Server’s own logs. The agent’s logs had the answer:

The remote server's certificate is presented without its certificate chain. Using 'ca_trusted_fingerprint'
requires that the server presents a certificate chain that includes the certificate's issuing certificate authority.
x509: certificate signed by unknown authority

“Healthy” only means the agent can talk to Fleet Server on 8220. The data goes somewhere else: straight to Elasticsearch on 9200, over its own TLS connection. That connection checks the CA by fingerprint, which works by comparing the pinned hash to the certificates the server hands over. Elasticsearch was only handing over its own certificate, not the CA above it, so there was nothing to compare against.

The fix was in the certs job from the last post. It now also writes es01-chain.crt (the server cert plus the CA), and Elasticsearch serves that instead. The CA itself didn’t change, so the fingerprint already in Fleet still worked. To check it:

openssl s_client -connect localhost:9200 -showcerts </dev/null | grep -c 'BEGIN CERTIFICATE'

That should print 2. Before the fix, it printed 1.

Problem 5: Event logs stuck on “Starting”

Now metrics were arriving, but still no Sysmon or Security events. elastic-agent status showed every Windows event log input sitting at Starting to read from … and never moving. Digging into the agent logs:

Input crashed with: input panic with: name winlog-windows_sysmon_operational-windows-windows-endpoints already used

I spent a while assuming I’d broken the policy with duplicate stream IDs. I hadn’t. Every ID was unique. This was a bug in Elastic Agent 9.1.0: each event log input tried to register its metrics under a name that was already taken, crashed, and never started reading. Restarting the agent just crashed it the same way again.

The fix was upgrading the stack and the agents to 9.5.5: bump STACK_VERSION in .env, then Fleet → Upgrade agent, Fleet Server first. One last trap along the way: Elastic’s version API listed 9.5.6, but the images and downloads for it weren’t published yet, so the pull just failed. Check that a version actually exists in the registry before pinning it.

Part 4: Verify

  • Done: Fleet shows win11-01 and fleet-server both Healthy on 9.5.5.
  • Done: elastic-agent status shows every event log input HEALTHY.
  • Done: elastic-agent inspect shows the CA path as C:\Program Files\LabCA\ca.crt.
  • Done: Discover returns events for both windows.sysmon_operational and system.security.
  • Done: Enrollment token revoked, the stale agent entry unenrolled, and the leftover files removed.
  • Done: 155 prebuilt rules enabled.

Fleet agents list showing win11-01 on Windows Endpoints and fleet-server on Fleet Server Policy, both Healthy on 9.5.5 Two agents now, and both of them mean it this time.

Discover showing 257 windows.sysmon_operational events from win11-01 over the last 15 minutes 257 Sysmon events in 15 minutes from a machine that’s mostly sitting idle.

Loose ends

Data’s flowing and the rules are on, but there’s still a list:

  • Nothing has alerted yet. The pipeline is proven up to “rules are running”, not “rules catch things”. The Atomic Red Team baseline is what closes that gap.
  • The rules aren’t tuned. 155 rules on default settings will throw false positives once the endpoint gets real use. Each tuning change will get logged per rule.
  • I haven’t checked rule health. Kibana’s Rule Monitoring page still needs a look for rules that are failing to run.
  • Some streams do nothing. The default policy also turns on ForwardedEvents and two Linux log-file inputs that collect nothing on Windows. Harmless for now, easy to remove from kibana.yml if they get noisy.
  • Some events got ingested twice. Re-enrolling reset the agent’s read position. The duplicates age out with the 30-day retention.
  • Sysmon’s version isn’t pinned. Sysinternals only publishes the latest build. The signature check proves it’s Microsoft’s, but not which version.
  • No snapshot before the upgrade. Fine with one day of data in the stack. It won’t be fine later.
  • Installing by hand. One script run per endpoint is fine for one endpoint. Intune should take this over in Month 2, deploying the agent and Sysmon as Win32 apps.

Wrapping up

The SOC has its first set of eyes. win11-01 is sending process, network, DNS and logon activity into Elastic, 155 rules are watching it, and the script that set it all up refuses to run anything it can’t verify.

The biggest lesson from this one: “Healthy” in Fleet doesn’t mean data is arriving. Always check Discover. Next up is dc01, a domain controller with AD DS, AD CS and DNS on Server Core, so the lab finally has a domain to attack and defend.