|
| 1 | +package key |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "path/filepath" |
| 7 | + |
| 8 | + "gopkg.in/yaml.v3" |
| 9 | +) |
| 10 | + |
| 11 | +type Rule struct { |
| 12 | + Id string `yaml:"id"` |
| 13 | + Enabled bool `yaml:"enabled"` |
| 14 | + Pattern string `yaml:"pattern"` |
| 15 | +} |
| 16 | + |
| 17 | +type Rules struct { |
| 18 | + Rules []Rule `yaml:"rules"` |
| 19 | +} |
| 20 | + |
| 21 | +func init() { |
| 22 | + configDir := "config" |
| 23 | + configFile := filepath.Join(configDir, "rule.yaml") |
| 24 | + |
| 25 | + if _, err := os.Stat(configFile); os.IsNotExist(err) { |
| 26 | + if err := os.MkdirAll(configDir, 0755); err != nil { |
| 27 | + fmt.Printf("Error creating config directory: %v\n", err) |
| 28 | + return |
| 29 | + } |
| 30 | + CreateConfigFile() |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +func ReadRuleFile() (*Rules, error) { |
| 35 | + configFile := filepath.Join("config", "rule.yaml") |
| 36 | + file, err := os.ReadFile(configFile) |
| 37 | + if err != nil { |
| 38 | + return nil, fmt.Errorf("error reading rule file: %v", err) |
| 39 | + } |
| 40 | + |
| 41 | + var rules Rules |
| 42 | + if err := yaml.Unmarshal(file, &rules); err != nil { |
| 43 | + return nil, fmt.Errorf("error unmarshalling rule file: %v", err) |
| 44 | + } |
| 45 | + |
| 46 | + return &rules, nil |
| 47 | +} |
| 48 | + |
| 49 | +func CreateConfigFile() { |
| 50 | + configFile := filepath.Join("config", "rule.yaml") |
| 51 | + defaultRules := Rules{ |
| 52 | + Rules: []Rule{ |
| 53 | + {Id: "domain", Enabled: false, Pattern: ""}, |
| 54 | + {Id: "path", Enabled: false, Pattern: ""}, |
| 55 | + {Id: "domain_url", Enabled: false, Pattern: ""}, |
| 56 | + {Id: "ip", Enabled: false, Pattern: ""}, |
| 57 | + {Id: "ip_url", Enabled: false, Pattern: `\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}`}, |
| 58 | + {Id: "email", Enabled: true, Pattern: `\b[A-Za-z0-9._\-]+@[A-Za-z0-9.\-]+\.[A-Za-z]{2,61}\b`}, |
| 59 | + {Id: "id_card", Enabled: true, Pattern: `\b([1-9]\d{5}(19|20)\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx])\b`}, |
| 60 | + {Id: "phone", Enabled: true, Pattern: `\b1[3-9]\d{9}\b`}, |
| 61 | + {Id: "jwt_token", Enabled: true, Pattern: `eyJ[A-Za-z0-9_/+\-]{10,}={0,2}\.[A-Za-z0-9_/+\-\\]{15,}={0,2}\.[A-Za-z0-9_/+\-\\]{10,}={0,2}`}, |
| 62 | + {Id: "Aliyun_AK_ID", Enabled: true, Pattern: `\bLTAI[A-Za-z\d]{12,30}\b`}, |
| 63 | + {Id: "QCloud_AK_ID", Enabled: true, Pattern: `\bAKID[A-Za-z\d]{13,40}\b`}, |
| 64 | + {Id: "JDCloud_AK_ID", Enabled: true, Pattern: `\bJDC_[0-9A-Z]{25,40}\b`}, |
| 65 | + {Id: "AWS_AK_ID", Enabled: true, Pattern: `["''](?:A3T[A-Z0-9]|AKIA|AGPA|AIDA|AROA|AIPA|ANPA|ANVA|ASIA)[A-Z0-9]{16}["'']`}, |
| 66 | + {Id: "VolcanoEngine_AK_ID", Enabled: true, Pattern: `\b(?:AKLT|AKTP)[a-zA-Z0-9]{35,50}\b`}, |
| 67 | + {Id: "Kingsoft_AK_ID", Enabled: true, Pattern: `\bAKLT[a-zA-Z0-9-_]{16,28}\b`}, |
| 68 | + {Id: "GCP_AK_ID", Enabled: true, Pattern: `\bAIza[0-9A-Za-z_\-]{35}\b`}, |
| 69 | + {Id: "secret_key", Enabled: true, Pattern: ""}, |
| 70 | + {Id: "bearer_token", Enabled: true, Pattern: `\b[Bb]earer\s+[a-zA-Z0-9\-=._+/\\]{20,500}\b`}, |
| 71 | + {Id: "basic_token", Enabled: true, Pattern: `\b[Bb]asic\s+[A-Za-z0-9+/]{18,}={0,2}\b`}, |
| 72 | + {Id: "auth_token", Enabled: true, Pattern: `["''\[]*[Aa]uthorization["''\]]*\s*[:=]\s*[''"]?\b(?:[Tt]oken\s+)?[a-zA-Z0-9\-_+/]{20,500}[''"]?`}, |
| 73 | + {Id: "private_key", Enabled: true, Pattern: `-----\s*?BEGIN[ A-Z0-9_-]*?PRIVATE KEY\s*?-----[a-zA-Z0-9\/\n\r=+]*-----\s*?END[ A-Z0-9_-]*? PRIVATE KEY\s*?-----`}, |
| 74 | + {Id: "gitlab_v2_token", Enabled: true, Pattern: `\b(glpat-[a-zA-Z0-9\-=_]{20,22})\b`}, |
| 75 | + {Id: "github_token", Enabled: true, Pattern: `\b((?:ghp|gho|ghu|ghs|ghr|github_pat)_[a-zA-Z0-9_]{36,255})\b`}, |
| 76 | + {Id: "qcloud_api_gateway_appkey", Enabled: true, Pattern: `\bAPID[a-zA-Z0-9]{32,42}\b`}, |
| 77 | + {Id: "wechat_appid", Enabled: true, Pattern: `["''](wx[a-z0-9]{15,18})["'']`}, |
| 78 | + {Id: "wechat_corpid", Enabled: true, Pattern: `["''](ww[a-z0-9]{15,18})["'']`}, |
| 79 | + {Id: "wechat_id", Enabled: true, Pattern: `["''](gh_[a-z0-9]{11,13})["'']`}, |
| 80 | + {Id: "password", Enabled: true, Pattern: `(?i)(?:admin_?pass|password|[a-z]{3,15}_?password|user_?pass|user_?pwd|admin_?pwd)\\?['"]*\s*[:=]\s*\\?['"][a-z0-9!@#$%&*]{5,50}\\?['"]`}, |
| 81 | + {Id: "wechat_webhookurl", Enabled: true, Pattern: `\bhttps://qyapi.weixin.qq.com/cgi-bin/webhook/send\?key=[a-zA-Z0-9\-]{25,50}\b`}, |
| 82 | + {Id: "dingtalk_webhookurl", Enabled: true, Pattern: `\bhttps://oapi.dingtalk.com/robot/send\?access_token=[a-z0-9]{50,80}\b`}, |
| 83 | + {Id: "feishu_webhookurl", Enabled: true, Pattern: `\bhttps://open.feishu.cn/open-apis/bot/v2/hook/[a-z0-9\-]{25,50}\b`}, |
| 84 | + {Id: "slack_webhookurl", Enabled: true, Pattern: `\bhttps://hooks.slack.com/services/[a-zA-Z0-9\-_]{6,12}/[a-zA-Z0-9\-_]{6,12}/[a-zA-Z0-9\-_]{15,24}\b`}, |
| 85 | + {Id: "grafana_api_key", Enabled: true, Pattern: `\beyJrIjoi[a-zA-Z0-9\-_+/]{50,100}={0,2}\b`}, |
| 86 | + {Id: "grafana_cloud_api_token", Enabled: true, Pattern: `\bglc_[A-Za-z0-9\-_+/]{32,200}={0,2}\b`}, |
| 87 | + {Id: "grafana_service_account_token", Enabled: true, Pattern: `\bglsa_[A-Za-z0-9]{32}_[A-Fa-f0-9]{8}\b`}, |
| 88 | + {Id: "app_key", Enabled: true, Pattern: `\b(?:VUE|APP|REACT)_[A-Z_0-9]{1,15}_(?:KEY|PASS|PASSWORD|TOKEN|APIKEY)['"]*[:=]"(?:[A-Za-z0-9_\-]{15,50}|[a-z0-9/+]{50,100}==?)"`}, |
| 89 | + }, |
| 90 | + } |
| 91 | + |
| 92 | + data, err := yaml.Marshal(&defaultRules) |
| 93 | + if err != nil { |
| 94 | + fmt.Printf("Error marshalling default rules: %v\n", err) |
| 95 | + return |
| 96 | + } |
| 97 | + |
| 98 | + if err := os.WriteFile(configFile, data, 0755); err != nil { |
| 99 | + fmt.Printf("Error writing default rule file: %v\n", err) |
| 100 | + } |
| 101 | +} |
0 commit comments