# Extracting data between two phrases with GROK

**URL:** <https://discuss.elastic.co/t/extracting-data-between-two-phrases-with-grok/302269>\
**Category:** Elasticsearch\
**Tags:** elastic-stack-security\
**Created:** [April 12, 2022, 7:06pm UTC](https://discuss.elastic.co/t/extracting-data-between-two-phrases-with-grok/302269 "2022-04-12T19:06:11Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ARDiver86](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ardiver86/32/85266_2.png) [@ARDiver86](https://discuss.elastic.co/u/ARDiver86)\
**Post date:** [April 12, 2022, 7:06pm UTC](https://discuss.elastic.co/t/extracting-data-between-two-phrases-with-grok/302269/1 "2022-04-12T19:06:11Z")

</div>

I'm trying to extract a powershell signature block from powershell scripts that are being ran in our environment. At the end of the powershell.file.script\_block\_text field there is sometimes a signature in this format:

```auto
# SIG # Begin signature block
# MIIbDQYJKoZIhvcNAQcCoIIa/jCCGvoCAQExCzAJBgUrDgMCGgUAMGkGCisGAQQB
# gjcCAQSgWzBZMDQGCisGAQQBgjcCAR4wJgIDAQAABBAfzDtgWUsITrck0sYpfvNR
# AgEAAgEAAgEAAgEAAgEAMCEwCQYFKw4DAhoFAAQUxKaXN7doWq+mq18IrzABoXMr
# 4l6gghXyMIIEoDCCA4igAwIBAgIKYRr16gAAAAAAajANBgkqhkiG9w0BAQUFADB5
# MQswCQYDVQQGEwJVUzETMBEGA1UECBMKV2FzaGluZ3RvbjEQMA4GA1UEBxMHUmVk
# bW9uZDEeMBwGA1UEChMVTWljcm9zb2Z0IENvcnBvcmF0aW9uMSMwIQYDVQQDExpN
# aWNyb3NvZnQgQ29kZSBTaWduaW5nIFBDQTAeFw0xMTExMDEyMjM5MTdaFw0xMzAy
# MDEyMjQ5MTdaMIGDMQswCQYDVQQGEwJVUzETMBEGA1UECBMKV2FzaGluZ3RvbjEQ
# MA4GA1UEBxMHUmVkbW9uZDEeMBwGA1UEChMVTWljcm9zb2Z0IENvcnBvcmF0aW9u
# SIG # End signature block"

```

However, I cannot quite figure out a GROK pattern to make this work. On the ingest pipeline I planned to extract everything between "Begin signature block" and "# SIG # End signature block", then remove all the hashtags # to leave the signature in a new field (powershell.file.script\_block\_signature).

I was thinking something like this but unable to get it to match:  
`(?<script_start>.* Begin signature block)(?<powershell.file.script_block_signature>[A-Za-z0-9]*)(?<script_end>.* End signature block)`

I got this to match and I think it will work but I'm curious about the performance:  
`(?<script_start>.* Begin signature block)(?<powershell.file.script_block_signature>(.|\r|\n)*)(?<script_end>.*# SIG # End signature block)`

On the last one I assume I'll just have to remove "\r\n# " with a processor

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [April 13, 2022, 1:49pm UTC](https://discuss.elastic.co/t/extracting-data-between-two-phrases-with-grok/302269/2 "2022-04-13T13:49:17Z")

</div>

How about something like (only did a quick test, so may be buggy):

```auto

POST _ingest/pipeline/_simulate
{
  "docs": [
    {
      "_source" : {
        "data" : """
# SIG # Begin signature block
# MIIbDQYJKoZIhvcNAQcCoIIa/jCCGvoCAQExCzAJBgUrDgMCGgUAMGkGCisGAQQB
# gjcCAQSgWzBZMDQGCisGAQQBgjcCAR4wJgIDAQAABBAfzDtgWUsITrck0sYpfvNR
# AgEAAgEAAgEAAgEAAgEAMCEwCQYFKw4DAhoFAAQUxKaXN7doWq+mq18IrzABoXMr
# 4l6gghXyMIIEoDCCA4igAwIBAgIKYRr16gAAAAAAajANBgkqhkiG9w0BAQUFADB5
# MQswCQYDVQQGEwJVUzETMBEGA1UECBMKV2FzaGluZ3RvbjEQMA4GA1UEBxMHUmVk
# bW9uZDEeMBwGA1UEChMVTWljcm9zb2Z0IENvcnBvcmF0aW9uMSMwIQYDVQQDExpN
# aWNyb3NvZnQgQ29kZSBTaWduaW5nIFBDQTAeFw0xMTExMDEyMjM5MTdaFw0xMzAy
# MDEyMjQ5MTdaMIGDMQswCQYDVQQGEwJVUzETMBEGA1UECBMKV2FzaGluZ3RvbjEQ
# MA4GA1UEBxMHUmVkbW9uZDEeMBwGA1UEChMVTWljcm9zb2Z0IENvcnBvcmF0aW9u
# SIG # End signature block"
        """
      }
    }],
    "pipeline": {
      "processors": [
        {
          "grok": {
            "field": "data",
            "patterns": ["# SIG # Begin signature block\n(?m)%{GREEDYDATA:message}\n# SIG # End signature block"]
          }
        }
      ]
    }
}

```

---

<div class="post-metadata">

**Author:** ![ARDiver86](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ardiver86/32/85266_2.png) [@ARDiver86](https://discuss.elastic.co/u/ARDiver86)\
**Post date:** [April 14, 2022, 2:08pm UTC](https://discuss.elastic.co/t/extracting-data-between-two-phrases-with-grok/302269/3 "2022-04-14T14:08:13Z")

</div>

Using the Grok Debugger in Kibana, I can't get it to match

---

<div class="post-metadata">

**Author:** ![leandrojmp](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/leandrojmp/32/107231_2.png) [@leandrojmp](https://discuss.elastic.co/u/leandrojmp)\
**Post date:** [April 14, 2022, 2:31pm UTC](https://discuss.elastic.co/t/extracting-data-between-two-phrases-with-grok/302269/4 "2022-04-14T14:31:27Z")

</div>

If you need to extract data from two strings, I think that the [dissect processor](https://www.elastic.co/guide/en/elasticsearch/reference/current/dissect-processor.html) works best, and the pattern is pretty similar to the one that @spinscale shared.

It would be something like this:

```auto
POST _ingest/pipeline/_simulate
{
  "docs": [
    {
      "_source" : {
        "data" : """
# SIG # Begin signature block
# MIIbDQYJKoZIhvcNAQcCoIIa/jCCGvoCAQExCzAJBgUrDgMCGgUAMGkGCisGAQQB
# gjcCAQSgWzBZMDQGCisGAQQBgjcCAR4wJgIDAQAABBAfzDtgWUsITrck0sYpfvNR
# AgEAAgEAAgEAAgEAAgEAMCEwCQYFKw4DAhoFAAQUxKaXN7doWq+mq18IrzABoXMr
# 4l6gghXyMIIEoDCCA4igAwIBAgIKYRr16gAAAAAAajANBgkqhkiG9w0BAQUFADB5
# MQswCQYDVQQGEwJVUzETMBEGA1UECBMKV2FzaGluZ3RvbjEQMA4GA1UEBxMHUmVk
# bW9uZDEeMBwGA1UEChMVTWljcm9zb2Z0IENvcnBvcmF0aW9uMSMwIQYDVQQDExpN
# aWNyb3NvZnQgQ29kZSBTaWduaW5nIFBDQTAeFw0xMTExMDEyMjM5MTdaFw0xMzAy
# MDEyMjQ5MTdaMIGDMQswCQYDVQQGEwJVUzETMBEGA1UECBMKV2FzaGluZ3RvbjEQ
# MA4GA1UEBxMHUmVkbW9uZDEeMBwGA1UEChMVTWljcm9zb2Z0IENvcnBvcmF0aW9u
# SIG # End signature block"
        """
      }
    }],
    "pipeline": {
      "processors": [
        {
        "dissect": {
            "field": "data",
            "pattern": "# SIG # Begin signature block\n%{message}\n# SIG # End signature block"
          }
        }
      ]
    }
}

```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [May 12, 2022, 2:32pm UTC](https://discuss.elastic.co/t/extracting-data-between-two-phrases-with-grok/302269/5 "2022-05-12T14:32:06Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
