Exploit database

GH-47f4be639736fc5e0579bbee434c4d9f

nocerainfosec/php2shell-checker

GitHubNot listedPoC
Download repository ZIP

Sources for CVE-2026-109618

411 sources

Preferred order: Exploit-DB, Metasploit, then GitHub repositories by stars. Open preferred source

Repository files2
nocerainfosec/php2shell-checkerRevision 930ac45bf9c1

Files from this repository share one source choice. Supporting files may cover other vulnerabilities.

lib/php-rules.js

JavaScript · 28,338 bytes
// Regras de classificação do PHP2Shell (CVE-2026-109618 / CVE-2026-109619).
// Fontes: guia técnico da NoceraInfoSec® v1.0 (Tabelas 5, 6 e 7), Hunt.io, Brandefense, ComputingForGeeks,
// Aikido e watchTowr (todos de 21-22/10/2026). Atualize este arquivo quando sairem novos boletins.
// Todo texto sai como mensagem { k, p } (ver lib/i18n.js).
import { msg } from "./i18n.js";

export const RULES_VERSION = "2026-10-23";

// Muda a cada versão do código. O popup compara com o service worker para detectar quando os arquivos foram
// atualizados no disco (git pull) mas o Chrome ainda roda o service worker antigo.
export const CODE_VERSION = "1.4.0";

// Primeira versão com patch por branch suportado.
export const FIXED = {
  "8.5": "8.5.12",
  "8.4": "8.4.27",
  "8.3": "8.3.36",
  "8.2": "8.2.35",
};

// Branches acima do maior listado foram lançados depois do patch.
const NEWEST_KNOWN_BRANCH = [8, 5];

// Pacotes de distribuição corrigidos por backport (versão dpkg, que o Debian/Ubuntu expõe no X-Powered-By).
export const DISTRO_FIXED = [
  { distro: "Ubuntu 26.10", branch: "8.5", match: /0ubuntu2\./i, pkg: "8.5.9-0ubuntu2.1", ref: "USN-8948-1" },
  { distro: "Ubuntu 26.04", branch: "8.5", match: /0ubuntu1\./i, pkg: "8.5.4-0ubuntu1.5", ref: "USN-8948-1" },
  { distro: "Ubuntu 24.04", branch: "8.3", match: /ubuntu0\.24\.04/i, pkg: "8.3.6-0ubuntu0.24.04.13", ref: "USN-8948-1" },
  { distro: "Ubuntu 22.04", branch: "8.1", match: /ubuntu2(\.|$)/i, pkg: "8.1.2-1ubuntu2.28", ref: "USN-8948-1" },
  { distro: "Ubuntu 20.04 ESM", branch: "7.4", match: /ubuntu2(\.|$)/i, pkg: "7.4.3-4ubuntu2.29+esm5", ref: "USN-8948-2" },
  { distro: "Debian 13", branch: "8.4", match: /deb13u/i, pkg: "8.4.27-1~deb13u1", ref: "DSA-6574-1" },
  { distro: "Debian 12", branch: "8.2", match: /deb12u/i, pkg: "8.2.35-1~deb12u1", ref: "DSA-6574-1 / DLA-4848-1" },
];

// Revisões do repositório Sury reportadas com o patch (relato não oficial).
export const SURY_FIXED = {
  "5.6": "5.6.40-104", "7.0": "7.0.33-92", "7.1": "7.1.33-80", "7.2": "7.2.34-66",
  "7.3": "7.3.33-35", "7.4": "7.4.33-31", "8.0": "8.0.30-25", "8.1": "8.1.34-9",
  "8.2": "8.2.35-1", "8.3": "8.3.36-1", "8.4": "8.4.27-1", "8.5": "8.5.12-1",
};

// Últimas releases de branches EOL para as quais fornecedores publicaram builds corrigidas
// que continuam anunciando o mesmo número (ComputingForGeeks, Brandefense e Aikido, 22/10/2026).
export const VENDOR_BACKPORTS = {
  "7.4.33": ["RHEL 8 php:7.4 (7.4.33-8)", "Remi 7.4.33-31", "cPanel ea-php74", "CloudLinux alt-php74", "Aikido php:7.4.33-fpm"],
  "8.0.30": ["RHEL 9 (8.0.30-8.el9_8.1)", "AlmaLinux 8.0.30-8.el9_8.alma.1", "Remi 8.0.30-21", "cPanel ea-php80", "CloudLinux alt-php80"],
  "8.1.34": ["Remi 8.1.34-6", "cPanel ea-php81", "CloudLinux alt-php81"],
  "8.1.32": ["AlmaLinux 8.1.32-1"],
  "7.3.33": ["Aikido php:7.3.33-fpm"],
};

