github.com/chainreactors/fingers@v1.2.1/xray/dsl_test.go (about)

     1  package xray
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/chainreactors/fingers/resources"
     7  	"github.com/chainreactors/neutron/protocols"
     8  	"github.com/chainreactors/utils/httputils"
     9  )
    10  
    11  func TestNginxDSLMatch(t *testing.T) {
    12  	engine, err := NewXrayEngine(resources.XrayWebData)
    13  	if err != nil {
    14  		t.Fatalf("load: %v", err)
    15  	}
    16  
    17  	// Simulate a typical nginx response
    18  	raw := []byte("HTTP/1.1 200 OK\r\nServer: nginx/1.16.1\r\nContent-Type: text/html\r\n\r\n<html><head><title>Welcome to CentOS</title></head><body>test</body></html>")
    19  
    20  	resp := httputils.NewResponseWithRaw(raw)
    21  	if resp == nil {
    22  		t.Fatal("failed to parse raw response")
    23  	}
    24  
    25  	// Check what buildEvent produces
    26  	event := engine.buildEvent(resp, "<html><head><title>Welcome to CentOS</title></head><body>test</body></html>", len(raw))
    27  
    28  	t.Logf("event keys:")
    29  	for k, v := range event {
    30  		t.Logf("  %s = %v", k, v)
    31  	}
    32  
    33  	// Check if server is in event
    34  	if sv, ok := event["server"]; ok {
    35  		t.Logf("server value: %q", sv)
    36  	} else {
    37  		t.Error("server key missing from event!")
    38  	}
    39  
    40  	// Now test full WebMatch
    41  	frames := engine.WebMatch(raw)
    42  	t.Logf("matched frames: %d", len(frames))
    43  	for _, f := range frames {
    44  		t.Logf("  %s", f.Name)
    45  	}
    46  
    47  	// Check specifically for nginx template
    48  	found := false
    49  	for _, tmpl := range engine.templates {
    50  		if tmpl.Info.Name == "Nginx" || tmpl.Id == "f5-nginx" {
    51  			t.Logf("found nginx template: id=%s name=%s", tmpl.Id, tmpl.Info.Name)
    52  			for _, req := range tmpl.GetRequests() {
    53  				if req.CompiledOperators == nil {
    54  					t.Log("  no compiled operators!")
    55  					continue
    56  				}
    57  				for mi, m := range req.CompiledOperators.Matchers {
    58  					t.Logf("  matcher[%d]: type=%s part=%s words=%v dsl=%v", mi, m.Type, m.Part, m.Words, m.DSL)
    59  					ok, matched := req.Match(event, m)
    60  					t.Logf("    match result: %v %v", ok, matched)
    61  				}
    62  			}
    63  			found = true
    64  		}
    65  	}
    66  	if !found {
    67  		t.Skip("nginx template not found in embedded data, skipping detailed matcher check")
    68  	}
    69  
    70  	// Also manually test the DSL expression
    71  	t.Log("\n--- Manual DSL test ---")
    72  	testData := protocols.InternalEvent{
    73  		"server": "nginx/1.16.1",
    74  		"body":   "test",
    75  	}
    76  	t.Logf("manual server value: %q", testData["server"])
    77  }