---
description: Pipeline for parsing sophos firewall logs (antivirus pipeline)
processors:
#######################
## ECS Event Mapping ##
#######################
- set:
    field: event.kind
    value: alert
- set:
    field: event.action
    value: "{{sophos.xg.log_subtype}}"
    if: "ctx.sophos?.xg?.log_subtype != null"
- set:
    field: event.outcome
    value: success
    if: "ctx.sophos?.xg?.log_subtype != null"
- append:
    field: event.category
    value:
      - malware
      - network
    if: "ctx.sophos?.xg?.log_subtype == 'Virus'"
- append:
    field: event.type
    value:
      - info
      - denied
      - connection
    if: "ctx.sophos?.xg?.log_subtype == 'Virus'"
- set:
    field: event.kind
    value: event
    if: '["09002"].contains(ctx.event?.code)'
- append:
    field: event.type
    value:
      - allowed
      - connection
    if: '["09002"].contains(ctx.event?.code)'
- append:
    field: event.category
    value: network
    if: '["09002"].contains(ctx.event?.code)'

#############################
## ECS Destination Mapping ##
#############################
- rename:
    field: sophos.xg.dst_ip
    target_field: destination.ip
    ignore_missing: true
    if: "ctx.sophos?.xg?.dst_ip != null"
- convert:
    field: sophos.xg.dst_port
    target_field: destination.port
    type: long
    ignore_failure: true
    ignore_missing: true
    if: "ctx.sophos?.xg?.dst_port != null"
- rename:
    field: sophos.xg.to_email_address
    target_field: destination.user.email
    ignore_missing: true
    if: "ctx.sophos?.xg?.to_email_address != null"
- rename:
    field: sophos.xg.dstdomain
    target_field: destination.domain
    ignore_failure: true
- rename:
    field: sophos.xg.dst_domainname
    target_field: destination.domain
    ignore_failure: true

########################
## ECS Source Mapping ##
########################
- rename:
    field: sophos.xg.src_ip
    target_field: source.ip
    ignore_missing: true
    if: "ctx.sophos?.xg?.src_ip != null"
- convert:
    field: sophos.xg.src_port
    target_field: source.port
    type: long
    ignore_failure: true
    ignore_missing: true
    if: "ctx.sophos?.xg?.src_port != null"
- rename:
    field: sophos.xg.user_name
    target_field: source.user.name
    ignore_missing: true
    if: "ctx.sophos?.xg?.user_name != null"
- rename:
    field: sophos.xg.from_email_address
    target_field: source.user.email
    ignore_missing: true
    if: "ctx.sophos?.xg?.from_email_address != null"
- rename:
    field: sophos.xg.src_domainname
    target_field: source.domain
    ignore_failure: true

######################
## ECS Rule Mapping ##
######################
- rename:
    field: sophos.xg.fw_rule_id
    target_field: rule.id
    ignore_missing: true
    if: "ctx.rule?.id == null"

#####################
## ECS URL Mapping ##
#####################
- rename:
     field: sophos.xg.url
     target_field: url.original
     ignore_missing: true
     if: "ctx.sophos?.xg?.url != null"
- uri_parts:
    if: ctx.url?.original != null && ctx.url.original.contains("://")
    field: url.original
    target_field: url
- set:
    if: ctx.url?.original != null && ctx.url.original.contains("://")
    field: url.full
    copy_from: url.original
    ignore_empty_value: true
- rename:
     field: sophos.xg.domainname
     target_field: url.domain
     ignore_failure: true

############################
## ECS User Agent Mapping ##
############################
- rename:
     field: sophos.xg.user_agent
     target_field: user_agent.original
     ignore_missing: true
     if: "ctx.sophos?.xg?.user_agent != null"
- convert:
    field: sophos.xg.status_code
    target_field: http.response.status_code
    type: long
    ignore_failure: true
    ignore_missing: true
    if: "ctx.sophos?.xg?.status_code != null && ctx.sophos?.xg?.status_code != ''"

######################
## ECS File Mapping ##
######################
- rename:
    field: sophos.xg.filename
    target_field: file.name
    ignore_missing: true
    if: "ctx.sophos?.xg?.filename != null"
- convert:
    field: sophos.xg.file_size
    target_field: file.size
    type: long
    ignore_failure: true
    ignore_missing: true
    if: "ctx.sophos?.xg?.file_size != null"
- rename:
    field: sophos.xg.file_path
    target_field: file.directory
    ignore_missing: true
    if: "ctx.sophos?.xg?.file_path != null"

######################
## ECS Network Mapping
######################
- rename:
    field: sophos.xg.protocol
    target_field: network.transport
    ignore_missing: true
- lowercase:
    field: sophos.xg.log_component
    target_field: network.protocol
    ignore_missing: true

#############
## Cleanup ##
#############
- lowercase:
      field: event.info
      ignore_failure: true
- remove:
    field:
    - sophos.xg.domainname
    - sophos.xg.dst_port
    - sophos.xg.src_port
    - sophos.xg.status_code
    - sophos.xg.file_size
    ignore_missing: true
on_failure:
- set:
    field: error.message
    value: '{{ _ingest.on_failure_message }}'