// Imagens corrigidas da Aikido mantêm a tag da versão anterior ao patch.
export const AIKIDO_SAME_TAG = new Set(["8.5.11", "8.4.26", "8.3.35", "8.2.34"]);

export const STATUS = {
  VULNERABLE: "vulnerable",     // versão anunciada sem patch
  EOL: "eol",                   // 8.1 ou anterior, sem release oficial
  CHECK: "check",               // depende de backport: confirmar no servidor
  BACKPORT: "backport",         // pacote de distro corrigido conhecido
  PATCHED: "patched",           // versão anunciada >= versão com patch
  NO_VERSION: "no_version",     // há indício de PHP mas a versão não é exposta
  NOT_DETECTED: "not_detected", // nenhum indício de PHP
};

export const STATUS_INFO = {
  vulnerable:   { severity: 5, color: "#d93025", badge: "#d93025" },
  eol:          { severity: 5, color: "#a50e0e", badge: "#a50e0e" },
  check:        { severity: 4, color: "#e37400", badge: "#e37400" },
  no_version:   { severity: 3, color: "#b06000", badge: "#5f6368" },
  backport:     { severity: 2, color: "#188038", badge: "#188038" },
  patched:      { severity: 1, color: "#188038", badge: "#188038" },
  not_detected: { severity: 0, color: "#5f6368", badge: "#5f6368" },
};

export const statusLabel = (status) => msg(`status.${status}`);

// Extrai a primeira versão do PHP de um texto (X-Powered-By, Server, phpinfo, etc.).
// Ex.: "PHP/8.1.2-1ubuntu2.14", "Apache/2.4.52 (Ubuntu) PHP/8.3.6", "PHP Version 8.4.27RC1".
export function extractPhpVersion(text) {
  if (!text) return null;
  const m = /PHP(?:\/|\s+Version\s+|\s+)v?(\d+)\.(\d+)(?:\.(\d+))?([0-9A-Za-z.+~:_-]*)/i.exec(text);
  if (!m) return null;
  return {
    raw: m[0],
    major: +m[1],
    minor: +m[2],
    patch: m[3] === undefined ? null : +m[3],
    suffix: m[4] || "",
    full: `${m[1]}.${m[2]}${m[3] !== undefined ? "." + m[3] : ""}${m[4] || ""}`,
  };
}

function cmpTriple(a, b) {
  for (let i = 0; i < 3; i++) if (a[i] !== b[i]) return a[i] - b[i];
  return 0;
}

const triple = (s) => s.split(".").map(Number);

// Comparação de versões no estilo dpkg (epoch:upstream-revision, com "~" < "").
export function dpkgCompare(a, b) {
  const order = (c) => {
    if (c === "~") return -1;
    if (/\d/.test(c)) return 0;
    if (!c) return 0;
    if (/[A-Za-z]/.test(c)) return c.charCodeAt(0);
    return c.charCodeAt(0) + 256;
  };
  const cmpPart = (x, y) => {
    let i = 0, j = 0;
    while (i < x.length || j < y.length) {
      let diff = 0;
      while ((i < x.length && !/\d/.test(x[i])) || (j < y.length && !/\d/.test(y[j]))) {
        const ox = order(x[i] || ""), oy = order(y[j] || "");
        if (ox !== oy) return ox - oy;
        i++; j++;
      }
      while (x[i] === "0") i++;
      while (y[j] === "0") j++;
      while (i < x.length && /\d/.test(x[i]) && j < y.length && /\d/.test(y[j])) {
        if (!diff) diff = x.charCodeAt(i) - y.charCodeAt(j);
        i++; j++;
      }
      if (i < x.length && /\d/.test(x[i])) return 1;
      if (j < y.length && /\d/.test(y[j])) return -1;
      if (diff) return diff;
    }
    return 0;
  };
  const split = (v) => {
    const e = v.indexOf(":");
    const epoch = e >= 0 ? +v.slice(0, e) : 0;
    const rest = e >= 0 ? v.slice(e + 1) : v;
    const d = rest.lastIndexOf("-");
    return [epoch, d >= 0 ? rest.slice(0, d) : rest, d >= 0 ? rest.slice(d + 1) : "0"];
  };
  const [ea, ua, ra] = split(a);
  const [eb, ub, rb] = split(b);
  return ea - eb || cmpPart(ua, ub) || cmpPart(ra, rb);
}

const DISTRO_HINT = /(ubuntu|deb\d*u?|debian|\.el\d|el\d|amzn|fc\d|suse|sury|alpine|\+b\d|remi)/i;
const PRERELEASE = /^(RC|alpha|beta|dev)/i;

