|
| 1 | +package evil |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "crypto/md5" |
| 6 | + "crypto/sha1" |
| 7 | + "encoding/base64" |
| 8 | + "encoding/hex" |
| 9 | + "log" |
| 10 | + "net/http" |
| 11 | + "net/http/httputil" |
| 12 | + "net/url" |
| 13 | + "regexp" |
| 14 | + "strconv" |
| 15 | + "sync" |
| 16 | + "time" |
| 17 | + |
| 18 | + "github.com/dengsgo/math-engine/engine" |
| 19 | +) |
| 20 | + |
| 21 | +func ServeEvilServer(addr string, hard bool) error { |
| 22 | + return http.ListenAndServe(addr, NewEvilServeMux(hard)) |
| 23 | +} |
| 24 | + |
| 25 | +func NewEvilServeMux(hard bool) *http.ServeMux { |
| 26 | + s := http.NewServeMux() |
| 27 | + mathRe := regexp.MustCompile(`\d+\s*[-+*/]\s*\d+`) |
| 28 | + sleepRe := regexp.MustCompile(`(?i)sleep\((\d+)\)`) |
| 29 | + waitForRe := regexp.MustCompile(`(?i)waitfor\s+delay\s+'0:0:(\d+)'`) |
| 30 | + s.HandleFunc("/", func(writer http.ResponseWriter, request *http.Request) { |
| 31 | + buf := bufPool.Get().(*bytes.Buffer) |
| 32 | + defer func() { |
| 33 | + buf.Reset() |
| 34 | + bufPool.Put(buf) |
| 35 | + }() |
| 36 | + |
| 37 | + buf.Write(CommonEvilResponse) |
| 38 | + |
| 39 | + data, err := httputil.DumpRequest(request, true) |
| 40 | + if err != nil { |
| 41 | + log.Println(err) |
| 42 | + } |
| 43 | + buf.Write(data) |
| 44 | + |
| 45 | + if hard { |
| 46 | + Split(data, SepFunc, func(bytes []byte) bool { |
| 47 | + GenEvilContent(buf, bytes) |
| 48 | + return true |
| 49 | + }) |
| 50 | + } |
| 51 | + |
| 52 | + unescape, _ := url.PathUnescape(string(data)) |
| 53 | + unescape, _ = url.QueryUnescape(unescape) |
| 54 | + if hard { |
| 55 | + Split([]byte(unescape), SepFunc, func(bytes []byte) bool { |
| 56 | + GenEvilContent(buf, bytes) |
| 57 | + return true |
| 58 | + }) |
| 59 | + } |
| 60 | + |
| 61 | + // 处理 sleep 和 WAITFOR DELAY |
| 62 | + sleepMatches := sleepRe.FindAllStringSubmatch(unescape, -1) |
| 63 | + for _, match := range sleepMatches { |
| 64 | + if len(match) > 1 { |
| 65 | + sleepTime, _ := strconv.Atoi(match[1]) |
| 66 | + if sleepTime > 50 { |
| 67 | + time.Sleep(time.Millisecond * time.Duration(sleepTime)) |
| 68 | + } else { |
| 69 | + time.Sleep(time.Second * time.Duration(sleepTime)) |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + waitForMatches := waitForRe.FindAllStringSubmatch(unescape, -1) |
| 75 | + for _, match := range waitForMatches { |
| 76 | + if len(match) > 1 { |
| 77 | + waitTime, _ := strconv.Atoi(match[1]) |
| 78 | + if waitTime > 50 { |
| 79 | + time.Sleep(time.Millisecond * time.Duration(waitTime)) |
| 80 | + } else { |
| 81 | + time.Sleep(time.Second * time.Duration(waitTime)) |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + for _, expr := range mathRe.FindAllString(unescape, -1) { |
| 87 | + r, err := engine.ParseAndExec(expr) |
| 88 | + if err != nil { |
| 89 | + log.Println(err) |
| 90 | + continue |
| 91 | + } |
| 92 | + GenEvilContent(buf, []byte(strconv.Itoa(int(r)))) |
| 93 | + } |
| 94 | + |
| 95 | + _, _ = writer.Write(buf.Bytes()) |
| 96 | + }) |
| 97 | + return s |
| 98 | +} |
| 99 | + |
| 100 | +var bufPool = sync.Pool{New: func() any { |
| 101 | + return bytes.NewBuffer(nil) |
| 102 | +}} |
| 103 | + |
| 104 | +func GenEvilContent(dst *bytes.Buffer, data []byte) { |
| 105 | + dst.Write(data) |
| 106 | + hashMD5 := md5.Sum(data) |
| 107 | + dst.Write(hashMD5[:]) |
| 108 | + dst.WriteString(" ") |
| 109 | + dst.WriteString(hex.EncodeToString(hashMD5[:])) |
| 110 | + dst.WriteString(" ") |
| 111 | + dst.WriteString(base64.StdEncoding.EncodeToString([]byte(hex.EncodeToString(hashMD5[:])))) |
| 112 | + dst.WriteString(" ") |
| 113 | + hashSha1 := sha1.Sum(data) |
| 114 | + dst.Write(hashSha1[:]) |
| 115 | + dst.WriteString(hex.EncodeToString(hashSha1[:])) |
| 116 | + dst.WriteString(" ") |
| 117 | + dst.WriteString(base64.StdEncoding.EncodeToString([]byte(hex.EncodeToString(hashSha1[:])))) |
| 118 | + dst.WriteString(" ") |
| 119 | + dst.WriteString(base64.StdEncoding.EncodeToString(data)) |
| 120 | + dst.WriteString(" ") |
| 121 | +} |
| 122 | + |
| 123 | +// CommonEvilResponse |
| 124 | +// 常见md5/sha1/base64 (1-1000的数字) |
| 125 | +// 常见登录表单 |
| 126 | +// 常见错误信息 |
| 127 | +var CommonEvilResponse = []byte(`<html> |
| 128 | + <head> |
| 129 | + <title>Level1</title> |
| 130 | + <title>OA系统</title> |
| 131 | + </head> |
| 132 | + |
| 133 | + <center><h1>Level1</h1></center> |
| 134 | + |
| 135 | + <header> |
| 136 | + <h1>OA系统</h1> |
| 137 | + <nav> |
| 138 | + <ul> |
| 139 | + <li><a href="#">首页</a></li> |
| 140 | + <li><a href="#">通知公告</a></li> |
| 141 | + <li><a href="#">个人中心</a></li> |
| 142 | + <li><a href="#">退出登录</a></li> |
| 143 | + </ul> |
| 144 | + </nav> |
| 145 | + </header> |
| 146 | + |
| 147 | + <main> |
| 148 | + <section class="banner"> |
| 149 | + <h2>欢迎使用OA系统</h2> |
| 150 | + <p>这是一款集办公、管理、协同于一体的企业级应用软件。</p> |
| 151 | + </section> |
| 152 | + |
| 153 | + <form action="/login" method="POST"> |
| 154 | + <div> |
| 155 | + <label for="username">用户名:</label> |
| 156 | + <input type="text" id="username" name="username" required> |
| 157 | + </div> |
| 158 | + <div> |
| 159 | + <label for="password">密码:</label> |
| 160 | + <input type="password" id="password" name="password" required> |
| 161 | + </div> |
| 162 | + <button type="submit">登录</button> |
| 163 | + </form> |
| 164 | + |
| 165 | + <section class="news"> |
| 166 | + <h2>新闻动态</h2> |
| 167 | + <ul> |
| 168 | + <li><a href="#">公司年会隆重举行</a></li> |
| 169 | + <li><a href="#">财务部门完成年度结算</a></li> |
| 170 | + <li><a href="#">研发部门推出新产品</a></li> |
| 171 | + </ul> |
| 172 | + </section> |
| 173 | + |
| 174 | + <section class="notice"> |
| 175 | + <h2>通知公告</h2> |
| 176 | + <ul> |
| 177 | + <li><a href="#">关于2022年春节放假的通知</a></li> |
| 178 | + <li><a href="#">办公室搬迁通知</a></li> |
| 179 | + </ul> |
| 180 | + </section> |
| 181 | + </main> |
| 182 | + |
| 183 | + <footer> |
| 184 | + <p>© 2023 OA系统 版权所有</p> |
| 185 | + </footer> |
| 186 | + |
| 187 | + <h1>Oops! Something went wrong.</h1> |
| 188 | + <p>We're sorry, but an error has occurred while processing your request. Please try again later.</p> |
| 189 | + <p>Error Code: 400</p> |
| 190 | + <p>Error Code: 401</p> |
| 191 | + <p>Error Code: 403</p> |
| 192 | + <p>Error Code: 404</p> |
| 193 | + <p>Error Code: 500</p> |
| 194 | + <p>Error Code: 501</p> |
| 195 | + <p>Error Code: 502</p> |
| 196 | + <p>Error Code: 503</p> |
| 197 | + { |
| 198 | + "error": { |
| 199 | + "code": 404, |
| 200 | + "message": "未找到请求的资源", |
| 201 | + "details": "请检查您的请求URL是否正确,并确保所请求的资源存在。" |
| 202 | + } |
| 203 | + } |
| 204 | + </body> |
| 205 | +</html> |
| 206 | +<!-- a padding to disable MSIE and Chrome friendly error page --> |
| 207 | +<!-- a padding to disable MSIE and Chrome friendly error page --> |
| 208 | +<!-- a padding to disable MSIE and Chrome friendly error page --> |
| 209 | +<!-- a padding to disable MSIE and Chrome friendly error page --> |
| 210 | +<!-- a padding to disable MSIE and Chrome friendly error page --> |
| 211 | +<!-- a padding to disable MSIE and Chrome friendly error page --> |
| 212 | +<?xml version="1.0" encoding="UTF-8"?> |
| 213 | +<?xml version="1.1" encoding="UTF-8"?>`) |
| 214 | + |
| 215 | +func init() { |
| 216 | + buf := bytes.NewBuffer(nil) |
| 217 | + buf.Write(CommonEvilResponse) |
| 218 | + for i := 0; i < 1000; i++ { |
| 219 | + GenEvilContent(buf, []byte(strconv.Itoa(i))) |
| 220 | + } |
| 221 | + buf.WriteString("\nroot:x:0:0:root:/root:/bin/bash\n") |
| 222 | + CommonEvilResponse = buf.Bytes() |
| 223 | +} |
0 commit comments