M. A. R. M. Fernando

11 papers Unranked 11
YearRankTypeTitle / Venue / Authors
2023 conf
ICIIS
S. H. Meetiyagoda, R. W. N. W. M. R. O. K. B. Galagoda, T. D. T. L. Dissanayake, M. A. R. M. Fernando, Waranatha L. Abeygunasekara
2019 conf
ICIIS
E. M. N. Ekanayake, H. M. N. P. Herath, G. C. Perera, J. V. Wijayakulasooriya, J. R. S. S. Kumara, M. A. R. M. Fernando, K. M. K. S. Bandara
2019 conf
ICIIS
U. K. R. M. H. Rajapaksha, J. R. S. S. Kumara, M. A. R. M. Fernando, K. A. S. Susantha, Venkatesh Srinivasan, D. K. Jayaratne
2017 conf
ICIIS
G. A. T. N. Aravinda, Kapila Bandara, G. A. Jayantha, J. R. S. S. Kumara, M. A. R. M. Fernando
2017 conf
ICIIS
J. R. S. S. Kumara, M. A. R. M. Fernando, C. S. Kalpage
2017 conf
ICIIS
H. M. M. G. T. Herath, J. R. S. S. Kumara, M. A. R. M. Fernando, K. M. K. S. Bandara, Ivan Serina
2017 conf
ICIIS
D. U. Bandara, J. R. S. S. Kumara, M. A. R. M. Fernando, C. S. Kalpage
2015 conf
ICIIS
W. M. S. C. Samarasinghe, S. Manojithan, S. Subhankan, V. Suges, M. A. R. M. Fernando, J. R. S. S. Kumara
2015 conf
ICIIS
A. U. A. W. Gunawardena, A. C. M. Ahamed, W. M. S. C. Samarasinghe, M. A. R. M. Fernando
2008 conf
ICIIS
S. Ranawana, C. M. B. Ekanayaka, N. A. S. A. Kurera, M. A. R. M. Fernando, K. A. R. Perera
2008 conf
ICIIS
H. I. S. Jayasundara, W. P. S. Sudarshani, M. A. R. M. Fernando
redb/extractors/js_extractors/js_deobfuscator.py
← Index redb/extractors/js_extractors/js_deobfuscator.py python
"""Subprocess-driven JavaScript deobfuscator with jsbeautifier fallback.

Owns the heavy lifting that was previously embedded inside
`JSDeobfuscationExtractor` (`_run_deobfuscator` + `_try_jsbeautifier`). Exposed
as a single module-level entry point `deobfuscate(source, log)` so it can be
called from `JSContext.deobfuscated` (cached per sample) without dragging
extractor state through the call.

Configuration (env vars):
    JS_DEOBFUSCATOR_PATH    Path or name of the external tool (default: webcrack).
    JS_DEOBFUSCATE_TIMEOUT  Seconds before the external tool is killed
                            (process-group SIGTERM, then SIGKILL). Default: 60.

If the external tool produces non-empty output and exits 0, that wins. Otherwise
the source is run through jsbeautifier (which only normalises formatting, but
already exposes strings hidden by minification). If neither path produces
output, returns (None, None).

`FileNotFoundError` for the external tool is treated as routine — the analysis
server is either provisioned with the tool or it isn't — and demoted to a
debug-level log.
"""

import os
import signal
import subprocess
import tempfile
from typing import Optional, Tuple

DEFAULT_DEOBFUSCATOR = "webcrack"
DEFAULT_TIMEOUT_SECS = 60


def _run_external(
    source: str, deobfuscator_path: str, timeout: int, log
) -> Tuple[Optional[str], int]:
    """Run the configured external deobfuscator over `source` and capture stdout.

    Returns (text, returncode). `text` is `None` and `returncode` is `-1` when
    the binary is missing, the run timed out, or any other unexpected failure
    occurred. Missing-binary is logged at debug; timeouts and unexpected errors
    surface at warning/error.
    """
    try:
        with tempfile.NamedTemporaryFile(
            suffix=".js", mode="w", delete=False, encoding="utf-8"
        ) as tmp:
            tmp.write(source)
            tmp_path = tmp.name

        try:
            process = subprocess.Popen(
                [deobfuscator_path, tmp_path],
                stdout=subprocess.PIPE,
                stderr=subprocess.PIPE,
                preexec_fn=os.setsid,
            )

            try:
                stdout, _ = process.communicate(timeout=timeout)
                return stdout.decode("utf-8", errors="replace"), process.returncode
            except subprocess.TimeoutExpired:
                # Kill the entire process group so spawned helpers (e.g. node
                # subprocesses webcrack itself launches) get cleaned up too.
                try:
                    os.killpg(os.getpgid(process.pid), signal.SIGTERM)
                    process.wait(timeout=5)
                except Exception:
                    try:
                        os.killpg(os.getpgid(process.pid), signal.SIGKILL)
                    except Exception:
                        pass
                log.warning(f"Deobfuscation timed out after {timeout}s")
                return None, -1
        finally:
            try:
                os.unlink(tmp_path)
            except Exception:
                pass
    except FileNotFoundError:
        log.debug(f"Deobfuscator binary not found at {deobfuscator_path}")
        return None, -1
    except Exception as e:
        log.error(f"Error running deobfuscator: {e}")
        return None, -1


def _try_jsbeautifier(source: str, log) -> Tuple[Optional[str], Optional[str]]:
    """Fallback path: format the source with jsbeautifier. Returns
    `(text, "jsbeautifier")` or `(None, None)` if jsbeautifier isn't installed
    or the call raised."""
    try:
        import jsbeautifier
        opts = jsbeautifier.default_options()
        opts.indent_size = 2
        return jsbeautifier.beautify(source, opts), "jsbeautifier"
    except ImportError:
        log.debug("jsbeautifier not available")
        return None, None
    except Exception as e:
        log.warning(f"jsbeautifier failed: {e}")
        return None, None


def deobfuscate(source: str, log) -> Tuple[Optional[str], Optional[str]]:
    """Run the configured external deobfuscator, falling back to jsbeautifier.

    Returns `(text, normalizer_used)` on success, or `(None, None)` when neither
    path produced non-empty output. `normalizer_used` is the basename of the
    external tool (e.g. `"webcrack"`) or the literal `"jsbeautifier"`.
    """
    if not source:
        return None, None

    deobfuscator_path = os.getenv("JS_DEOBFUSCATOR_PATH", DEFAULT_DEOBFUSCATOR)
    timeout = int(os.getenv("JS_DEOBFUSCATE_TIMEOUT", str(DEFAULT_TIMEOUT_SECS)))

    text, returncode = _run_external(source, deobfuscator_path, timeout, log)
    if text and returncode == 0 and text.strip():
        return text, os.path.basename(deobfuscator_path)

    return _try_jsbeautifier(source, log)