// Release da distribuição a partir do nome do pacote (só a convenção de nomes, sem afirmar suporte).
const UBUNTU_BY_BASE = {
  "5.3.2": "Ubuntu 10.04", "5.3.6": "Ubuntu 11.04", "5.3.10": "Ubuntu 12.04", "5.4.9": "Ubuntu 13.04",
  "5.5.9": "Ubuntu 14.04", "7.0.33": "Ubuntu 16.04", "7.2.24": "Ubuntu 18.04", "7.4.3": "Ubuntu 20.04",
  "8.1.2": "Ubuntu 22.04", "8.3.6": "Ubuntu 24.04",
};
export function distroRelease(base, suffix) {
  if (/sury/i.test(suffix)) {
    const os = /\+(ubuntu|debian)(\d+(?:\.\d+)?)/i.exec(suffix);
    return os ? `Sury (${os[1][0].toUpperCase()}${os[1].slice(1)} ${os[2]})` : "Sury";
  }
  const ubuntu = /ubuntu0\.(\d\d\.\d\d)/i.exec(suffix);
  if (ubuntu) return `Ubuntu ${ubuntu[1]}`;
  if (/ubuntu/i.test(suffix) && UBUNTU_BY_BASE[base]) return UBUNTU_BY_BASE[base];
  const deb = /deb(\d+)u/i.exec(suffix);
  if (deb) return `Debian ${deb[1]}`;
  const el = /\.el(\d+)/i.exec(suffix);
  if (el) return `RHEL ${el[1]}`;
  return null;
}

