{"id":207717,"date":"2025-07-08T12:53:38","date_gmt":"2025-07-08T04:53:38","guid":{"rendered":"https:\/\/server.hk\/cnblog\/207717\/"},"modified":"2025-07-08T12:53:38","modified_gmt":"2025-07-08T04:53:38","slug":"go-%e4%b8%ad-for-%e5%be%aa%e7%8e%af%e5%86%85%e7%9a%84%e9%94%99%e8%af%af%e5%a4%84%e7%90%86%e5%8f%af%e8%83%bd%e4%bc%9a%e5%af%bc%e8%87%b4%e4%b8%8b%e4%b8%80%e6%ac%a1%e8%bf%ad%e4%bb%a3","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/207717\/","title":{"rendered":"Go \u4e2d for \u5faa\u73af\u5185\u7684\u9519\u8bef\u5904\u7406\u53ef\u80fd\u4f1a\u5bfc\u81f4\u4e0b\u4e00\u6b21\u8fed\u4ee3"},"content":{"rendered":"<p><b><\/b> <\/p>\n<p>\u5f53\u524d\u4f4d\u7f6e\uff1a <span>&gt;<\/span> <span>&gt;<\/span> <span>&gt;<\/span> <span>&gt;<\/span> <span>Go \u4e2d for \u5faa\u73af\u5185\u7684\u9519\u8bef\u5904\u7406\u53ef\u80fd\u4f1a\u5bfc\u81f4\u4e0b\u4e00\u6b21\u8fed\u4ee3<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-24 22:33:33<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u54c8\u55bd\uff01\u4eca\u5929\u5fc3\u8840\u6765\u6f6e\u7ed9\u5927\u5bb6\u5e26\u6765\u4e86<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aGo \u4e2d for \u5faa\u73af\u5185\u7684\u9519\u8bef\u5904\u7406\u53ef\u80fd\u4f1a\u5bfc\u81f4\u4e0b\u4e00\u6b21\u8fed\u4ee3\u300b<\/span>\uff0c\u60f3\u5fc5\u5927\u5bb6\u5e94\u8be5\u5bf9<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">Golang<\/span>\u90fd\u4e0d\u964c\u751f\u5427\uff0c\u90a3\u4e48\u9605\u8bfb\u672c\u6587\u5c31\u90fd\u4e0d\u4f1a\u5f88\u56f0\u96be\uff0c\u4ee5\u4e0b\u5185\u5bb9\u4e3b\u8981\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\uff0c\u82e5\u662f\u4f60\u6b63\u5728\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">Golang<\/span>\uff0c\u5343\u4e07\u522b\u9519\u8fc7\u8fd9\u7bc7\u6587\u7ae0~\u5e0c\u671b\u80fd\u5e2e\u52a9\u5230\u4f60\uff01<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u6211\u5f88\u96be\u4f7f\u7528\u7279\u5b9a\u7684 go \u5b9e\u73b0\u6765\u5c06\u65e5\u5fd7\u6587\u4ef6\u53d1\u9001\u5230\u4e0d\u540c\u7684\u4f4d\u7f6e\uff1a<\/p>\n<pre>package main\n\nfunc isDestinationSIEM(json_msg string, json_obj *jason.Object, siem_keys []string) (bool) {\n    if json_obj != nil {\n        dest, err := json_obj.GetString(\"destination\")\n        if err == nil {\n            if strings.Contains(dest,\"SIEM\") {\n                return true\n            }\n        }\n\n        for _, key :=  range siem_keys {\n            if strings.Contains(json_msg, key) {\n                return true\n            }\n        }\n    }\n    return false\n}\n\nfunc sendToSIEM(siem_dst string, json_msg string) (error) {\n    \/\/ Create connection to syslog server\n    roots := x509.NewCertPool()\n    ok := roots.AppendCertsFromPEM([]byte(rootPEM))\n    if !ok {\n        fmt.Println(\"failed to parse root certificate\")\n    }\n    config := &amp;tls.Config{RootCAs: roots, InsecureSkipVerify: true}\n    conn, err := tls.Dial(\"tcp\", siem_dst, config)\n    if err != nil {\n        fmt.Println(\"Error connecting SIEM\")\n        fmt.Println(err.Error())\n    } else {\n        \/\/ Send log message\n        _, err = fmt.Fprintf(conn, json_msg)\n        if err != nil {\n            fmt.Println(\"Error sending SIEM message: \", json_msg)\n            fmt.Println(err.Error())\n        }\n    }\n    defer conn.Close()\n\n    return err\n}\n\n\n\nfunc main() {\n\n    \/\/ simplified code otherwise there would have been too much\n    \/\/ but the 'devil' is this for loop\n    for _, obj := range objects {\n\n        \/\/ first check\n        isSIEM := isDestinationSIEM(obj, siem_keys)\n        if isSIEM {\n           err := sendToSIEM(obj)\n           if err != nil {\n             \/\/ print error\n           }\n\n        isAUDIT:= isDestinationSIEM(obj)\n        if isAUDIT {\n           err := sendToAUDIT(obj)\n           if err != nil {\n             \/\/ print error\n           }\n\n\n\n\n    } \/\/ end of for\n\n\n}<\/pre>\n<p>\u5f53\u201cif issiem\u201d\u8fd4\u56de\u9519\u8bef\u65f6\uff0c\u4e0d\u4f1a\u8fdb\u884c\u7b2c\u4e8c\u6b21\u68c0\u67e5\u201cif isaudit\u201d\u3002 \u4e3a\u4ec0\u4e48\u662f\u8fd9\u6837\uff1f\u5982\u679c\u8fd4\u56de\u9519\u8bef\uff0c\u5faa\u73af\u662f\u5426\u4ece\u4e0b\u4e00\u6b21\u8fed\u4ee3\u5f00\u59cb\uff1f<\/p>\n<p>\u9519\u8bef\u770b\u8d77\u6765\u50cf\u8fd9\u6837\uff1a \u8fd0\u884c\u65f6\u9519\u8bef\uff1a\u65e0\u6548\u7684\u5185\u5b58\u5730\u5740\u6216\u96f6\u6307\u9488\u53d6\u6d88\u5f15\u7528\uff1aerrorstring\uff08\u5217\u51fa\u4e86\u51e0\u4e2a go \u5305\uff09<\/p>\n<p> <\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p> <\/p>\n<p>\u9519\u8bef\u770b\u8d77\u6765\u50cf\u8fd9\u6837\uff1a\u8fd0\u884c\u65f6\u9519\u8bef\uff1a\u65e0\u6548\u7684\u5185\u5b58\u5730\u5740\u6216\u96f6\u6307\u9488\u53d6\u6d88\u5f15\u7528\uff1aerrorString\uff08\u5217\u51fa\u4e86\u51e0\u4e2a go \u5305\uff09<\/p>\n<p>\u8fd9\u610f\u5473\u7740\u60a8\u6355\u83b7\u4e86 <code>panic()<\/code> \u5e76\u4e14\u60a8\u7684\u7a0b\u5e8f\u5df2\u505c\u6b62\uff0c\u8fd9\u610f\u5473\u7740\u60a8\u7684\u5708\u5b50 <code>for<\/code> \u4e5f\u5df2\u505c\u6b62\u3002<\/p>\n<p>\u8fd9\u91cc\u8be6\u7ec6\u4ecb\u7ecd\u4e86\u5982\u4f55\u5904\u7406\u6050\u614c <\/p>\n<p>\u4eca\u5929\u5e26\u5927\u5bb6\u4e86\u89e3\u4e86\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u5e0c\u671b\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff1b\u5173\u4e8eGolang\u7684\u6280\u672f\u77e5\u8bc6\u6211\u4eec\u4f1a\u4e00\u70b9\u70b9\u6df1\u5165\u4ecb\u7ecd\uff0c\u6b22\u8fce\u5927\u5bb6\u5173\u6ce8\u516c\u4f17\u53f7\uff0c\u4e00\u8d77\u5b66\u4e60\u7f16\u7a0b~<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f53\u524d\u4f4d\u7f6e\uff1a &gt; &gt; &#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-207717","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207717","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=207717"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207717\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=207717"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=207717"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=207717"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}