Skip to content

Commit 1eb1293

Browse files
author
Yunus AYDIN
committed
Add Web Cache Deception Query and Example Code Snippet
1 parent 8be6aed commit 1eb1293

531 files changed

Lines changed: 17010 additions & 0 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package main
2+
3+
import (
4+
"flag"
5+
"fmt"
6+
"html/template"
7+
"log"
8+
"net/http"
9+
"os/exec"
10+
"strings"
11+
"sync"
12+
)
13+
14+
var sessionMap = make(map[string]string)
15+
16+
var (
17+
templateCache = make(map[string]*template.Template)
18+
mutex = &sync.Mutex{}
19+
)
20+
21+
type Lists struct {
22+
Uid string
23+
UserName string
24+
UserLists []string
25+
ReadFile func(filename string) string
26+
}
27+
28+
func parseTemplateFile(templateName string, tmplFile string) (*template.Template, error) {
29+
mutex.Lock()
30+
defer mutex.Unlock()
31+
32+
// Check if the template is already cached
33+
if cachedTemplate, ok := templateCache[templateName]; ok {
34+
fmt.Println("cached")
35+
return cachedTemplate, nil
36+
}
37+
38+
// Parse and store the template in the cache
39+
parsedTemplate, _ := template.ParseFiles(tmplFile)
40+
fmt.Println("not cached")
41+
42+
templateCache[templateName] = parsedTemplate
43+
return parsedTemplate, nil
44+
}
45+
46+
func ShowAdminPageCache(w http.ResponseWriter, r *http.Request) {
47+
48+
if r.Method == "GET" {
49+
fmt.Println("cache called")
50+
sessionMap[r.RequestURI] = "admin"
51+
52+
// Check if a session value exists
53+
if _, ok := sessionMap[r.RequestURI]; ok {
54+
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" + "\");'"
55+
56+
// 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");'
57+
fmt.Println(cmd)
58+
59+
res, err := exec.Command("sh", "-c", cmd).Output()
60+
if err != nil {
61+
fmt.Println("err : ", err)
62+
}
63+
64+
splitedRes := strings.Split(string(res), "\n")
65+
66+
p := Lists{Uid: "1", UserName: "admin", UserLists: splitedRes}
67+
68+
parsedTemplate, _ := parseTemplateFile("page", "./views/admin/userlists.gtpl")
69+
w.Header().Set("Cache-Control", "no-store, no-cache")
70+
err = parsedTemplate.Execute(w, p)
71+
}
72+
} else {
73+
http.NotFound(w, nil)
74+
}
75+
76+
}
77+
78+
func main() {
79+
var portNum = flag.String("p", "80", "Specify application server listening port")
80+
flag.Parse()
81+
fmt.Println("Vulnapp server listening : " + *portNum)
82+
83+
http.Handle("/assets/", http.StripPrefix("/assets/", http.FileServer(http.Dir("assets/"))))
84+
http.HandleFunc("/adminusers", ShowAdminPageCache)
85+
86+
http.HandleFunc("/adminusers/", ShowAdminPageCache)
87+
err := http.ListenAndServe(":"+*portNum, nil)
88+
if err != nil {
89+
log.Fatal("ListenAndServe: ", err)
90+
}
91+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* @name Web Cache Deception
3+
* @description A caching system has been detected on the application and is vulnerable to web cache deception. By manipulating the URL it is possible to force the application to cache pages that are only accessible by an authenticated user. Once cached, these pages can be accessed by an unauthenticated user.
4+
* @kind path-problem
5+
* @problem.severity error
6+
* @security-severity 9
7+
* @precision high
8+
* @id go/web-cache-deception
9+
* @tags security
10+
* external/cwe/cwe-525
11+
*/
12+
13+
import go
14+
15+
from DataFlow::CallNode httpHandleFuncCall, DataFlow::CallNode call, DataFlow::Node predecessor, Expr predecessorExpr, CallExpr headerGetCall, Method get
16+
where
17+
httpHandleFuncCall.getTarget().hasQualifiedName("net/http", "HandleFunc") and
18+
httpHandleFuncCall.getNumArgument() > 1 and
19+
httpHandleFuncCall.getArgument(0).getType().toString() = "string" and
20+
httpHandleFuncCall.getArgument(0).toString().matches("%/\"") and
21+
// Trace the second argument's data flow to its predecessor
22+
predecessor = httpHandleFuncCall.getArgument(1).getAPredecessor() and
23+
// Find the corresponding expression for the predecessor
24+
get.hasQualifiedName("net/http", "Header", "Set") and
25+
call = get.getACall() and
26+
call.getArgument(0).toString().matches("\"Cache-Control\"")
27+
select httpHandleFuncCall.getArgument(0), call.getArgument(0)
28+
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"languages":{"go":{"displayName":"Go","files":["WebCacheDeception.go"],"linesOfCode":67,"name":"go"}}}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
sourceLocationPrefix: /Users/yunus.aydin/Research/codeql-fork/go/ql/src/Security/CVE-525
3+
baselineLinesOfCode: 67
4+
unicodeNewlines: false
5+
columnKind: utf8
6+
primaryLanguage: go
7+
creationMetadata:
8+
cliVersion: 2.15.1
9+
creationTime: 2023-11-13T20:28:51.105630Z
10+
finalised: true
2.05 KB
Binary file not shown.
Binary file not shown.
7.9 KB
Binary file not shown.
Binary file not shown.

go/ql/src/Security/CWE-525/wcd/db-go/default/cache/.lock

Whitespace-only changes.
Binary file not shown.

0 commit comments

Comments
 (0)