When an incident happens, the first question is always the same: what do the logs show? That is when many organizations discover that the log they needed wasn't enabled, or was in a region nobody covered, or had already expired. That is why cloud security logs need to be planned before the incident. There is no such thing as retroactive collection: what wasn't recorded at the time is lost.
In AWS environments, a few log sources answer most of the questions in an investigation: who acted, from where, what they accessed and which way the traffic went. Below are those sources, how to centralize them and how long it makes sense to keep them.
The sources that matter most
Each log answers a different question. CloudTrail tells you who did what in the account. VPC Flow Logs show the network traffic between resources, which helps you spot lateral movement and exfiltration. WAF logs show what was blocked and what got through at the edge, and load balancer logs record the requests that reached the application. On top of several of these sources, GuardDuty handles threat detection.
- CloudTrail: administrative activity and API calls
- VPC Flow Logs: network traffic and lateral movement
- WAF logs: what was blocked and what got through at the edge
- Load balancer logs: requests that reached the application
- Application logs: the business context of what happened
- GuardDuty: threat detection that correlates several sources
Centralization and retention
Logs scattered across several accounts and regions are of little help when the pressure is on. Centralize everything in a dedicated destination with restricted access, preferably a separate log account. That way, whoever compromises production cannot erase the evidence. With the logs in one place, you can also correlate different sources into a single timeline.
Set retention based on the time that usually passes between compromise and discovery, which often runs to months. With retention that is too short, the log expires right before you need it. Keep older history in low-cost storage and leave the most recent data ready for fast queries.
Correlation and detection
Collecting is only half the job, because you also need to be able to query and correlate the data. Send the logs to an analysis solution (a data lake with queries, CloudWatch or a SIEM) and build reusable queries for the questions that come up most often in investigations. Set alerts for high-severity events, such as root usage, disabled trails and broad permission changes.
Synchronize clocks and standardize the time zone. To correlate events from different sources you need a reliable time reference, and even a small difference between clocks throws off the timeline reconstruction.
Queries that answer the investigation questions
Centralized logs only help if you can ask them what you need to know, and quickly. Keep queries ready for the most common questions in an investigation, so you don't have to write them from scratch at the worst moment. On AWS, two native tools cover most cases: CloudWatch Logs Insights, for logs in CloudWatch log groups, and Athena, for logs in S3 such as the CloudTrail logs. See the two examples below:
fields @timestamp, sourceIPAddress, userIdentity.arn| filter eventName = "ConsoleLogin"| filter responseElements.ConsoleLogin = "Failure"| sort @timestamp desc| limit 50SELECT eventtime, useridentity.arn, eventname, sourceipaddressFROM cloudtrail_logsWHERE eventname IN ('CreateAccessKey', 'AttachUserPolicy', 'StopLogging')ORDER BY eventtime DESCLIMIT 100Both queries only read data. The first returns the most recent console login failures, with the source IP and the identity used; the second looks for sensitive identity actions, such as key creation and stopping the trail. In other words, they show who acted, from where and on what, without changing anything. Keep these queries versioned and documented, along with where each log source lives, and the time between suspicion and response gets much shorter.
Integrity, access and cost
If whoever compromised the environment can alter or delete the log, it no longer counts as evidence. So, besides centralizing it in a restricted account, make the files immutable during the retention period, encrypt them with a dedicated key and limit who can read and who can delete. When the source offers integrity validation, use it: that is what proves the records were not tampered with.
Cost is what leads many teams to collect too little or keep logs for too short a time. You don't have to choose between visibility and the monthly bill. Use storage tiers, with recent history ready for fast queries and older data moved to cheaper storage without being deleted. Partitioning the data by date and by source also helps, because each query scans less volume and the cost of investigating drops considerably.
Practical checklist
- Enable a multi-region CloudTrail trail as the baseline
- Turn on VPC Flow Logs in the relevant networks
- Collect WAF and load balancer logs
- Forward application logs with their business context
- Enable GuardDuty for correlated detection
- Centralize logs in a dedicated, restricted account
- Set retention that matches the typical time to discovery
- Send the logs to an analysis solution
- Build reusable queries and alerts for high-severity events
- Standardize the time zone and synchronize clocks
- Keep investigation queries ready for the common questions
- Make logs immutable and encrypted, with restricted access
- Use storage tiers and partitioning to control cost
Best practices
- Plan your logs before the incident, because during it is too late
- Centralize in a separate account to preserve the evidence
- Cover network, identity, edge and application
- Keep logs for as long as actually passes between compromise and discovery
- Build queries and alerts for the most common questions
- Keep a reliable time reference so you can correlate
- Have investigation queries ready before you need them
- Protect log integrity and restrict who can delete logs
Common mistakes
- Finding out mid-incident that the needed log was not enabled
- The investigation lacks evidence exactly at the critical point.
- Logs scattered across accounts and regions
- Correlation becomes slow and difficult right in the middle of an incident.
- Retention that is too short
- Logs expire before the incident is discovered.
- Collecting without the ability to query
- Lots of stored data and few answers when it is time to investigate.
- Unsynchronized clocks
- The timeline becomes inconsistent and correlation suffers.
- Logs with no protection against tampering
- Whoever compromises the environment deletes or alters the evidence the investigation would rely on.
- Collecting too little for fear of cost
- Without storage tiers, the cut tends to fall on exactly the log the investigation would miss.
Authorship and review
Cesar Gargiulo
CEO and founder of GUARDIASEC. Technical and editorial lead for the content published in this library.
Published on . Found a technical error? Write to [email protected] and we will correct it and keep a record of the change.