Skip to content

Instantly share code, notes, and snippets.

@horacioibrahim
Last active December 18, 2024 15:00
Show Gist options
  • Save horacioibrahim/60817f283b2aafd8ef25295f75c61361 to your computer and use it in GitHub Desktop.
Save horacioibrahim/60817f283b2aafd8ef25295f75c61361 to your computer and use it in GitHub Desktop.
AgnosticData + Elastic Insights: JWT introspected
String token = null;
// Primeiro, verificar em params._source['utm_case']['token']
if (params._source.containsKey('utm_case') && params._source['utm_case'] != null) {
if (params._source['utm_case'].containsKey('token')) {
token = params._source['utm_case']['token'];
// Processar o JWT se o token for encontrado
int firstDot = token.indexOf(".");
int secondDot = token.indexOf(".", firstDot + 1);
// Verifica se é um token JWT válido
if (firstDot > 0 && secondDot > firstDot) {
String base64Url = token.substring(firstDot + 1, secondDot);
String base64 = base64Url.replace('-', '+').replace('_', '/');
try {
byte[] decoded = java.util.Base64.getDecoder().decode(base64);
// Converte byte array manualmente para string
String decodedPayload = "";
for (byte b : decoded) {
decodedPayload += (char) b;
}
// Procurar pela chave "document"
String key = "\"document\":\"";
int startIndex = decodedPayload.indexOf(key) + key.length();
int endIndex = decodedPayload.indexOf("\"", startIndex);
if (startIndex > key.length() - 1 && endIndex > startIndex) {
String documentValue = decodedPayload.substring(startIndex, endIndex);
emit(documentValue);
} else {
emit("empty");
}
} catch (Exception e) {
emit("empty");
}
} else {
emit("empty");
}
} else {
emit("empty1");
}
}
else if (params._source.containsKey('payload_parsed')
&& params._source['payload_parsed'].containsKey('target_url')) {
String targetUrl = params._source['payload_parsed']['target_url'];
// Extrair o parâmetro "token" da URL
int tokenStart = targetUrl.indexOf("token=");
if (tokenStart > -1) {
tokenStart += "token=".length();
int tokenEnd = targetUrl.indexOf("&", tokenStart); // Próximo '&' ou fim da string
if (tokenEnd == -1) {
tokenEnd = targetUrl.length();
}
String token2 = targetUrl.substring(tokenStart, tokenEnd);
// Processar o JWT se o token for encontrado
int firstDot = token2.indexOf(".");
int secondDot = token2.indexOf(".", firstDot + 1);
// Verifica se é um token JWT válido
if (firstDot > 0 && secondDot > firstDot) {
String base64Url = token2.substring(firstDot + 1, secondDot);
String base64 = base64Url.replace('-', '+').replace('_', '/');
try {
byte[] decoded = java.util.Base64.getDecoder().decode(base64);
// Converte byte array manualmente para string
String decodedPayload = "";
for (byte b : decoded) {
decodedPayload += (char) b;
}
// Procurar pela chave "document"
String key = "\"document\":\"";
int startIndex = decodedPayload.indexOf(key) + key.length();
int endIndex = decodedPayload.indexOf("\"", startIndex);
if (startIndex > key.length() - 1 && endIndex > startIndex) {
String documentValue = decodedPayload.substring(startIndex, endIndex);
emit(documentValue);
} else {
emit("empty");
}
} catch (Exception e) {
emit("empty");
}
} else {
emit("empty");
}
} else {
emit("empty");
}
}
// Caso nenhuma das verificações encontre um token
else {
emit("empty");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment