# How to mutate sensitive data in logstash

**URL:** <https://discuss.elastic.co/t/how-to-mutate-sensitive-data-in-logstash/173441>\
**Category:** Logstash\
**Created:** [March 22, 2019, 7:28am UTC](https://discuss.elastic.co/t/how-to-mutate-sensitive-data-in-logstash/173441 "2019-03-22T07:28:42Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![manickalai](https://avatars.discourse-cdn.com/v4/letter/m/94ad74/32.png) [@manickalai](https://discuss.elastic.co/u/manickalai)\
**Post date:** [March 22, 2019, 7:28am UTC](https://discuss.elastic.co/t/how-to-mutate-sensitive-data-in-logstash/173441/1 "2019-03-22T07:28:43Z")

</div>

Input : "refNo":"0015531891539643", cardNo=6055490201604225

Requirment: The requirement is to show the start 6 digits and last 4 digits and mask the remaining numbers of cardNo in logstash. I applied gsub/mutate filter but the replacement string doesn't allow regex. Any other way this can be done in logstash alone cardNo, not refNo?

Output : "refNo":"0015531891539643", cardNo=605549\*\*\*\*\*\*4225

---

<div class="post-metadata">

**Author:** ![Badger](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/badger/32/25190_2.png) [@Badger](https://discuss.elastic.co/u/Badger)\
**Post date:** [March 22, 2019, 1:18pm UTC](https://discuss.elastic.co/t/how-to-mutate-sensitive-data-in-logstash/173441/2 "2019-03-22T13:18:00Z")

</div>

Provided that you always want \*\*\*\*\*\* (and not to match the number of digits masked) then you could do it using

```
mutate { gsub => ["message", "(cardNo=[0-9]{6})[0-9]+([0-9]{4})", "\1 ****** \2" ] }
```

---

<div class="post-metadata">

**Author:** ![manickalai](https://avatars.discourse-cdn.com/v4/letter/m/94ad74/32.png) [@manickalai](https://discuss.elastic.co/u/manickalai)\
**Post date:** [March 27, 2019, 2:10pm UTC](https://discuss.elastic.co/t/how-to-mutate-sensitive-data-in-logstash/173441/3 "2019-03-27T14:10:41Z")

</div>

> [@Badger](#):
>
> * * *

i tried with below

filter {  
mutate {  
gsub =\> ["message", "cardNo : ([0-9]{4})[0-9]+([0-9]{4})", "\*\*\*\*\*\*" ]  
}  
}

Output : its not working.

Could you help me on it

---

<div class="post-metadata">

**Author:** ![Badger](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/badger/32/25190_2.png) [@Badger](https://discuss.elastic.co/u/Badger)\
**Post date:** [March 27, 2019, 2:13pm UTC](https://discuss.elastic.co/t/how-to-mutate-sensitive-data-in-logstash/173441/4 "2019-03-27T14:13:53Z")

</div>

Your initial post said you have "cardNo=", not "cardNo : "

---

<div class="post-metadata">

**Author:** ![manickalai](https://avatars.discourse-cdn.com/v4/letter/m/94ad74/32.png) [@manickalai](https://discuss.elastic.co/u/manickalai)\
**Post date:** [March 27, 2019, 2:45pm UTC](https://discuss.elastic.co/t/how-to-mutate-sensitive-data-in-logstash/173441/5 "2019-03-27T14:45:38Z")

</div>

sorry,

now changed into  
filter {  
mutate {  
gsub =\> ["message", "cardNo= ([0-9]{4})[0-9]+([0-9]{4})", "\*\*\*\*\*\*" ]  
}  
}

but the output is the same.

---

<div class="post-metadata">

**Author:** ![manickalai](https://avatars.discourse-cdn.com/v4/letter/m/94ad74/32.png) [@manickalai](https://discuss.elastic.co/u/manickalai)\
**Post date:** [March 28, 2019, 6:17am UTC](https://discuss.elastic.co/t/how-to-mutate-sensitive-data-in-logstash/173441/6 "2019-03-28T06:17:53Z")

</div>

HI Badger,

i am using given below regex, but the out is not hide the card no alone if the input : "refNo":"0015531891539643", cardNo=6055490201604225

filter {  
mutate {  
gsub =\> ["message", "cardNo= ([0-9]{4})[0-9]+([0-9]{4})", "\*\*\*\*\*\*" ]  
}  
}

---

<div class="post-metadata">

**Author:** ![manickalai](https://avatars.discourse-cdn.com/v4/letter/m/94ad74/32.png) [@manickalai](https://discuss.elastic.co/u/manickalai)\
**Post date:** [March 28, 2019, 10:52am UTC](https://discuss.elastic.co/t/how-to-mutate-sensitive-data-in-logstash/173441/7 "2019-03-28T10:52:02Z")

</div>

Input : "refNo":"0015531891539643", cardNo=6055490201604225  
filter {  
mutate {  
gsub =\> ["message", "cardNo=(\d{6})(\d{6})(\d{4})", "\1######\3"]  
}  
}

Output : refNo:0015531891539643,605549######4225

Problem statement : "cardNo=" is missing

---

<div class="post-metadata">

**Author:** ![Badger](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/badger/32/25190_2.png) [@Badger](https://discuss.elastic.co/u/Badger)\
**Post date:** [March 28, 2019, 12:30pm UTC](https://discuss.elastic.co/t/how-to-mutate-sensitive-data-in-logstash/173441/8 "2019-03-28T12:30:52Z")

</div>

Use the filter I suggested instead of the one you are using. It includes cardNo= in the capture group. Also, there is no point is creating capture groups that you do not backreference.

```
mutate { gsub => ["message", "(cardNo=[0-9]{6})[0-9]+([0-9]{4})", "\1 ****** \2" ] }
```

---

<div class="post-metadata">

**Author:** ![manickalai](https://avatars.discourse-cdn.com/v4/letter/m/94ad74/32.png) [@manickalai](https://discuss.elastic.co/u/manickalai)\
**Post date:** [March 29, 2019, 7:41am UTC](https://discuss.elastic.co/t/how-to-mutate-sensitive-data-in-logstash/173441/9 "2019-03-29T07:41:55Z")

</div>

thanks for the support, it has been filtered,

one more query if the input is "expiryDate":"2109"

how to filter it ?

---

<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:** [April 26, 2019, 7:42am UTC](https://discuss.elastic.co/t/how-to-mutate-sensitive-data-in-logstash/173441/10 "2019-04-26T07:42:07Z")

</div>

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