// Classifica uma versão anunciada. Retorna { status, title, detail[], actions[] }.
export function classifyVersion(v) {
  const branch = `${v.major}.${v.minor}`;
  const suffix = v.suffix;
  const base = `${v.major}.${v.minor}.${v.patch ?? 0}`;
  const pkg = `${base}${suffix}`;
  const fixed = FIXED[branch];
  const backportNote = msg("note.backport");

  if (v.patch === null && fixed) {
    // Só "PHP/8.3" (sem patch): não dá para concluir dentro de branches suportados.
    return {
      status: STATUS.CHECK,
      title: msg("v.noPatch.title", { branch }),
      detail: [msg("v.noPatch.detail", { branch, fixed })],
      actions: [msg("act.confirmServer")],
    };
  }

  const isNewerBranch =
    v.major > NEWEST_KNOWN_BRANCH[0] ||
    (v.major === NEWEST_KNOWN_BRANCH[0] && v.minor > NEWEST_KNOWN_BRANCH[1]);
  if (isNewerBranch) {
    return {
      status: STATUS.PATCHED,
      title: msg("v.newer.title", { v: v.full }),
      detail: [msg("v.newer.detail")],
      actions: [msg("act.confirmCritical")],
    };
  }

  // Builds de pré-release (8.4.27RC1, 8.5.12RC1) NÃO contêm a correção.
  if (PRERELEASE.test(suffix)) {
    return {
      status: STATUS.VULNERABLE,
      title: msg("v.pre.title", { v: v.full }),
      detail: [msg("v.pre.detail")],
      actions: [msg("act.upgradeStable", { fixed: fixed || "8.2.35+" })],
    };
  }

  if (fixed && cmpTriple([v.major, v.minor, v.patch], triple(fixed)) >= 0) {
    return {
      status: STATUS.PATCHED,
      title: msg("v.patched.title", { v: v.full }),
      detail: [msg("v.patched.detail", { fixed })],
      actions: [msg("act.confirmRestart")],
    };
  }

  // Abaixo da versão corrigida, ou branch sem suporte. Verificar backports conhecidos.
  if (/sury/i.test(suffix)) {
    const ref = SURY_FIXED[branch];
    const debVer = pkg.replace(/\+.*$/, "");
    if (ref && dpkgCompare(debVer, ref) >= 0) {
      return {
        status: STATUS.CHECK,
        title: msg("v.suryOk.title", { v: v.full }),
        detail: [msg("v.suryOk.detail", { ref })],
        actions: [msg("act.suryChangelog", { branch })],
      };
    }
    if (ref) {
      return {
        status: STATUS.VULNERABLE,
        title: msg("v.suryOld.title", { v: v.full }),
        detail: [msg("v.suryOld.detail", { ref })],
        actions: [msg("act.suryUpdate", { branch })],
      };
    }
  }

  for (const d of DISTRO_FIXED) {
    if (d.branch === branch && d.match.test(suffix)) {
      if (dpkgCompare(pkg, d.pkg) >= 0) {
        return {
          status: STATUS.BACKPORT,
          title: msg("v.distroOk.title", { v: v.full, distro: d.distro }),
          detail: [msg("v.distroOk.detail", { pkg: d.pkg, ref: d.ref })],
          actions: [msg("act.restartAfterPkg")],
        };
      }
      return {
        status: STATUS.VULNERABLE,
        title: msg("v.distroOld.title", { v: v.full, distro: d.distro }),
        detail: [msg("v.distroOld.detail", { pkg: d.pkg, ref: d.ref })],
        actions: [msg("act.distroUpdate", { pkg: d.pkg })],
      };
    }
  }

  // Build que o próprio administrador rotulou como corrigida (visto no Shodan: "7.4.33-php2shell-fix").
  if (/php2shell|109618|109619/i.test(suffix)) {
    return {
      status: STATUS.CHECK,
      title: msg("v.customFix.title", { v: v.full }),
      detail: [msg("v.customFix.detail")],
      actions: [msg("act.confirmPkg")],
    };
  }

  // Dotdeb foi descontinuado: o sufixo parece Debian, mas não há quem publique backport.
  const dotdeb = /dotdeb/i.test(suffix);
  if (!dotdeb && DISTRO_HINT.test(suffix)) {
    const distro = distroRelease(base, suffix);
    return {
      status: STATUS.CHECK,
      title: distro
        ? msg(fixed ? "v.distroNamed.title" : "v.distroNamed.titleEol", { v: v.full, distro })
        : msg(fixed ? "v.distroCheck.title" : "v.distroCheck.titleEol", { v: v.full }),
      detail: [msg("v.distroCheck.detail")],
      actions: [msg("act.confirmPkg"), msg("act.ifNoBackport")],
    };
  }

  // Número "puro" que fornecedores mantêm em builds corrigidas (RHEL, Remi, cPanel, CloudLinux, Aikido).
  if (!suffix && VENDOR_BACKPORTS[base]) {
    return {
      status: STATUS.CHECK,
      title: msg("v.vendorLast.title", { v: base, branch }),
      detail: [msg("v.vendorLast.detail", { v: base, branch, vendors: VENDOR_BACKPORTS[base].join(", ") })],
      actions: [msg("act.confirmPkg"), msg("act.migrate")],
    };
  }

  if (!fixed) {
    return {
      status: STATUS.EOL,
      title: msg("v.eol.title", { v: v.full }),
      detail: [msg("v.eol.detail"), dotdeb ? msg("note.dotdeb") : backportNote],
      actions: [msg("act.migrate"), msg("act.confirmBeforeClose")],
    };
  }

  const detail = [msg("v.vuln.detail", { branch, fixed }), backportNote];
  if (!suffix && AIKIDO_SAME_TAG.has(base)) detail.push(msg("note.aikido", { v: base }));
  return {
    status: STATUS.VULNERABLE,
    title: msg("v.vuln.title", { v: v.full }),
    detail,
    actions: [msg("act.upgrade", { fixed }), msg("act.stopgap"), msg("act.confirmBeforeClose")],
  };
}

// Lê a tabela de um phpinfo() (HTML). Funciona em string, sem DOM, para rodar no service worker.
export function parsePhpinfo(html) {
  if (!html || !/phpinfo\(\)|<h1 class="p">PHP Version/i.test(html)) return null;
  const cell = (name) => {
    const re = new RegExp(`<td class="e">\\s*${name.replace(/\./g, "\\.")}\\s*</td>\\s*<td class="v">\\s*(.*?)\\s*</td>`, "i");
    const m = re.exec(html);
    return m ? m[1].replace(/<[^>]+>/g, "").trim() : null;
  };
  const h1 = /<h1 class="p">\s*(PHP Version [^<]+)<\/h1>/i.exec(html);
  const title = /<title>\s*(PHP [^<]*?)\s*-\s*phpinfo\(\)/i.exec(html);
  return {
    versionText: h1 ? h1[1] : title ? title[1] : null,
    sapi: cell("Server API"),
    uploadProgress: cell("session.upload_progress.enabled"),
    memoryLimit: cell("memory_limit"),
    hardLimit: cell("memory_hard_limit_percent") !== null,
    exposePhp: cell("expose_php"),
  };
}

