Hello Team,
I was trying to implement the hashing using Filebeat script processor. But Filebeat doesnt have Crypto module by default and staring below error. Is there any option to achieve this functionality?
Geting below Javascript error.
ReferenceError: CryptoJS is not defined at inline.js:19:20(7)
The below mentioned code I have used in the Filebeat processors:
var anonymizedMessage = message.replace(regex, function(match) {
var randomString = generateRandomString(8); // Generate a random salt
var hash = CryptoJS.SHA256(match + randomString).toString(CryptoJS.enc.Base64); // Generate hash using SHA-256
return hash;
});
It would be more helpful if I get any suggestion or idea to resolve this?
To achieve hashing in Filebeat without CryptoJS, consider preprocessing logs with an external script or using Logstash with a Ruby filter for hashing, as Filebeat doesn't support external JavaScript libraries like CryptoJS.
@leandrojmp and @rtwolfe94022 are absolutely right, you can't use external dependencies in the script processor. It looks like you're trying to generate a hash, so I would recommend taking a look at the fingerprint processor to see if that will do what you need.
Thank you for your time and response.
I apologize for not providing more context initially. My objective is to replace sensitive information in log messages with hashed values before sending them to Logstash. Specifically, I aim to use a script processor to generate unique hash values and replace occurrences of sensitive data such as "SECURITY***" with hashes, so used a below mentioned hashing algorithm like SHA-256 in combination with a random salt.
Code below.
processors:
- script:
lang: javascript
id: generate_random_hashes
source: |
function generateRandomString(length) {
// Generate a random string of specified length with uppercase letters
var result = '';
var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
var charactersLength = characters.length;
for (var i = 0; i < length; i++) {
result += characters.charAt(Math.floor(Math.random() * charactersLength));
}
return result;
}
function process(event) {
var message = event.Get("message");
var regex = /(SECURITY\w+)|(SECURITY\d+)/g;
// Replace each SECURITY keywords with a unique hash value
var anonymizedMessage = message.replace(regex, function(match) {
var randomString = generateRandomString(8); // Generate a random salt
var hash = CryptoJS.SHA256(match + randomString).toString(CryptoJS.enc.Base64); // Generate hash using SHA-256
return hash;
});
// Add a field indicating anonymization
if (anonymizedMessage !== event.Get("message")) {
event.Put("anonymized", true);
event.Put("message", anonymizedMessage);
}
return event;
}
Here, The generateRandomString function generates a random salt with the specified length.
Each occurrence of SECURITY in the message is replaced with a unique hash value generated by combining the SECURITY with the random salt and then hashing it using SHA-256.
The resulting hash value is added to the anonymizedMessage.
This hash value is unique for each occurrence of SECURITY keyword due to the random salt, ensuring that different occurrences are replaced with different hash values.
But It seems that Filebeat doen't have the CryptoJS library by default and due to that, its stating the below Javascript error.
ReferenceError: CryptoJS is not defined at inline.js:19:20(7)
@carly.richmond - as you mentioned, I could see there is an Algorithm (md5, sha1, sha256, sha384, sha512, xxhash) available in fingerprint processor.
I'm not sure how to utilize this processor for my usecase but anyway I will try it out.
I attempted to utilize the Fingerprint processor to substitute a specific field with encoded values using the sha256 algorithm (method='sha256'). However, I encountered an issue where the Fingerprint processor is replacing the entire message instead of just the particular fields. Can someone assist me in achieving this goal? Below, I've provided the code I used and the input data. Please review and advise if I've misconfigured anything.
I utilized different field and I was expecting each field should contain the hashed/encoded values. However, I encountered another issue where, when attempting to replace two different columns (fields), only one encoded value is present in the new field instead of two separate entries.
My intention is to replace those specific fields and update the same in the original message, as illustrated below
Apache, Apache Lucene, Apache Hadoop, Hadoop, HDFS and the yellow elephant
logo are trademarks of the
Apache Software Foundation
in the United States and/or other countries.