|
| 1 | +package modules_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "net/http" |
| 6 | + "net/http/httptest" |
| 7 | + "testing" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/vmfunc/sif/internal/modules" |
| 11 | +) |
| 12 | + |
| 13 | +func runIloModule(t *testing.T, file string, status int, body string) *modules.Result { |
| 14 | + t.Helper() |
| 15 | + def, err := modules.ParseYAMLModule(file) |
| 16 | + if err != nil { |
| 17 | + t.Fatalf("parse %s: %v", file, err) |
| 18 | + } |
| 19 | + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 20 | + w.WriteHeader(status) |
| 21 | + _, _ = w.Write([]byte(body)) |
| 22 | + })) |
| 23 | + defer srv.Close() |
| 24 | + |
| 25 | + res, err := modules.ExecuteHTTPModule(context.Background(), srv.URL, def, modules.Options{ |
| 26 | + Timeout: 5 * time.Second, |
| 27 | + Threads: 2, |
| 28 | + }) |
| 29 | + if err != nil { |
| 30 | + t.Fatalf("execute %s: %v", file, err) |
| 31 | + } |
| 32 | + return res |
| 33 | +} |
| 34 | + |
| 35 | +func iloExtract(res *modules.Result, key string) string { |
| 36 | + for _, f := range res.Findings { |
| 37 | + if v := f.Extracted[key]; v != "" { |
| 38 | + return v |
| 39 | + } |
| 40 | + } |
| 41 | + return "" |
| 42 | +} |
| 43 | + |
| 44 | +func TestHPEIloXMLDataExposureModule(t *testing.T) { |
| 45 | + const ilo = "../../modules/recon/hpe-ilo-xmldata-exposure.yaml" |
| 46 | + |
| 47 | + t.Run("a real ilo xmldata response is flagged with serial, product and firmware", func(t *testing.T) { |
| 48 | + body := `<?xml version="1.0"?><RIMP><HSI><SBSN>CZC1234ABC </SBSN><SPN>ProLiant DL380 Gen10</SPN>` + |
| 49 | + `<UUID>31393736-3935-435A-4331-323334414243</UUID><SP>1</SP><cUUID>00000000-0000-0000-0000-000000000000</cUUID>` + |
| 50 | + `<VIRTUAL><STATE>Inactive</STATE><VID><BSN></BSN><cUUID></cUUID></VID></VIRTUAL></HSI>` + |
| 51 | + `<MP><ST>1</ST><PN>Integrated Lights-Out 5 (iLO 5)</PN><FWRI>2.44</FWRI><HWRI>ASIC: 17</HWRI>` + |
| 52 | + `<SN>ILO1234567890 </SN><UUID>ILO000000000000</UUID><IPM>1</IPM><SSO>0</SSO><PWRM>3.4</PWRM></MP></RIMP>` |
| 53 | + res := runIloModule(t, ilo, 200, body) |
| 54 | + if len(res.Findings) == 0 { |
| 55 | + t.Fatal("expected an ilo xmldata finding") |
| 56 | + } |
| 57 | + if v := iloExtract(res, "ilo_server_serial"); v != "CZC1234ABC" { |
| 58 | + t.Errorf("ilo_server_serial=%q, want CZC1234ABC", v) |
| 59 | + } |
| 60 | + if v := iloExtract(res, "ilo_server_product"); v != "ProLiant DL380 Gen10" { |
| 61 | + t.Errorf("ilo_server_product=%q, want ProLiant DL380 Gen10", v) |
| 62 | + } |
| 63 | + if v := iloExtract(res, "ilo_firmware"); v != "2.44" { |
| 64 | + t.Errorf("ilo_firmware=%q, want 2.44", v) |
| 65 | + } |
| 66 | + }) |
| 67 | + |
| 68 | + t.Run("an unrelated xml api with a generic RIMP-shaped body is not flagged", func(t *testing.T) { |
| 69 | + body := `<?xml version="1.0"?><RIMP><HSI><SBSN>FAKE</SBSN></HSI><MP><PN>Generic Management Card</PN></MP></RIMP>` |
| 70 | + if res := runIloModule(t, ilo, 200, body); len(res.Findings) > 0 { |
| 71 | + t.Errorf("a non-ilo RIMP-shaped body should not match, got %d findings", len(res.Findings)) |
| 72 | + } |
| 73 | + }) |
| 74 | + |
| 75 | + t.Run("a page that merely mentions Integrated Lights-Out in prose is not flagged", func(t *testing.T) { |
| 76 | + body := `<html><body>Our support team can help configure Integrated Lights-Out on your HPE server.</body></html>` |
| 77 | + if res := runIloModule(t, ilo, 200, body); len(res.Findings) > 0 { |
| 78 | + t.Errorf("a prose mention should not match, got %d findings", len(res.Findings)) |
| 79 | + } |
| 80 | + }) |
| 81 | + |
| 82 | + t.Run("a 404 is not a leak", func(t *testing.T) { |
| 83 | + if res := runIloModule(t, ilo, 404, "not found"); len(res.Findings) > 0 { |
| 84 | + t.Errorf("a 404 should not match, got %d findings", len(res.Findings)) |
| 85 | + } |
| 86 | + }) |
| 87 | +} |
0 commit comments