// Combina as evidências de um host em um veredito.
export function assess(host) {
  const versions = host.versions || [];
  if (versions.length) {
    // Usa a versão com maior severidade (pior caso) quando o host anuncia mais de uma.
    let worst = null;
    for (const v of versions) {
      const c = { ...classifyVersion(v), version: v };
      if (!worst || STATUS_INFO[c.status].severity > STATUS_INFO[worst.status].severity) worst = c;
    }
    // memory_hard_limit_percent só existe em builds com a correção (ComputingForGeeks).
    if (host.phpinfo && host.phpinfo.hardLimit && worst.status !== STATUS.PATCHED) {
      return {
        status: STATUS.BACKPORT,
        title: msg("v.phpinfoFixed.title", { v: worst.version.full }),
        detail: [msg("v.phpinfoFixed.detail")],
        actions: [msg("act.removePhpinfo"), msg("act.confirmRestart")],
        version: worst.version,
      };
    }
    return worst;
  }
  const indicators = host.indicators || [];
  // Sem versão legível, o formato do PHPSESSID ainda pode fixar um teto: um ID de sessão <=7.0 significa EOL.
  const sess = indicators.find((i) => i.kind === "session" && i.value && i.value.p);
  if (sess && sess.value.p.range === "<=7.0") {
    return {
      status: STATUS.EOL,
      title: msg("v.inferred.title", { range: "≤7.0" }),
      detail: [msg("v.inferred.detail"), msg("v.inferred.caveat")],
      actions: [msg("act.migrate"), msg("act.confirmHosting")],
      badge: "≤7.0",
    };
  }
  if (indicators.some((i) => i.kind !== "ioc")) {
    return {
      status: STATUS.NO_VERSION,
      title: msg("a.noVersion.title"),
      detail: [msg("a.noVersion.detail")],
      actions: [msg("act.confirmHosting")],
    };
  }
  return {
    status: STATUS.NOT_DETECTED,
    title: msg("a.none.title"),
    detail: [msg("a.none.detail")],
    actions: [],
  };
}

// Produtos que rodam em PHP e aparecem no X-Powered-By sem a palavra "PHP".
const PHP_PRODUCTS =
  /W3 Total Cache|WP Engine|WordPress|WP Rocket|Craft CMS|ProcessWire|Laravel|Symfony|Drupal|Joomla|Magento|PrestaShop|TYPO3|Kirby|Contao|Concrete|OpenCart|WooCommerce|Nextcloud|ownCloud|phpBB|MediaWiki|Moodle|PleskLin|Fat-Free Framework|Nette Framework|BoidCMS|CodeIgniter|CakePHP|Yii|Zend|Laminas|Slim|Phalcon|Kohana|Grav|Statamic|October CMS/i;

// Cookies que indicam PHP ou um framework/CMS em PHP.
export const COOKIE_HINTS = [
  [/^PHPSESSID$/i, "PHP"],
  [/^laravel_session$/i, "Laravel"],
  [/^ci_session$/i, "CodeIgniter"],
  [/^CAKEPHP$/i, "CakePHP"],
  [/^(wordpress_|wp-settings|wordpress_logged_in)/i, "WordPress"],
  [/^S?SESS[0-9a-f]{32}$/i, "Drupal"],
  [/^PrestaShop-/i, "PrestaShop"],
  [/^(frontend|adminhtml|mage-cache-sessid)$/i, "Magento"],
  [/^symfony$|^sf_redirect$/i, "Symfony"],
  [/^MOODLEID_|^MoodleSession/i, "Moodle"],
  [/^zbx_session$/i, "Zabbix"],
  [/^phpMyAdmin|^pma_/i, "phpMyAdmin"],
  [/^roundcube_sessid$/i, "Roundcube"],
  [/^GLPI_/i, "GLPI"],
  [/^WHMCS/i, "WHMCS"],
  [/^(oc_sessionPassphrase|__Host-nc_sameSiteCookie(lax|strict)|nc_session_id)$/i, "Nextcloud/ownCloud"],
  [/^Cacti$/i, "Cacti"],
  [/^OpenEMR$/i, "OpenEMR"],
  [/^KIMAI_/i, "Kimai"],
];

// Cabeçalhos que indicam PHP mesmo sem a versão.
export const HEADER_HINTS = [
  [/^x-pingback$/i, () => "WordPress (X-Pingback / xmlrpc.php)"],
  [/^x-generator$/i, (v) => (/drupal|wordpress|joomla|typo3/i.test(v) ? msg("ind.generator", { v }) : null)],
  [/^x-drupal-(cache|dynamic-cache)$/i, () => "Drupal"],
  [/^x-magento-/i, () => "Magento"],
  [/^x-powered-cms$/i, (v) => `CMS: ${v}`],
  [/^x-php-response-code$/i, () => "X-PHP-Response-Code"],
  [/^x-php-pid$/i, () => "X-PHP-PID"],
  [/^link$/i, (v) => (/wp-json|api\.w\.org/i.test(v) ? "WordPress (REST API, Link header)" : null)],
  // X-Powered-By sem "PHP" só conta quando nomeia um produto que roda em PHP.
  [/^x-powered-by$/i, (v) => (!/PHP/i.test(v) && PHP_PRODUCTS.test(v) ? `X-Powered-By: ${v}` : null)],
];

