# Filter out passwords with mutate

**URL:** <https://discuss.elastic.co/t/filter-out-passwords-with-mutate/127345>\
**Category:** Logstash\
**Created:** [April 9, 2018, 3:27pm UTC](https://discuss.elastic.co/t/filter-out-passwords-with-mutate/127345 "2018-04-09T15:27:07Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![joshiegy](https://avatars.discourse-cdn.com/v4/letter/j/848f3c/32.png) [@joshiegy](https://discuss.elastic.co/u/joshiegy)\
**Post date:** [April 9, 2018, 3:27pm UTC](https://discuss.elastic.co/t/filter-out-passwords-with-mutate/127345/1 "2018-04-09T15:27:07Z")

</div>

Hi,

I'm trying to filter out passwords that icingabeat sends out.  
The field with password is "check\_result.command" and the password is always after "-p," or "-P,".

Example line:  
/usr/lib/nagios/plugins/check\_mysql, -H, hostname.tld, -p, Xkaw\*!dH=VlM@, -u, monitoring

(the password is an example).

I've tried:

> filter {  
> if "icingabeat" in [src\_type] {  
> if [check\_result][command] =~ /.\*-[pP],.\*/ {  
> mutate {  
> gsub =\> [  
> "[check\_result][command]", "-p, [^,]\*", "-p, FILTERED\_PASSWORD"  
> ]  
> }  
> }  
> }  
> }

Thou it does not catch the password-field. Any ideas?

---

<div class="post-metadata">

**Author:** ![yaauie](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/yaauie/32/23363_2.png) [@yaauie](https://discuss.elastic.co/u/yaauie)\
**Post date:** [April 9, 2018, 11:53pm UTC](https://discuss.elastic.co/t/filter-out-passwords-with-mutate/127345/2 "2018-04-09T23:53:22Z")

</div>

tl;dr: the `.*` at the beginning of your outer `if`-statement pattern is too greedy; it captures the whole string (including any `-p,` sequence) up until a newline, so the next character sequence will _never_ be a literal hyphen (`-`).

Your outer check pattern should be `/ -[pP], /` (note the literal spaces on either side and absence of other patterns).

* * *

While attempting to come up with a solution, I improved your pattern a bit, and got to a point where I could reliably capture passwords without changing the case of the preceding p/P (not sure if this matters for you):

```auto
(?<= -[pP], )[^,]*

```

In the pipeline config, this becomes:

```auto
filter {
  mutate {
    gsub => ["[check_result][command]", "(?<= -[pP], )[^,]*", "FILTERED_PASSWORD"]
  }
}

```

By using a positive lookbehind assertion of `-[pP],`, we can capture sequences that are proceeded by this pattern without including it in our match, and since the `-p,` or `-P,` isn't part of the match, we don't have to worry about replacing it.

Which brings me to: EDGE CASES

What if a password includes a literal comma? Will this end up leaking everything after that literal comma into your logs? 😩

If your producer handles literal commas in passwords by escaping them (e.g., by prefixing them with a backslash), the following would ensure we capture the _whole_ password):

```auto
(?<= -[pP], )(?:\\,|[^,])*

```

In our repeated match, we either capture a literal-backslash-literal-comma sequence, or anything-but-a-comma.

If your producer does _not_ escape literal commas, the following pattern would be a bit better, as it only breaks the capture once it encounters either a literal-comma-literal-space sequence or EOL by moving the break pattern into a positive-lookahead clause and making the repeat operator less greedy:

```auto
(?<= -[pP], ).+?(?=, |$)

```

I tested the above pattern with the following command lines, and it seems to handle the edge-cases well.

```auto
/usr/lib/nagios/plugins/check_mysql, -H, hostname.tld, -p, Xkaw*!dH=VlM@, -u, monitoring
/usr/lib/nagios/plugins/check_mysql, -H, hostname.tld, -p, Xkaw*!d,H=VlM@, -u, monitoring
/usr/lib/nagios/plugins/check_mysql, -H, hostname.tld, -p, Xkaw*!dH=V,lM@, -u, monitoring
/usr/lib/nagios/plugins/check_mysql, -H, hostname.tld, -u monitoring -p, Xkaw*!dH=V,lM@
/usr/lib/nagios/plugins/check_mysql, -H, hostname.tld, -u monitoring -p, Xkaw*!dH=VlM@
/usr/lib/nagios/plugins/check_mysql, -H, hostname.tld, -u monitoring -p, ,Xkaw*!dH=VlM@
/usr/lib/nagios/plugins/check_mysql, -H, hostname.tld, -u monitoring -p, Xkaw*!dH=VlM@,
/usr/lib/nagios/plugins/check_mysql, -H, hostname.tld, -P, Xkaw*!dH=VlM@, -u, monitoring
/usr/lib/nagios/plugins/check_mysql, -H, hostname.tld, -P, Xkaw*!d,H=VlM@, -u, monitoring
/usr/lib/nagios/plugins/check_mysql, -H, hostname.tld, -P, Xkaw*!dH=V,lM@, -u, monitoring
/usr/lib/nagios/plugins/check_mysql, -H, hostname.tld, -u monitoring -P, Xkaw*!dH=V,lM@
/usr/lib/nagios/plugins/check_mysql, -H, hostname.tld, -u monitoring -P, Xkaw*!dH=VlM@
/usr/lib/nagios/plugins/check_mysql, -H, hostname.tld, -u monitoring -P, ,Xkaw*!dH=VlM@
/usr/lib/nagios/plugins/check_mysql, -H, hostname.tld, -u monitoring -P, Xkaw*!dH=VlM@,

```

---

<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 7, 2018, 11:53pm UTC](https://discuss.elastic.co/t/filter-out-passwords-with-mutate/127345/3 "2018-05-07T23:53:45Z")

</div>

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