4. ELK Stack: "L" is for Lord of the Stack
Logstash is my favorite tool. So much flexibility and so much to play with.

What is Logstash?

In my honest opinion, Logstash is the best tool of the stack and deserves the title of The Lord of the Stack
Logstash, one of the core products of the Elastic Suite, is used to aggregate and process data to send it to Elasticsearch or many other destinations . Logstash is an open-source server-side pipeline for data processing that lets you ingest data from multiple sources simultaneously, then transforms it before it's indexed in Elasticsearch. Similar to an event collector, Logstash is the agent that intervenes between the source of our data and the final storage location, it is very flexible that it can import data from almost any source.

Logstash can:

    Transform, parse and filter data in real time.
    Structuring unstructured data.
    Anonymize personal data or exclude it completely.
    Perform geolocation searches.
    Be scalable on several nodes.
    Be used as a buffer for Elasticsearch.
Logstash can work in three stages: inputs β†’ filters β†’ outputs. Inputs generate events, filters modify them, and outputs send them elsewhere.
Logstash workflow
Inputs and outputs support codecs that allow you to encode or decode data as it enters or leaves the pipeline without having to use a separate filter.
Logstash is part of ETL (Extract, Transform and Load) technology :

Logstash Anatomy :

Logstash uses configuration files that are maintained under the /etc/logstash/conf.d/ folder. These files can play the role of parsers in a SIEM use case, where you can define data entry, process and transforms event logs and send them to Elasticsearch.
Logstash configuration files

Input plugins

Most commonly used input plugins in a centralized logging context are :
    Syslog : Upon running, Logstash is going to start listening on port 514 for syslog incoming data.
1
input {
2
syslog {
3
port => 514
4
}
5
}
Copied!
    File: Reading logs from a file
1
input {
2
file {
3
path => "/home/elk/folder/filename.csv"
4
start_position => beginning
5
}
6
}
Copied!
    Beats: Upon running, Logstash is going to start listening on port 5044 for beats agents incoming data.
1
input {
2
syslog {
3
port => 5044
4
}
5
}
Copied!
    TCP/UDP:
1
input {
2
tcp {
3
port => 5400
4
}
5
udp {
6
port => 5500
7
}
8
}
Copied!
Notice that Logstash can't listen to the same port number defined in two or more inputs.

Filter plugins

This is where the magic happens, where you can parse, enrich, drop, remove specific fields or hide them
    GROK: This filter is an enhanced version of Regular Expressions, where it provides pre-cooked Regex expressions like IP address format or Syslog header expression.
Event sample :LEEF:1.0|FORCEPOINT|Firewall|6.6.1|Connection_Discarded|devTimeFormat=MMM dd yyyy HH:mm:ss devTime=Apr 18 2020 21:03:07 proto=17 dstPort=137 srcPort=137 dst=10.20.65.255 src=10.10.1.79 action=Discard sender=FP_CLUSTER 1
1
filter {
2
grok {
3
match => {"<%{NUMBER:entier}>%{WORD:logformat}:%{NUMBER:format_version}\|%{WORD:observer_product}\|%{WORD:observer_
4
type}\|%{NUMBER:observer_version}.*?\|%{DATA:event_name}\|%{GREEDYDATA:fp_msg}"}
5
}
6
}
Copied!
    KV: This filter provides an easy way to parse any event that has a key/value format
Event sample: date=2020-04-22 time=14:46:33 devname="fgt1" devid="FG100" logid="0000000013" type="traffic" subtype="forward" level="notice" vd="root" eventtime=1587563193 srcip=X.X.X.X srcport=42837 srcintf="PUBL" srcintfrole="DMZ" dstip=X.X.X.X dstport=53 dstintf="wan1" dstintfrole="wan" sessionid=70225713
1
filter {
2
kv {
3
value_split => " = " # key value seperator
4
field_split => " " # key to key seperator
5
}
6
}
Copied!
    MUTATE: if you need to remove, add, copy fields or replace them and add tags to enrich you event logs then MUTATE is the way to go. This particularly helpful for event normalization using ECS format