// ---------- Fingerprint do formato do PHPSESSID (infere um teto de versão) ----------
// O PHP <= 7.0 montava o ID a partir de session.hash_function (0=md5 128 bits, 1=sha256 256 bits) com
// session.hash_bits_per_character (4/5/6 bits), tirando caracteres do hexconvtab:
//   4 -> "0-9a-f" (32 símbolos), 5 -> "0-9a-v" (32 símbolos), 6 -> "0-9a-zA-Z,-" (64 símbolos)
//   comprimento = ceil(bits_do_hash / bits_por_char)
// O PHP >= 7.1 REMOVEU essas duas diretivas (e a opção sha256), trocando-as por session.sid_length /
// session.sid_bits_per_character (padrões 32 / 4). Por isso alguns pares comprimento+alfabeto só existem
// em <= 7.0, outros só em >= 7.1 e o padrão 32-hex é ambíguo. Verificado em php-src 5.6.40/7.0.33 vs 7.4.
const SID_B6 = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ,-"; // alfabeto de 6 bits
const SID_B5 = "0123456789abcdefghijklmnopqrstuv"; // primeiros 32 do hexconvtab (5 bits)
const SID_B4 = "0123456789abcdef"; // hex (4 bits)

function sidAlphabet(id) {
  if ([...id].every((c) => SID_B4.includes(c))) return 4;
  if ([...id].every((c) => SID_B5.includes(c))) return 5;
  if ([...id].every((c) => SID_B6.includes(c))) return 6;
  return 0; // não parece um ID nativo do PHP
}

// Retorna null quando o ID não carrega sinal útil, senão { range, confidence, bits, n }.
export function fingerprintSession(value) {
  if (!value || !/^[0-9a-zA-Z,-]+$/.test(value)) return null;
  const n = value.length;
  const bits = sidAlphabet(value);
  if (!bits) return null;
  // Combinações que exigem as diretivas session.* do PHP <= 7.0 (removidas no 7.1):
  // md5 em 5/6 bits -> 26 / 22 chars; sha256 em 4/5/6 bits -> 64 / 52 / 43 chars.
  const LEGACY = new Set(["26-5", "22-6", "64-4", "52-5", "43-6"]);
  if (LEGACY.has(`${n}-${bits}`)) return { range: "<=7.0", confidence: "high", bits, n };
  // Alfabeto de 6 bits em um comprimento não-legado só existe no >= 7.1 (session.sid_bits_per_character=6).
  if (bits === 6 && n !== 22) return { range: ">=7.1", confidence: "high", bits, n };
  // 32/4-bit hex é o padrão nas duas eras -> sem sinal.
  return null;
}

// Cabeçalhos de cache-limiter que o PHP emite ao iniciar uma sessão (session.cache_limiter padrão "nocache").
// A data "Thu, 19 Nov 1981 08:52:00 GMT" é fixa no php-src (ext/session/session.c), então prova PHP mesmo com
// expose_php=Off e o cookie renomeado.
export const PHP_SESSION_EXPIRES = "Thu, 19 Nov 1981 08:52:00 GMT";
export function isPhpSessionCacheLimiter(expires, cacheControl, pragma) {
  return (
    (expires || "").trim() === PHP_SESSION_EXPIRES &&
    /no-store/i.test(cacheControl || "") &&
    /no-cache/i.test(cacheControl || "") &&
    /must-revalidate/i.test(cacheControl || "") &&
    /no-cache/i.test(pragma || "")
  );
}

