271 lines
7.4 KiB
Go
271 lines
7.4 KiB
Go
// Package intel holds pure, deterministic intelligence helpers for Urutau:
|
|
// attack families, MITRE ATT&CK mapping, network typing, cadence, sessions,
|
|
// campaigns, verdicts, STIX export and metrics. Nothing here performs I/O.
|
|
package intel
|
|
|
|
import (
|
|
"net/url"
|
|
"regexp"
|
|
"strings"
|
|
|
|
"git.lucasrguerra.dev.br/ciberfauna/urutau/internal/core"
|
|
)
|
|
|
|
// Attack family identifiers.
|
|
const (
|
|
FamTraversal = "traversal"
|
|
FamLog4Shell = "log4shell"
|
|
FamSQLi = "sqli"
|
|
FamXSS = "xss"
|
|
FamRCE = "rce-injection"
|
|
FamWebshell = "webshell"
|
|
FamEnvCreds = "env-credentials"
|
|
FamVCS = "vcs"
|
|
FamBackup = "backup"
|
|
FamWordPress = "wordpress"
|
|
FamPhpMyAdmin = "phpmyadmin-db"
|
|
FamActuator = "actuator-debug"
|
|
FamIoTCGI = "iot-cgi"
|
|
FamAdminPanel = "admin-panel"
|
|
FamGeneric404 = "generic-404"
|
|
FamPortScan = "port-scan"
|
|
maxPathScan = 8192
|
|
maxPathScanTop = maxPathScan / 2
|
|
)
|
|
|
|
var backupExts = []string{".sql", ".bak", ".old", ".orig", ".swp", ".zip", ".tar.gz", ".tgz", ".7z", ".rar", ".tar", ".gz"}
|
|
|
|
// Plain substring rules, in priority order. Families that need special
|
|
// handling (traversal raw check, webshell, vcs, backup) are in matchFamily.
|
|
var (
|
|
logRules = []string{"${jndi:"}
|
|
sqliRules = []string{"union select", "' or ", "sleep(", "benchmark(", "information_schema"}
|
|
xssRules = []string{"<script", "javascript:", "onerror="}
|
|
rceRules = []string{";wget", "/bin/sh", "/bin/bash", "cmd=", "|sh", "eval(", "php://", "base64_decode", "allow_url_include", "/vendor/phpunit"}
|
|
envRules = []string{"/.env", "/.aws", "/.ssh", "id_rsa", "/.git-credentials", "wp-config", "/config.json", "credentials", ".npmrc", ".dockercfg"}
|
|
wpRules = []string{"/wp-login.php", "/xmlrpc.php", "/wp-admin", "/wp-content/plugins/", "/wp-includes", "wp-json/wp/v2/users"}
|
|
dbRules = []string{"phpmyadmin", "/pma/", "/adminer", "/mysql", "/sql"}
|
|
actRules = []string{"/actuator", "/_profiler", "/telescope", "/server-status", "phpinfo", "/debug", "/console"}
|
|
iotRules = []string{"/cgi-bin/", "/boaform/", "/hnap1", "/goform", "/shell?cd", "/setup.cgi", "/gponform", "/tmui", "/remote/", "/vpn/", "/owa/", "/ecp/"}
|
|
admRules = []string{"/admin", "/login", "/manager/html", "/user/login"}
|
|
travRules = []string{"../", `..\`, "..;/", "/etc/passwd", "/proc/self"}
|
|
vcsRules = []string{"/.svn", "/.hg", "/.gitconfig"}
|
|
shellSubs = []string{"c99", "r57", "up.php", "xmrlpc"}
|
|
)
|
|
|
|
var hexPair = regexp.MustCompile(`%[0-9a-fA-F]{2}`)
|
|
|
|
func lenientUnescape(s string) string {
|
|
return hexPair.ReplaceAllStringFunc(s, func(m string) string {
|
|
d, err := url.PathUnescape(m)
|
|
if err != nil {
|
|
return m
|
|
}
|
|
return d
|
|
})
|
|
}
|
|
|
|
// prep caps, percent-decodes (up to twice, tolerant of bad escapes) and
|
|
// lowercases a path. It returns the lowercase raw and decoded forms.
|
|
func prep(path string) (raw, dec string) {
|
|
if len(path) > maxPathScan {
|
|
path = path[:maxPathScanTop] + path[len(path)-maxPathScanTop:]
|
|
}
|
|
raw = strings.ToLower(path)
|
|
dec = raw
|
|
for i := 0; i < 2; i++ {
|
|
d, err := url.PathUnescape(dec)
|
|
if err != nil {
|
|
d = lenientUnescape(dec)
|
|
}
|
|
if d == dec {
|
|
break
|
|
}
|
|
dec = d
|
|
}
|
|
return raw, dec
|
|
}
|
|
|
|
func firstSub(s string, subs []string) string {
|
|
for _, m := range subs {
|
|
if strings.Contains(s, m) {
|
|
return m
|
|
}
|
|
}
|
|
return ""
|
|
}
|
|
|
|
func isDelim(s string, i int, delims string) bool {
|
|
return i >= len(s) || strings.IndexByte(delims, s[i]) >= 0
|
|
}
|
|
|
|
// webshellMarker returns the webshell marker found in dec, or "".
|
|
func webshellMarker(dec string) string {
|
|
for from := 0; ; {
|
|
i := strings.Index(dec[from:], "/shell")
|
|
if i < 0 {
|
|
break
|
|
}
|
|
end := from + i + len("/shell")
|
|
if isDelim(dec, end, "/._?-") && !strings.HasPrefix(dec[end:], "?cd") {
|
|
return "/shell"
|
|
}
|
|
from += i + 1
|
|
}
|
|
if m := firstSub(dec, shellSubs); m != "" {
|
|
return m
|
|
}
|
|
for _, name := range []string{"/wso", "/alfa"} {
|
|
for from := 0; ; {
|
|
i := strings.Index(dec[from:], name)
|
|
if i < 0 {
|
|
break
|
|
}
|
|
if isDelim(dec, from+i+len(name), "/.?") {
|
|
return name
|
|
}
|
|
from += i + 1
|
|
}
|
|
}
|
|
if strings.Contains(dec, "/wp-content/uploads/") && strings.Contains(dec, ".php") {
|
|
return "/wp-content/uploads/"
|
|
}
|
|
return ""
|
|
}
|
|
|
|
func vcsMarker(dec string) string {
|
|
for from := 0; ; {
|
|
i := strings.Index(dec[from:], "/.git")
|
|
if i < 0 {
|
|
break
|
|
}
|
|
rest := dec[from+i+5:]
|
|
if !strings.HasPrefix(rest, "hub") && !strings.HasPrefix(rest, "lab") {
|
|
return "/.git"
|
|
}
|
|
from += i + 1
|
|
}
|
|
return firstSub(dec, vcsRules)
|
|
}
|
|
|
|
func pathOnly(p string) string {
|
|
if i := strings.IndexAny(p, "?#"); i >= 0 {
|
|
return p[:i]
|
|
}
|
|
return p
|
|
}
|
|
|
|
func backupMarker(dec string) string {
|
|
p := dec
|
|
if i := strings.IndexAny(p, "?#"); i >= 0 {
|
|
p = p[:i]
|
|
}
|
|
for _, e := range backupExts {
|
|
if strings.HasSuffix(p, e) {
|
|
return e
|
|
}
|
|
}
|
|
return ""
|
|
}
|
|
|
|
// matchFamily finds the first family matching the (already prepared) path.
|
|
func matchFamily(raw, dec string) (fam, marker string) {
|
|
if m := firstSub(dec, travRules); m != "" {
|
|
return FamTraversal, m
|
|
}
|
|
if strings.Contains(raw, "%2e%2e") {
|
|
return FamTraversal, "%2e%2e"
|
|
}
|
|
if m := firstSub(dec, logRules); m != "" {
|
|
return FamLog4Shell, m
|
|
}
|
|
sp := dec
|
|
if strings.IndexByte(dec, '+') >= 0 {
|
|
sp = strings.ReplaceAll(dec, "+", " ")
|
|
}
|
|
if m := firstSub(sp, sqliRules); m != "" {
|
|
return FamSQLi, m
|
|
}
|
|
if m := firstSub(dec, xssRules); m != "" {
|
|
return FamXSS, m
|
|
}
|
|
if m := firstSub(sp, rceRules); m != "" {
|
|
return FamRCE, m
|
|
}
|
|
if core.ForgeBrowse(pathOnly(dec)) { // repository content on a code forge, not a probe
|
|
return "", ""
|
|
}
|
|
if m := webshellMarker(dec); m != "" {
|
|
return FamWebshell, m
|
|
}
|
|
if m := firstSub(dec, envRules); m != "" {
|
|
return FamEnvCreds, m
|
|
}
|
|
if m := vcsMarker(dec); m != "" {
|
|
return FamVCS, m
|
|
}
|
|
if m := backupMarker(dec); m != "" {
|
|
return FamBackup, m
|
|
}
|
|
if m := firstSub(dec, wpRules); m != "" {
|
|
return FamWordPress, m
|
|
}
|
|
if m := firstSub(dec, dbRules); m != "" {
|
|
return FamPhpMyAdmin, m
|
|
}
|
|
if m := firstSub(dec, actRules); m != "" {
|
|
return FamActuator, m
|
|
}
|
|
if m := firstSub(dec, iotRules); m != "" {
|
|
return FamIoTCGI, m
|
|
}
|
|
if m := firstSub(dec, admRules); m != "" {
|
|
return FamAdminPanel, m
|
|
}
|
|
return "", ""
|
|
}
|
|
|
|
var strongFams = map[string]bool{
|
|
FamTraversal: true, FamLog4Shell: true, FamSQLi: true, FamXSS: true, FamRCE: true,
|
|
FamWebshell: true, FamEnvCreds: true, FamVCS: true,
|
|
}
|
|
|
|
// Family classifies a request path into an attack family ("" for benign).
|
|
// Requests the classifier considers plain traffic (core.OK) only keep the
|
|
// strong families unless toolUA is set; SSH and ban events have no family.
|
|
// status is accepted for symmetry with the classifier and does not alter
|
|
// the result. A NotFound request with no other family is "generic-404".
|
|
func Family(path string, cat core.Category, status int, toolUA bool) string {
|
|
switch cat {
|
|
case core.SSHFail, core.SSHProbe, core.SSHOK, core.Ban:
|
|
return ""
|
|
}
|
|
raw, dec := prep(path)
|
|
fam, _ := matchFamily(raw, dec)
|
|
if fam == "" {
|
|
if cat == core.NotFound {
|
|
return FamGeneric404
|
|
}
|
|
return ""
|
|
}
|
|
if cat == core.OK && !toolUA && !strongFams[fam] {
|
|
return ""
|
|
}
|
|
return fam
|
|
}
|
|
|
|
// Marker returns the fragment of the (decoded, lowercase) path that triggered
|
|
// its family, for highlighting in the UI. It is "" when nothing matched.
|
|
func Marker(path string) string {
|
|
raw, dec := prep(path)
|
|
_, m := matchFamily(raw, dec)
|
|
return m
|
|
}
|
|
|
|
// PathFamily returns the family of path ignoring category gating ("" if none).
|
|
func PathFamily(path string) string {
|
|
raw, dec := prep(path)
|
|
f, _ := matchFamily(raw, dec)
|
|
return f
|
|
}
|