mirror of
https://github.com/Ed1s0nZ/CyberStrikeAI.git
synced 2026-12-01 16:13:23 +01:00
153 lines
5.9 KiB
Go
153 lines
5.9 KiB
Go
package database
|
|
|
|
import (
|
|
"net/netip"
|
|
"sort"
|
|
"strings"
|
|
|
|
"golang.org/x/net/publicsuffix"
|
|
)
|
|
|
|
// VulnerabilityNavigation contains counts across the entire filtered result set.
|
|
// Records can belong to several hosts, so parent counts are deduplicated by ID.
|
|
type VulnerabilityNavigation struct {
|
|
Key string `json:"key"`
|
|
Label string `json:"label"`
|
|
Count int `json:"count"`
|
|
Severities map[string]int `json:"severities"`
|
|
Pending int `json:"pending"`
|
|
Children []*VulnerabilityNavigation `json:"children,omitempty"`
|
|
}
|
|
|
|
type vulnerabilityNavigationCount struct {
|
|
node *VulnerabilityNavigation
|
|
ids map[string]bool
|
|
}
|
|
|
|
func (g *vulnerabilityNavigationCount) add(id, severity, status string) {
|
|
if g.ids[id] {
|
|
return
|
|
}
|
|
g.ids[id] = true
|
|
g.node.Count++
|
|
g.node.Severities[severity]++
|
|
if status == "" || status == "open" || status == "confirmed" {
|
|
g.node.Pending++
|
|
}
|
|
}
|
|
|
|
const vulnerabilityQueueIDExpr = `COALESCE((SELECT bt.queue_id FROM batch_tasks bt WHERE bt.conversation_id=vulnerabilities.conversation_id ORDER BY bt.id LIMIT 1),'')`
|
|
|
|
// appendVulnerabilitySelection restricts records without joining/duplicating them.
|
|
func appendVulnerabilitySelection(query string, args []interface{}, selection string) (string, []interface{}) {
|
|
kind, value, _ := strings.Cut(selection, ":")
|
|
host := `COALESCE(json_extract(jt.value,'$.host'),'')`
|
|
switch kind {
|
|
case "host":
|
|
if value == "" {
|
|
query += ` AND NOT EXISTS(SELECT 1 FROM json_each(targets_json) jt WHERE json_extract(jt.value,'$.group_host')=1 AND ` + host + `<>'')`
|
|
} else {
|
|
query += ` AND EXISTS(SELECT 1 FROM json_each(targets_json) jt WHERE json_extract(jt.value,'$.group_host')=1 AND ` + host + `=?)`
|
|
args = append(args, value)
|
|
}
|
|
case "domain":
|
|
query += ` AND EXISTS(SELECT 1 FROM json_each(targets_json) jt WHERE json_extract(jt.value,'$.group_host')=1 AND (` + host + `=? OR ` + host + ` LIKE ? ESCAPE '\'))`
|
|
querySuffix := strings.TrimSuffix(escapeVulnerabilityLikePattern("."+value), "%")
|
|
args = append(args, value, querySuffix)
|
|
case "task":
|
|
query += " AND " + vulnerabilityTaskIDExpr + "=?"
|
|
args = append(args, value)
|
|
case "queue":
|
|
query += " AND " + vulnerabilityQueueIDExpr + "=?"
|
|
args = append(args, value)
|
|
case "tag":
|
|
query += " AND " + vulnerabilityTaskIDExpr + "='' AND TRIM(COALESCE(task_tag,''))=?"
|
|
args = append(args, value)
|
|
}
|
|
return query, args
|
|
}
|
|
|
|
func (db *DB) VulnerabilityNavigationForAccess(filter VulnerabilityListFilter, access RBACListAccess, view string) ([]*VulnerabilityNavigation, error) {
|
|
filter.BrowserSelection = ""
|
|
fields, join := `COALESCE(json_extract(jt.value,'$.host'),''), '', ''`, ` LEFT JOIN json_each(targets_json) jt ON json_extract(jt.value,'$.group_host')=1`
|
|
if view == "task" {
|
|
fields = vulnerabilityTaskIDExpr + ", " + vulnerabilityQueueIDExpr + `, COALESCE((SELECT bq.title FROM batch_tasks bt JOIN batch_task_queues bq ON bq.id=bt.queue_id WHERE bt.conversation_id=vulnerabilities.conversation_id ORDER BY bt.id LIMIT 1),'')`
|
|
join = ""
|
|
}
|
|
query := `SELECT DISTINCT vulnerabilities.id, severity, COALESCE(status,''), COALESCE(task_tag,''), ` + fields + ` FROM vulnerabilities` + join + ` WHERE 1=1`
|
|
query, args := filter.appendWhere(query, nil)
|
|
if view == "host" && strings.TrimSpace(filter.HostQuery) != "" {
|
|
query += ` AND json_extract(jt.value,'$.host') LIKE ? ESCAPE '\'`
|
|
args = append(args, escapeVulnerabilityLikePattern(strings.ToLower(strings.TrimSpace(filter.HostQuery))))
|
|
}
|
|
query, args = appendVulnerabilityAccessFilter(query, args, access)
|
|
rows, err := db.Query(query, args...)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
roots := map[string]*vulnerabilityNavigationCount{}
|
|
children := map[string]map[string]*vulnerabilityNavigationCount{}
|
|
makeCount := func(key, label string) *vulnerabilityNavigationCount {
|
|
return &vulnerabilityNavigationCount{node: &VulnerabilityNavigation{Key: key, Label: label, Severities: map[string]int{}}, ids: map[string]bool{}}
|
|
}
|
|
for rows.Next() {
|
|
var id, severity, status, tag, target, queue, queueTitle string
|
|
if err := rows.Scan(&id, &severity, &status, &tag, &target, &queue, &queueTitle); err != nil {
|
|
return nil, err
|
|
}
|
|
key, label := "host:"+target, target
|
|
rootKey, rootLabel := key, label
|
|
if view == "host" {
|
|
if _, err := netip.ParseAddr(target); err != nil {
|
|
if domain, err := publicsuffix.EffectiveTLDPlusOne(target); err == nil {
|
|
rootKey, rootLabel = "domain:"+domain, domain
|
|
}
|
|
}
|
|
} else {
|
|
key, label = "tag:"+strings.Trim(tag, " "), strings.Trim(tag, " ")
|
|
if target != "" {
|
|
key, label = "task:"+target, target
|
|
}
|
|
rootKey, rootLabel = key, label
|
|
if target != "" && queue != "" {
|
|
rootKey, rootLabel = "queue:"+queue, queueTitle
|
|
}
|
|
}
|
|
if roots[rootKey] == nil {
|
|
roots[rootKey] = makeCount(rootKey, rootLabel)
|
|
children[rootKey] = map[string]*vulnerabilityNavigationCount{}
|
|
}
|
|
roots[rootKey].add(id, severity, status)
|
|
if key != rootKey {
|
|
if children[rootKey][key] == nil {
|
|
children[rootKey][key] = makeCount(key, label)
|
|
}
|
|
children[rootKey][key].add(id, severity, status)
|
|
}
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
result := make([]*VulnerabilityNavigation, 0, len(roots))
|
|
less := func(a, b *VulnerabilityNavigation) bool {
|
|
if (a.Label == "") != (b.Label == "") {
|
|
return b.Label == ""
|
|
}
|
|
return a.Key < b.Key
|
|
}
|
|
for key, g := range roots {
|
|
for _, child := range children[key] {
|
|
g.node.Children = append(g.node.Children, child.node)
|
|
}
|
|
sort.Slice(g.node.Children, func(i, j int) bool { return less(g.node.Children[i], g.node.Children[j]) })
|
|
// Avoid a redundant parent when a domain has only its own host.
|
|
if len(g.node.Children) == 1 && g.node.Children[0].Label == g.node.Label {
|
|
g.node = g.node.Children[0]
|
|
}
|
|
result = append(result, g.node)
|
|
}
|
|
sort.Slice(result, func(i, j int) bool { return less(result[i], result[j]) })
|
|
return result, nil
|
|
}
|