// Analisa os cabeçalhos de uma resposta. Função pura, usada pelo service worker e pelo QA com dados do Shodan.
// Retorna { headers: {server, x-powered-by}, versions: [{ v, source }], indicators: [{ kind, value }] }.
export function analyzeHeaders(responseHeaders) {
  const headers = {};
  const versions = [];
  const indicators = [];
  const cookieNames = [];
  let expires = "", cacheControl = "", pragma = "";
  for (const { name, value = "" } of responseHeaders) {
    const n = name.toLowerCase();

    // PHP DebugBar ligado em produção vaza consultas, requisições e variáveis.
    if (/^phpdebugbar/i.test(n)) {
      indicators.push({ kind: "alert", value: msg("ind.debugbar") });
      continue;
    }

    // Guarda os cabeçalhos de cache-limiter para o teste de sessão do PHP após o loop.
    if (n === "expires") expires = value;
    else if (n === "cache-control") cacheControl = value;
    else if (n === "pragma") pragma = value;

    if (n === "x-powered-by" || n === "server") {
      // Vários X-Powered-By na mesma resposta (ex.: "PHP/8.1" e "PleskLin") são somados.
      headers[n] = headers[n] ? `${headers[n]}, ${value}` : value;
      const v = extractPhpVersion(value);
      if (v) versions.push({ v, source: `${name}: ${value}` });
      // "php", "ThinkPHP", "FrankenPHP Caddy", "PHP/qy7": há PHP, mas sem versão legível.
      else if (/php/i.test(value)) indicators.push({ kind: "header", value: `${name}: ${value}` });
    }

    if (n === "set-cookie") {
      const cookieName = value.split("=")[0].trim();
      cookieNames.push(cookieName);
      for (const [re, label] of COOKIE_HINTS) {
        if (re.test(cookieName)) indicators.push({ kind: "cookie", value: msg("ind.cookie", { name: cookieName, label }) });
      }
      if (/^PHPSESSID$/i.test(cookieName)) {
        const val = value.split(";")[0].slice(cookieName.length + 1);
        if (val.length > 128) indicators.push({ kind: "alert", value: msg("ind.bigSess", { n: val.length }) });
        const fp = fingerprintSession(val);
        // O range vai dentro do próprio value (msg) porque o service worker só preserva { kind, value }.
        if (fp) indicators.push({ kind: "session", value: msg("ind.session", { n: fp.n, bits: fp.bits, range: fp.range }) });
      }
      continue;
    }

    for (const [re, fn] of HEADER_HINTS) {
      if (re.test(n)) {
        const label = fn(value);
        if (label) indicators.push({ kind: "header", value: label });
      }
    }
  }
  // Laravel envia XSRF-TOKEN junto com "<nome_do_app>_session" (o nome padrão laravel_session já é coberto).
  const appSession = cookieNames.find((c) => /_session$/i.test(c) && !/^laravel_session$/i.test(c));
  if (appSession && cookieNames.some((c) => /^XSRF-TOKEN$/i.test(c))) {
    indicators.push({ kind: "cookie", value: msg("ind.cookie", { name: `XSRF-TOKEN + ${appSession}`, label: "Laravel" }) });
  }
  // Prova passiva de PHP mesmo sem versão nem cookie legível (cabeçalhos de sessão com a data fixa do php-src).
  if (isPhpSessionCacheLimiter(expires, cacheControl, pragma)) {
    indicators.push({ kind: "header", value: msg("ind.cacheLimiter") });
  }
  return { headers, versions, indicators };
}

// Painéis de stacks de desenvolvimento expostos na internet (QA Shodan: 66 hosts reais com WampServer ou Laragon).
// Eles listam as versões instaladas; no WampServer, "PHP x -> Apache module" é a versão que atende o site.
const DEV_DASHBOARDS = [
  [/WAMPSERVER/i, "WampServer"],
  [/Laragon/i, "Laragon"],
  [/XAMPP/i, "XAMPP"],
  [/\bMAMP\b/i, "MAMP"],
];
export function parseDevDashboard(title, text) {
  const hit = DEV_DASHBOARDS.find(([re]) => re.test(title || ""));
  if (!hit) return null;
  const t = (text || "").replace(/\s+/g, " ");
  const versions = [];
  let modphp = false;
  const wampModule = /PHP\s+(\d+\.\d+\.\d+)\s*->\s*Apache module/i.exec(t);
  if (wampModule) {
    versions.push(`PHP ${wampModule[1]}`);
    modphp = true;
  }
  const label = /PHP version\s*:?\s*(\d+\.\d+\.\d+)/i.exec(t);
  if (!versions.length && label) versions.push(`PHP ${label[1]}`);
  const sig = /Apache\/[\d.]+[^P]{0,80}PHP\/(\d+\.\d+\.\d+)/i.exec(t);
  if (!versions.length && sig) versions.push(`PHP ${sig[1]}`);
  // Desde o XAMPP 5.5 o pacote leva o número do PHP que embarca ("XAMPP for Windows 8.2.12" = PHP 8.2.12).
  const xampp = /XAMPP(?: for (?:Windows|Linux|OS X))?\s+v?((\d+)\.\d+\.\d+)/i.exec(`${title} ${t}`);
  if (!versions.length && xampp && +xampp[2] >= 5) versions.push(`PHP ${xampp[1]}`);
  return { name: hit[1], versions, modphp };
}