1
filter {
2
mutate {
3
copy =>{ "[direction]"=> "[network][direction]" }
4
copy =>{ "[locport]"=> "[source][port]" }
5
copy => { "[action]"=> "[event][action]" }
6
add_field => { "[ecs][version]" => "1.5.0" }
7
rename => { "[destination_ip]"=> "[destination][ip]" }
8
remove_field => [ "logformat" ]
9
add_tag => ["forcepoint"]
10
}
11
}
Copied!
    Translate: This is helpful for event enrichment purposes when you need correlate fields content with predefined dictionaries and then override them accordingly. For example if you want to explicitly name SIDs in a windows environment you can use this filter.
1
filter {
2
translate {
3
field => "TargetUserSid"
4
destination => "TargetUserSid"
5
dictionary_path => "/etc/logstash/dictionaries/sid.yaml"
6
override => true
7
}
8
}
Copied!
the /etc/logstash/dictionaries/sid.yaml file can look something like this
YAML dictionary
    DATE: Normalizing dates is something crucial for data indexing in Elasticsearch. This filter can be used to transform any date to the desired format.
1
filter {
2
date {
3
match => [ "logdate", "MMM dd yyyy HH:mm:ss” ]
4
}
5
}
Copied!
    CSV: Useful for ingesting data in csv format
1
filter {
2
csv {
3
columns => ["[event][outcome]","[event][type]","[user][name]","[server][ip]","[server][name]"]
4
separator => ","
5
}
6
}
Copied!
    JSON: Ingesting incoming events in json format
1
filter {
2
json {
3
source => "message"
4
}
5
}
Copied!
    CIDR: This filter would be useful for defining your networks and tagging your traffic and also enriching event with IP addresses locality.
1
filter {
2
cidr {
3
add_field => {"[source][locality]" => "private"}
4
address => [ "%{[source][ip]}" ]
5
network => [ "10.0.0.0/8", "172.16.0.0/12", "192.168.0.0/16" ]
6
}
7
}
Copied!
    TLD: This plugin is not bundled by default, so you need to install it using the following command /usr/share/logstash/bin/logstash-plugin install logstash-filter-tld. TLD plugin can be used for extracting domain names related information like TLD, eTLD plus 1, TRD...etc.
1
filter {
2
if [alert][destination][domain] {
3
tld {
4
source => "[alert][destination][domain]"
5
target => "[domain_dns]"
6
}
7
if [domain_dns][trd]{
8
mutate {
9
join => { "[domain_dns][trd]" => "." }
10
}
11
}
12
mutate {
13
copy => { "[domain_dns][domain]" => "[alert][dns][question][registered_domain]" }
14
copy => { "[domain_dns][trd]" => "[alert][dns][question][subdomain]" }
15
copy => { "[domain_dns][tld]" => "[alert][dns][question][top_level_domain]" }
16
copy => { "[alert][dns][question][registered_domain]" => "[alert][related][domain]" }
17
}
18
}
19
20
}
Copied!
    GEOIP: Geolocation public IPs enrichment.
1
if [src_ip_geo_apply] {
2
geoip {
3
source => "[src_ip_geo_apply]"
4
target => "[alert][source][geo]"
5
fields => ["city_name", "continent_code", "country_code2", "country_name", "location", "region_code", "region_name"]
6
}
7
}
Copied!

Output plugins

The main outputs for our use case would be Elasticsearch, Email or a Pipeline (We would cover pipelines in the next article).
    Elasticsearch : Elasticsearch output supports many options which you can read more about in elastic.co
1
output {
2
if [host] == "10.1.1.254" {
3
elasticsearch {
4
hosts => ["localhost:9200"] # Elasticsearch hostname or IP address
5
index => "firewallfortigate" # Name of destination index
6
}
7
}
8
else if "forcepoint" in [tags] {
9
elasticsearch {
10
hosts => ["localhost:9200"]
11
index => "forcepoint"
12
}
13
}
14
}
Copied!
Last modified 9mo ago