mirror of
https://github.com/activecm/rita
synced 2026-06-08 13:02:45 +00:00
b923c39e2d
* Update on config structure, functionality, and tests Co-Authored-By: Naomi Kramer <naomiagoddard@gmail.com> * Extend subnet type to read/write from db, update tests Co-Authored-By: Liza Tsibur <liza@activecountermeasures.com> * updated read file config test and subnet tests * fixed config and util tests, updated subnet related functions Co-Authored-By: Naomi Kramer <naomiagoddard@gmail.com> * Remove error return from GetDefaultConfig Co-Authored-By: Liza Tsibur <liza@activecountermeasures.com> * added json tags to database struct * Updated beacon weights validation for config * updates to score thresholds validation tags * changes to config subnet validation and testing * Update subnet.go * Write missing host entries to http to populate http_proto * Updating some fields to uint64 * WIP update some field types * Update zeek count types and fix tests * Add clickhouse credentials * Misc fixes * Update pointer * Add ability to mark datasets as sample datasets * fix column name * Fix datasets exiting import if hour is empty * Fix zeek count parsing from TSV files * Remove storing dns conns in arrays, Fix historical first seen dns lag * Remove unused columns * Update config.hjson * Update config.hjson * updated impact category score functions to use float64 Co-Authored-By: Naomi Kramer <naomiagoddard@gmail.com> * Update subnet.go * Store import version in imports table * Fix duplicated SNI/IP long connections * Update subnet_test.go * Cleanup output * Rolling files updates (#39) * Limit number of days to import for rolling datasets * Fix breaking imports when import was interrupted * Remove debug output --------- Co-authored-by: Naomi Kramer <naomi@activecountermeasures.com> * Omit parts of env from output * Set max for threat intel datasize * Remove SELinux neutering for QA * Add network size column * Fix http_proto for missing host, update tests for missing host fixes * Add online feeds to default config * Update sshprep (#45) * Update sshprep Co-Authored-By: William Stearns <3538265+william-stearns@users.noreply.github.com> * Update sshprep Add Bradley's suggestion of using head -1 to limit to a single address. --------- Co-authored-by: Naomi Kramer <naomi@activecountermeasures.com> Co-authored-by: William Stearns <3538265+william-stearns@users.noreply.github.com> * Installer Behavior Tweaks (#41) * Add --yes flag to add-apt-repository command * Add missing sudo flags, make sure we're using the SUDO variable instead * Add ability to perform zone transfers (#48) * Store zone transfer records Co-Authored-By: moth <25512187+0x6d6f7468@users.noreply.github.com> * Update config * Add tests * Tests, connectivity test * Update tests --------- Co-authored-by: moth <25512187+0x6d6f7468@users.noreply.github.com> * Support RedHat/RHEL as a valid target (#47) * Update sshprep Co-Authored-By: William Stearns <3538265+william-stearns@users.noreply.github.com> * Supporrt RedHat/RHEL as a valid target --------- Co-authored-by: Naomi Kramer <naomi@activecountermeasures.com> Co-authored-by: William Stearns <3538265+william-stearns@users.noreply.github.com> Co-authored-by: moth <moth@blackhillsinfosec.com> * Fix tests (#49) * Fix tests * Update WalkFiles to use UTC * fixed issue with rolling datasets over 24hours old not getting historical first seen timestamp set (#52) * Change values from float32 to float64 (#50) * Switch float32 to float64 * Update threat category calculation to match CalculateBucketedScore (#51) --------- Co-authored-by: Liza Tsibur <liza@activecountermeasures.com> * Bump max query execution time default value * Use string instead of error for ZoneTransferConnectivityErrors struct fields (#61) * Upgrade Golang to version 1.24 (#59) (#60) * Replace get_url with shell and curl (#58) * Update sshprep Co-Authored-By: William Stearns <3538265+william-stearns@users.noreply.github.com> * Replace get_url with shell and curl * Use get_url by default, fall back to curl if it fails --------- Co-authored-by: Naomi Kramer <naomi@activecountermeasures.com> Co-authored-by: William Stearns <3538265+william-stearns@users.noreply.github.com> Co-authored-by: moth <moth@blackhillsinfosec.com> * add automated log transfer, AC-Hunter issue 135 (#62) * Update sshprep Co-Authored-By: William Stearns <3538265+william-stearns@users.noreply.github.com> * add automated log transfer, PR135 * cron requires non-executable permission * Specify suggested YAML plugin and config in VSCode workspace * Linting and light cleanup * Update generate_installer.sh Download zeek_log_transport.sh to send to the sensor. * Create cron file if remote zeek installation * Only run zeek log import steps for remote sensor installations --------- Co-authored-by: Naomi Kramer <naomi@activecountermeasures.com> Co-authored-by: William Stearns <3538265+william-stearns@users.noreply.github.com> Co-authored-by: moth <moth@blackhillsinfosec.com> * Temporarily disable RITA/Zeek log transport until installer is modular (#66) * Uniform -y flag usage for repo management/package installation; Uniform SUDO variable usage (#68) * Resolve Installer Side Effects and Formalize RHEL Support (#73) * Add missing necessary wildcards for RHEL versions * Remove Ansible task replacing python3-requests to avoid RHEL distro installation side effects * Update supported distros in README * Update scoring defaults * Resolve Ansible Reboot Errors (#75) * Clean up conditionals; Fix reboot step for Ubuntu * Suppress erroneous error output on RPM systems, ignore errors on reboot necessity checks * Ignore missing host rows for openhttp (#76) * Fix integration tests due to prevalence (#77) --------- Co-authored-by: Liza Tsibur <liza@activecountermeasures.com> Co-authored-by: moth <moth@blackhillsinfosec.com> Co-authored-by: William Stearns <william.l.stearns@gmail.com> Co-authored-by: William Stearns <3538265+william-stearns@users.noreply.github.com> Co-authored-by: moth <25512187+0x6d6f7468@users.noreply.github.com>
207 lines
7.0 KiB
Go
207 lines
7.0 KiB
Go
package importer
|
|
|
|
import (
|
|
"errors"
|
|
"net"
|
|
"strings"
|
|
"sync/atomic"
|
|
"time"
|
|
|
|
"github.com/activecm/rita/v5/config"
|
|
"github.com/activecm/rita/v5/database"
|
|
"github.com/activecm/rita/v5/importer/zeektypes"
|
|
zlog "github.com/activecm/rita/v5/logger"
|
|
"github.com/activecm/rita/v5/util"
|
|
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
var errMissingQuery = "blank or missing query field in dns log entry, skipping entry"
|
|
|
|
type DNSEntry struct {
|
|
ImportTime time.Time `ch:"import_time"`
|
|
ZeekUID util.FixedString `ch:"zeek_uid"`
|
|
Hash util.FixedString `ch:"hash"`
|
|
Timestamp time.Time `ch:"ts"`
|
|
Src net.IP `ch:"src"`
|
|
Dst net.IP `ch:"dst"`
|
|
SrcNUID uuid.UUID `ch:"src_nuid"`
|
|
DstNUID uuid.UUID `ch:"dst_nuid"`
|
|
SrcPort uint16 `ch:"src_port"`
|
|
DstPort uint16 `ch:"dst_port"`
|
|
SrcLocal bool `ch:"src_local"`
|
|
DstLocal bool `ch:"dst_local"`
|
|
TransactionID uint16 `ch:"transaction_id"`
|
|
RoundTripTime float64 `ch:"round_trip_time"`
|
|
Query string `ch:"query"`
|
|
QueryClassCode uint16 `ch:"query_class_code"`
|
|
QueryClassName string `ch:"query_class_name"`
|
|
QueryTypeCode uint16 `ch:"query_type_code"`
|
|
QueryTypeName string `ch:"query_type_name"`
|
|
ResponseCode uint16 `ch:"response_code"`
|
|
ResponseCodeName string `ch:"response_code_name"`
|
|
AuthoritativeAnswer bool `ch:"authoritative_answer"`
|
|
RecursionDesired bool `ch:"recursion_desired"`
|
|
RecursionAvailable bool `ch:"recursion_available"`
|
|
Z uint16 `ch:"z"`
|
|
Answers []string `ch:"answers"`
|
|
TTLs []float64 `ch:"ttls"`
|
|
Rejected bool `ch:"rejected"`
|
|
// PDNS field
|
|
ResolvedIP net.IP `ch:"resolved_ip"`
|
|
}
|
|
|
|
type UniqueFQDN struct {
|
|
Hash util.FixedString `ch:"hash"`
|
|
Src net.IP `ch:"src"`
|
|
Dst net.IP `ch:"dst"`
|
|
FQDN string `ch:"fqdn"`
|
|
SrcNUID uuid.UUID `ch:"src_nuid"`
|
|
DstNUID uuid.UUID `ch:"dst_nuid"`
|
|
}
|
|
|
|
// parseDNS listens on a channel of raw dns log records, formats them into dns and pdns entries and and sends them to be written to the database
|
|
func parseDNS(cfg *config.Config, dns <-chan zeektypes.DNS, dnsOutput, pdnsOutput chan<- database.Data, numDNS, numPDNSRaw *uint64, importTime time.Time) {
|
|
logger := zlog.GetLogger()
|
|
|
|
// loop over raw dns channel
|
|
for d := range dns {
|
|
|
|
// parse raw record as a dns entry
|
|
entry, err := formatDNSRecord(cfg, &d, importTime)
|
|
if err != nil {
|
|
logger.Debug().Err(err).
|
|
Str("log_path", d.LogPath).
|
|
Str("zeek_uid", d.UID).
|
|
Str("timestamp", (time.Unix(int64(d.TimeStamp), 0)).String()).
|
|
Str("src", d.Source).
|
|
Str("dst", d.Destination).
|
|
Str("query", d.Query).
|
|
Send()
|
|
continue
|
|
}
|
|
|
|
// entry was subject to filtering
|
|
if entry == nil {
|
|
continue
|
|
}
|
|
|
|
dnsOutput <- entry // send to dns log writer
|
|
|
|
// addToUDNS(uDNSMap, entry) // add to unique dns map
|
|
atomic.AddUint64(numDNS, 1) // increment dns record counter
|
|
|
|
// parse dns entry into pdns entries based on dns entries's resolved ips
|
|
parsePDNSRecord(entry, pdnsOutput, numPDNSRaw)
|
|
|
|
}
|
|
}
|
|
|
|
// formatDNSRecord takes a raw dns record and formats it into the structure needed by the database
|
|
func formatDNSRecord(cfg *config.Config, parseDNS *zeektypes.DNS, importTime time.Time) (*DNSEntry, error) {
|
|
|
|
// get source destination pair
|
|
src := parseDNS.Source
|
|
dst := parseDNS.Destination
|
|
|
|
// parse addresses into binary format
|
|
srcIP := net.ParseIP(src)
|
|
dstIP := net.ParseIP(dst)
|
|
|
|
// verify that both addresses were able to be parsed successfully
|
|
if (srcIP == nil) || (dstIP == nil) {
|
|
return nil, errors.New(errParseSrcDst)
|
|
}
|
|
|
|
// verify that query field is set
|
|
if parseDNS.Query == "" {
|
|
return nil, errors.New(errMissingQuery)
|
|
}
|
|
|
|
// ignore domains that have no periods (com, org, uk)
|
|
if !strings.Contains(parseDNS.Query, ".") {
|
|
return nil, nil
|
|
}
|
|
|
|
// Run query through filter to filter out certain domains and
|
|
// filter out traffic which is external -> external or external -> internal (if specified in the config file)
|
|
ignore := (cfg.Filtering.FilterDomain(parseDNS.Query) || cfg.Filtering.FilterDNSPair(srcIP, dstIP))
|
|
|
|
// If domain is not subject to filtering, process
|
|
if ignore {
|
|
return nil, nil
|
|
}
|
|
|
|
srcNUID := util.ParseNetworkID(srcIP, parseDNS.AgentUUID)
|
|
dstNUID := util.ParseNetworkID(dstIP, parseDNS.AgentUUID)
|
|
|
|
zeekUID, err := util.NewFixedStringHash(parseDNS.UID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
hash, err := util.NewFixedStringHash(srcIP.To16().String(), dstIP.To16().String(), parseDNS.Query)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
entry := &DNSEntry{
|
|
ImportTime: importTime,
|
|
ZeekUID: zeekUID,
|
|
Hash: hash,
|
|
Timestamp: time.Unix(int64(parseDNS.TimeStamp), 0),
|
|
Src: srcIP,
|
|
Dst: dstIP,
|
|
SrcNUID: srcNUID,
|
|
DstNUID: dstNUID,
|
|
SrcPort: uint16(parseDNS.SourcePort),
|
|
DstPort: uint16(parseDNS.DestinationPort),
|
|
SrcLocal: cfg.Filtering.CheckIfInternal(srcIP),
|
|
DstLocal: cfg.Filtering.CheckIfInternal(dstIP),
|
|
TransactionID: uint16(parseDNS.TransID),
|
|
RoundTripTime: parseDNS.RTT,
|
|
Query: parseDNS.Query,
|
|
QueryClassCode: uint16(parseDNS.QClass),
|
|
QueryClassName: parseDNS.QClassName,
|
|
QueryTypeCode: uint16(parseDNS.QType),
|
|
QueryTypeName: parseDNS.QTypeName,
|
|
ResponseCode: uint16(parseDNS.RCode),
|
|
ResponseCodeName: parseDNS.RCodeName,
|
|
AuthoritativeAnswer: parseDNS.AA,
|
|
RecursionDesired: parseDNS.RD,
|
|
RecursionAvailable: parseDNS.RA,
|
|
Z: uint16(parseDNS.Z),
|
|
Answers: parseDNS.Answers,
|
|
TTLs: parseDNS.TTLs,
|
|
Rejected: parseDNS.Rejected,
|
|
}
|
|
|
|
return entry, nil
|
|
}
|
|
|
|
// parsePDNSRecord takes a single dns entry and splits it into multiple entries, one for each answer with a resolved ip in the dns record.
|
|
func parsePDNSRecord(dnsRecord *DNSEntry, writeChan chan<- database.Data, numDNS *uint64) {
|
|
|
|
uniqueResolvedMap := make(map[string]bool)
|
|
|
|
if dnsRecord.QueryTypeName == "A" {
|
|
// attempt to parse answers, copy the entry for every resolved IP
|
|
|
|
// storing resolved IPs as an IPv6 column instead of an Array(IPv6) column significantly improves the
|
|
// lookup time of resolved IPs
|
|
for _, answer := range dnsRecord.Answers {
|
|
answerIP := net.ParseIP(answer)
|
|
// Check if answer is an IP address and store it if it is
|
|
if answerIP != nil {
|
|
uniqueResolvedMap[answer] = true
|
|
// we must create a copy of this entry by dereferencing it before
|
|
// assigning the resolved IP and sending it to the writer
|
|
newEntry := *dnsRecord
|
|
newEntry.ResolvedIP = answerIP.To16()
|
|
writeChan <- &newEntry
|
|
atomic.AddUint64(numDNS, 1) // increment pdns counter
|
|
}
|
|
}
|
|
}
|
|
}
|