use crate::ops::security_detection::detector_support::{attacks, DetectionError};
pub const SPEC_TOML: &str = r#"schema_version = 1
id = "security_detection.detect_obfuscated_js"
archetype = "rule-bytes-to-bool"
display_name = "Detect Obfuscated JS"
summary = "Returns true for eval plus decoder plus character-code JavaScript obfuscation."
category = "C"
[intrinsic]
wgsl = "security_detection_detect_obfuscated_js"
[signature]
inputs = ["Bytes"]
output = "Bool"
laws = []
equivalence_classes = ["eval_atob_fromcharcode", "plain_js", "encoded_tokens", "t47_cap"]
workgroup_size = [64, 1, 1]
tags = ["security-detection", "javascript", "obfuscation", "t47"]
fixtures_dir = "fixtures/"
"#;
pub const REFERENCE_VECTORS_TOML: &str = r#"[[case]]
name = "positive_eval_decoder_charcode"
input = "eval(atob(String.fromCharCode(81,85,74,68)))"
expected = true
[[case]]
name = "negative_plain_js"
input = "console.log('hello world')"
expected = false
"#;
pub mod lowering {
#[must_use]
pub const fn source() -> &'static str {
r#"struct Params {
input_len: u32,
_pad0: u32,
_pad1: u32,
_pad2: u32,
}
@group(0) @binding(0) var<storage, read> input: array<u32>;
@group(0) @binding(1) var<storage, read_write> output: array<u32>;
@group(0) @binding(2) var<uniform> params: Params;
fn lower_ascii(byte: u32) -> u32 {
if (byte >= 65u && byte <= 90u) {
return byte + 32u;
}
return byte;
}
fn hex_value(byte: u32) -> u32 {
if (byte >= 48u && byte <= 57u) {
return byte - 48u;
}
let lowered = lower_ascii(byte);
if (lowered >= 97u && lowered <= 102u) {
return lowered - 87u;
}
return 255u;
}
fn normalized_at(index: u32) -> u32 {
if (index + 2u < params.input_len && input[index] == 37u) {
let hi = hex_value(input[index + 1u]);
let lo = hex_value(input[index + 2u]);
if (hi < 16u && lo < 16u) {
return lower_ascii(hi * 16u + lo);
}
}
return lower_ascii(input[index]);
}
fn matches_at(index: u32, needle: array<u32, 12>, len: u32) -> bool {
if (index + len > params.input_len) {
return false;
}
for (var offset = 0u; offset < len; offset = offset + 1u) {
if (normalized_at(index + offset) != needle[offset]) {
return false;
}
}
return true;
}
fn contains(lane: u32, needle: array<u32, 12>, len: u32) -> bool {
for (var index = lane; index < params.input_len; index = index + 64u) {
if (matches_at(index, needle, len)) {
return true;
}
}
return false;
}
var<workgroup> lane_flags: array<u32, 64>;
@compute @workgroup_size(64)
fn security_detection_detect_obfuscated_js(@builtin(local_invocation_id) lid: vec3<u32>) {
let lane = lid.x;
var flags = 0u;
if (contains(lane, array<u32, 12>(101u, 118u, 97u, 108u, 40u, 0u, 0u, 0u, 0u, 0u, 0u, 0u), 5u) ||
contains(lane, array<u32, 12>(115u, 101u, 116u, 116u, 105u, 109u, 101u, 111u, 117u, 116u, 40u, 0u), 11u)) {
flags = flags | 1u;
}
if (contains(lane, array<u32, 12>(97u, 116u, 111u, 98u, 40u, 0u, 0u, 0u, 0u, 0u, 0u, 0u), 5u) ||
contains(lane, array<u32, 12>(117u, 110u, 101u, 115u, 99u, 97u, 112u, 101u, 40u, 0u, 0u, 0u), 9u)) {
flags = flags | 2u;
}
if (contains(lane, array<u32, 12>(102u, 114u, 111u, 109u, 99u, 104u, 97u, 114u, 99u, 111u, 100u, 101u), 12u) ||
contains(lane, array<u32, 12>(92u, 120u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u), 2u) ||
contains(lane, array<u32, 12>(92u, 117u, 48u, 48u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u), 4u)) {
flags = flags | 4u;
}
lane_flags[lane] = flags;
workgroupBarrier();
if (lane == 0u) {
var combined = 0u;
for (var i = 0u; i < 64u; i = i + 1u) {
combined = combined | lane_flags[i];
}
output[0] = select(0u, 1u, (combined & 7u) == 7u);
}
}
"#
}
}
pub fn detect_obfuscated_js(input: &[u8]) -> Result<bool, DetectionError> {
attacks::detect_obfuscated_js(input)
}
pub mod implementation {
pub use super::detect_obfuscated_js;
pub mod kernel {
pub use super::super::detect_obfuscated_js;
}
pub mod lowering {
pub mod wgsl {
pub use super::super::super::lowering::source;
}
}
}