99 "sync/atomic"
1010 "time"
1111
12- jsoniter "github.com/json-iterator/go"
1312 "github.com/miekg/dns"
1413 "github.com/pkg/errors"
1514 "github.com/projectdiscovery/gologger"
@@ -315,21 +314,49 @@ func (h *DNSServer) handleInteraction(domain string, w dns.ResponseWriter, r *dn
315314 }
316315 }
317316
317+ host , _ , _ := net .SplitHostPort (w .RemoteAddr ().String ())
318+
318319 if h .options .RootTLD && foundDomain != "" {
319- h .storeRootTLDInteraction (foundDomain , domain , requestMsg , responseMsg , w , r )
320+ interaction := & Interaction {
321+ Protocol : "dns" ,
322+ UniqueID : domain ,
323+ FullId : domain ,
324+ QType : toQType (r .Question [0 ].Qtype ),
325+ RawRequest : requestMsg ,
326+ RawResponse : responseMsg ,
327+ RemoteAddress : host ,
328+ Timestamp : time .Now (),
329+ }
330+ if h .options .OnResult != nil {
331+ h .options .OnResult (interaction )
332+ }
333+ h .options .storeRootTLDInteraction (interaction , foundDomain )
320334 }
321335
322336 if foundDomain == "" {
323337 return
324338 }
325339
340+ storeMatch := func (uniqueID , fullID string ) {
341+ h .options .storeInteraction (& Interaction {
342+ Protocol : "dns" ,
343+ UniqueID : uniqueID ,
344+ FullId : fullID ,
345+ QType : toQType (r .Question [0 ].Qtype ),
346+ RawRequest : requestMsg ,
347+ RawResponse : responseMsg ,
348+ RemoteAddress : host ,
349+ Timestamp : time .Now (),
350+ }, uniqueID [:h .options .CorrelationIdLength ])
351+ }
352+
326353 if h .options .ScanEverywhere {
327354 chunks := stringsutil .SplitAny (requestMsg , ".\n \t \" '" )
328355 for _ , chunk := range chunks {
329356 for part := range stringsutil .SlideWithLength (chunk , h .options .GetIdLength ()) {
330357 normalizedPart := strings .ToLower (part )
331358 if h .options .isCorrelationID (normalizedPart ) {
332- h . storeMatchedInteraction (normalizedPart , part , requestMsg , responseMsg , w , r )
359+ storeMatch (normalizedPart , part )
333360 }
334361 }
335362 }
@@ -347,63 +374,11 @@ func (h *DNSServer) handleInteraction(domain string, w dns.ResponseWriter, r *dn
347374 if i + 1 <= len (parts ) {
348375 fullID = strings .Join (parts [:i + 1 ], "." )
349376 }
350- h . storeMatchedInteraction (normalizedPartChunk , fullID , requestMsg , responseMsg , w , r )
377+ storeMatch (normalizedPartChunk , fullID )
351378 }
352379 }
353380}
354381
355- func (h * DNSServer ) storeRootTLDInteraction (rootDomain , fqdn , requestMsg , responseMsg string , w dns.ResponseWriter , r * dns.Msg ) {
356- host , _ , _ := net .SplitHostPort (w .RemoteAddr ().String ())
357- interaction := & Interaction {
358- Protocol : "dns" ,
359- UniqueID : fqdn ,
360- FullId : fqdn ,
361- QType : toQType (r .Question [0 ].Qtype ),
362- RawRequest : requestMsg ,
363- RawResponse : responseMsg ,
364- RemoteAddress : host ,
365- Timestamp : time .Now (),
366- }
367-
368- if h .options .OnResult != nil {
369- h .options .OnResult (interaction )
370- }
371-
372- data , err := jsoniter .Marshal (interaction )
373- if err != nil {
374- gologger .Warning ().Msgf ("Could not encode root tld dns interaction: %s\n " , err )
375- return
376- }
377- gologger .Debug ().Msgf ("Root TLD DNS Interaction: \n %s\n " , string (data ))
378- if err := h .options .Storage .AddInteractionWithId (rootDomain , data ); err != nil {
379- gologger .Warning ().Msgf ("Could not store dns interaction: %s\n " , err )
380- }
381- }
382-
383- func (h * DNSServer ) storeMatchedInteraction (uniqueID , fullID , requestMsg , responseMsg string , w dns.ResponseWriter , r * dns.Msg ) {
384- correlationID := uniqueID [:h .options .CorrelationIdLength ]
385- host , _ , _ := net .SplitHostPort (w .RemoteAddr ().String ())
386- interaction := & Interaction {
387- Protocol : "dns" ,
388- UniqueID : uniqueID ,
389- FullId : fullID ,
390- QType : toQType (r .Question [0 ].Qtype ),
391- RawRequest : requestMsg ,
392- RawResponse : responseMsg ,
393- RemoteAddress : host ,
394- Timestamp : time .Now (),
395- }
396- data , err := jsoniter .Marshal (interaction )
397- if err != nil {
398- gologger .Warning ().Msgf ("Could not encode dns interaction: %s\n " , err )
399- return
400- }
401- gologger .Debug ().Msgf ("DNS Interaction: \n %s\n " , string (data ))
402- if err := h .options .Storage .AddInteraction (correlationID , data ); err != nil {
403- gologger .Warning ().Msgf ("Could not store dns interaction: %s\n " , err )
404- }
405- }
406-
407382// CustomRecordConfig represents a custom DNS record configuration
408383type CustomRecordConfig struct {
409384 Type string `yaml:"type"`
0 commit comments