|
| 1 | +package bad |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "html/template" |
| 6 | + "log" |
| 7 | + "net/http" |
| 8 | + "os/exec" |
| 9 | + "strings" |
| 10 | + "sync" |
| 11 | +) |
| 12 | + |
| 13 | +var sessionMap = make(map[string]string) |
| 14 | + |
| 15 | +var ( |
| 16 | + templateCache = make(map[string]*template.Template) |
| 17 | + mutex = &sync.Mutex{} |
| 18 | +) |
| 19 | + |
| 20 | +type Lists struct { |
| 21 | + Uid string |
| 22 | + UserName string |
| 23 | + UserLists []string |
| 24 | + ReadFile func(filename string) string |
| 25 | +} |
| 26 | + |
| 27 | +func parseTemplateFile(templateName string, tmplFile string) (*template.Template, error) { |
| 28 | + mutex.Lock() |
| 29 | + defer mutex.Unlock() |
| 30 | + |
| 31 | + // Check if the template is already cached |
| 32 | + if cachedTemplate, ok := templateCache[templateName]; ok { |
| 33 | + fmt.Println("cached") |
| 34 | + return cachedTemplate, nil |
| 35 | + } |
| 36 | + |
| 37 | + // Parse and store the template in the cache |
| 38 | + parsedTemplate, _ := template.ParseFiles(tmplFile) |
| 39 | + fmt.Println("not cached") |
| 40 | + |
| 41 | + templateCache[templateName] = parsedTemplate |
| 42 | + return parsedTemplate, nil |
| 43 | +} |
| 44 | + |
| 45 | +func ShowAdminPageCache(w http.ResponseWriter, r *http.Request) { |
| 46 | + |
| 47 | + if r.Method == "GET" { |
| 48 | + fmt.Println("cache called") |
| 49 | + sessionMap[r.RequestURI] = "admin" |
| 50 | + |
| 51 | + // Check if a session value exists |
| 52 | + if _, ok := sessionMap[r.RequestURI]; ok { |
| 53 | + cmd := "mysql -h mysql -u root -prootwolf -e 'select id,name,mail,age,created_at,updated_at from vulnapp.user where name not in (\"" + "admin" + "\");'" |
| 54 | + |
| 55 | + // mysql -h mysql -u root -prootwolf -e 'select id,name,mail,age,created_at,updated_at from vulnapp.user where name not in ("test");--';echo");' |
| 56 | + fmt.Println(cmd) |
| 57 | + |
| 58 | + res, err := exec.Command("sh", "-c", cmd).Output() |
| 59 | + if err != nil { |
| 60 | + fmt.Println("err : ", err) |
| 61 | + } |
| 62 | + |
| 63 | + splitedRes := strings.Split(string(res), "\n") |
| 64 | + |
| 65 | + p := Lists{Uid: "1", UserName: "admin", UserLists: splitedRes} |
| 66 | + |
| 67 | + parsedTemplate, _ := parseTemplateFile("page", "./views/admin/userlists.gtpl") |
| 68 | + w.Header().Set("Cache-Control", "no-store, no-cache") |
| 69 | + err = parsedTemplate.Execute(w, p) |
| 70 | + } |
| 71 | + } else { |
| 72 | + http.NotFound(w, nil) |
| 73 | + } |
| 74 | + |
| 75 | +} |
| 76 | + |
| 77 | +func main() { |
| 78 | + fmt.Println("Vulnapp server listening : 1337") |
| 79 | + |
| 80 | + http.Handle("/assets/", http.StripPrefix("/assets/", http.FileServer(http.Dir("assets/")))) |
| 81 | + |
| 82 | + http.HandleFunc("/adminusers/", ShowAdminPageCache) |
| 83 | + err := http.ListenAndServe(":1337", nil) |
| 84 | + if err != nil { |
| 85 | + log.Fatal("ListenAndServe: ", err) |
| 86 | + } |
| 87 | +} |
0 commit comments