// Assinatura do Apache em páginas de erro e listagens ("Apache/2.4.39 (Win64) PHP/7.2.18 Server at ... Port 80").
// Mostra a versão mesmo quando um proxy remove o cabeçalho Server. No QA: 164 hosts reais, nenhum honeypot.
export function versionsFromSignatures(texts) {
  const out = [];
  for (const text of texts || []) {
    const v = extractPhpVersion(text);
    if (v) out.push({ v, source: `Apache signature: ${text.trim().slice(0, 120)}` });
  }
  return out;
}

// Pistas de SAPI a partir dos cabeçalhos. O PHP classifica Apache + mod_php como o cenário de maior risco.
export function sapiFromHeaders(server = "", xpb = "") {
  const hints = [];
  const hasPhp = /PHP/i.test(xpb) || /PHP/i.test(server);
  if (/Apache/i.test(server) && /PHP\//i.test(server)) hints.push(msg("sapi.modphp"));
  if (/LiteSpeed/i.test(server)) hints.push(msg("sapi.litespeed"));
  if ((/Caddy|FrankenPHP/i.test(server) && hasPhp) || /FrankenPHP/i.test(xpb)) hints.push(msg("sapi.caddy"));
  if (/nginx|openresty/i.test(server) && hasPhp) hints.push(msg("sapi.nginx"));
  return hints;
}

// ---------- Infraestrutura de ataque do PHP2Shell ----------
// Fonte: Hunt.io, "Hunting Leaked PHP2Shell RCE Across Open Directories" (22/10/2026).
// Serve para o analista que abre diretórios abertos de atacantes no navegador.

export const IOC_SOURCE = "Hunt.io (2026-10-22)";

export const IOC_IPS = {
  "85.11.167.243": "ioc.scanner",
  "92.118.112.100": "ioc.payload",
  "89.32.41.16": "ioc.botnet",
  "89.32.41.50": "ioc.botnet",
  "45.198.224.88": "ioc.botnet",
  "91.90.123.25": "ioc.research",
  "102.210.149.166": "ioc.staging",
  "122.10.110.72": "ioc.staging",
  "122.10.17.49": "ioc.staging",
  "58.221.60.160": "ioc.probeDir",
  "206.188.197.105": "ioc.toolkitDir",
  "41.223.30.7": "ioc.staging",
  "157.230.117.70": "ioc.staging",
  "104.219.251.31": "ioc.shellDir",
  "107.150.31.151": "ioc.staging",
  "217.160.132.82": "ioc.staging",
  "47.100.125.108": "ioc.staging",
  "54.169.109.7": "ioc.staging",
  "139.162.165.136": "ioc.staging",
};

// 45.198.224.0/24 (AS215925, VPSVAULT): rede de scanner.
export const IOC_PREFIXES = [["45.198.224.", "ioc.scannerNet"]];

export const IOC_DOMAINS = [
  ["botlesscucks.st", "ioc.c2"],
  ["godofthunder.st", "ioc.c2"],
  ["gettingheadfromduckysmom.st", "ioc.c2"],
  ["maste.mangsud.org", "ioc.shellSrc"],
];

// Arquivos vistos nos diretórios abertos dos atacantes.
export const IOC_FILES = [
  [/^aslr_exploit\.py$/i, "ioc.toolkit"],
  [/^deep_probe\.py$/i, "ioc.toolkit"],
  [/^c2\.py$/i, "ioc.toolkit"],
  [/^probe_rfc1867\.py$/i, "ioc.probe"],
  [/^verify_fixation\.py$/i, "ioc.probe"],
  [/^autologin_fd\.php$/i, "ioc.webshell"],
  [/^tebaran_/i, "ioc.deployer"],
  [/php2shell|109618|109619/i, "ioc.named"],
];

// Retorna a chave do rótulo do IOC ou null.
export function matchIocHost(hostname) {
  if (IOC_IPS[hostname]) return IOC_IPS[hostname];
  for (const [prefix, label] of IOC_PREFIXES) if (hostname.startsWith(prefix)) return label;
  for (const [domain, label] of IOC_DOMAINS) if (hostname === domain || hostname.endsWith("." + domain)) return label;
  return null;
}

// shell.php só conta junto com outro arquivo do toolkit, por ser um nome genérico.
export function matchIocFiles(names) {
  const hits = [];
  for (const name of names) {
    for (const [re, label] of IOC_FILES) {
      if (re.test(name)) {
        hits.push({ file: name, label });
        break;
      }
    }
  }
  if (hits.length && names.some((n) => /^shell\.php$/i.test(n))) hits.push({ file: "shell.php", label: "ioc.webshell" });
  return hits;
}