ctfs
  • 👋Hello!
  • 🏴Practice
    • 🌐Cryptohack
      • Introduction
      • General
        • Encoding
        • XOR
        • Mathematics
        • Data Formats
      • Symmetric Ciphers
        • How AES Works
        • Symmetric Starter
        • Block Ciphers 1
        • Stream Ciphers
      • Mathematics
        • Modular Math
        • Lattices
      • RSA
        • Starter
        • Primes Part 1
        • Public Exponent
    • 🌐PortSwigger
      • Path Traversal
      • File Upload
      • SSRF Attacks
    • 🌐TryHackMe
      • Basic Skills
      • Linux
      • Penetration Testing
      • Networking
      • OSINT
  • 🚩Competitions
    • 2025
      • 🇮🇩GKSK#9 Osintathon
        • Mudik Lebaran (100 pts)
        • Foto Patung (100 pts)
        • Kolektor Komik (100 pts)
        • Tolong Aku (100 pts)
        • Kencan Pertama (100 pts)
        • Nama Si Pelaku (100 pts)
        • Cekidot (100 pts)
        • Ledakan! (100 pts)
        • 🎹🎶 (100 pts)
        • Batu Besar (100 pts)
        • Komentar (100 pts)
        • Ini dimana? (100 pts)
        • Koordinat Foto Misterius (100 pts)
        • Bianglalaaa (100 pts)
        • Aku Hacker (100 pts)
        • Anjazzz (100 pts)
        • Dikirim Kakakku (129 pts)
        • Ingfo Loker (154 pts)
        • MISSING 00 (100 pts)
        • MISSING 01 (154 pts)
        • Siapa Aku? (154 pts)
      • 🇮🇩IFEST 13
        • Ququerer (250 pts)
        • Silent Trace (370 pts)
        • Nugas (Solved After Event)
        • Free Flag (280 pts)
        • Brute (Solved After Event)
        • Web V1 (Solved After Event)
        • Bypass (Solved After Event)
        • Orbiter (Solved After Event)
      • 🌐OSINT Combine (Wildlife)
        • Getting Started (100 pts)
        • Proper Poppy (100 pts)
        • Legendary Beasts (200 pts)
        • Shadow Fleet (200 pts)
        • Proper Poppy II (200 pts)
        • Not So Smug Smuggler (200 pts)
        • Icy (200 pts)
        • Forest Pals (200 pts)
        • Safari Time II (200 pts)
        • Sneaky! (200 pts)
        • Hello Friend (300 pts)
        • Busy As A (300 pts)
        • Get Rotated! (300 pts)
        • High Seas (300 pts)
        • Nocturnal (300 pts)
        • Safari Time (400 pts)
        • Peak Weather (400 pts)
        • Singsong (400 pts)
        • Falling Fell (500 pts)
        • Kitty Cats (500 pts)
      • 🇮🇩RECURSION
        • let him cook
        • Basic Math
        • Favourite Number
        • Zarrar Cipher (100 pts)
        • paBlue Team (100 pts)
        • [🩸] I wish I was there on December 21, 2024 (100 pts)
        • Small House (200 pts)
        • [🩸] Mission Difference (456 pts)
    • 2024
      • 🌐Santa Claus CTF
        • Complete Picture
        • Day 1 - Big Bang
        • Day 2 - The Summer Job
        • Day 3 - The Visitors
        • Day 4 - Happy Birthday
        • Day 5 - Say My Name
        • Day 6 - Say "Cheese"
        • Day 7 - Revealing Pixels
        • Day 8 - Connecting The Dots
        • Day 9 - 404 Not Found
        • Day 10 - Breaking News
        • Day 11 - Ayrton Santa
        • Day 12 - Lost and Found
        • Day 13 - Planespotting
        • Day 14 - Santa Surveillance
        • Day 15 - Shaken, Not Stirred
        • Day 16 - Status Update
        • Day 17 - Waste ...of Time
        • Day 18 - Lost in Translation
        • Day 19 - Santa's Clones
        • Day 20 - Losing Tracks
        • Day 21 - Sing my Song
        • Day 22 - Eagle Eye
        • Day 23 - Distances Matters
        • Day 24 - Mastermind
      • 🌐Cyber Jawara International
        • Stone Game (100 pts)
        • prepare the tools (176 pts)
        • Persona (484 pts)
      • 🌐OSMOSIS Precon CTF
        • 1 The art of espionage
        • # 2 The Hack
        • # 3 The rabbit hole
        • # 4 The Association
        • # 6 Where is number 5
        • # 5 Who is it
        • Too many Layers
        • The prize
      • 🇮🇩Intechfest
        • Sanity Check (100 pts)
        • Alin (113 pts)
        • GerakSendiri (106 pts)
        • Details (100 pts)
      • 🇮🇩COMPFEST 16
        • Let's Help John! (100 pts)
        • money gone, wallet also gone (100 pts)
        • head’s up! (493 pts)
        • CaRd (304 pts)
        • Sanity Check (100 pts)
      • 🇮🇩Gemastik
        • Baby AES (451 pts)
        • Baby Structured (100 pts)
      • 🇮🇩Technofair 11
        • Kenangan
        • Xorban
        • Marsha
        • Siap Tempur!!
        • eftipi
        • kurang berarti
        • DUMPling
        • Malicious
      • 🌐DIVER OSINT
        • chiban
      • 🇮🇩GKSK#8 Osintathon
        • Sport Location
        • Meklaren lu warna apa boss ?
        • Postcode
        • Rumah Minang
        • Latihan
        • Anak Misterius
        • Travelling Anywhere
        • The Thief
        • Danger Watch
        • Misteri Ruang Angkasa
        • Fun Walk
        • I am Late
        • My Oshi
        • Wellcome to my Youtube Channel
        • Pesan Tersembunyi Wingdings
        • Salah Fokus
        • Apa itu GKSK?
        • Foto Bersejarah
        • Picture
        • Nostalgia Child
        • oldschool
        • Summer Olympic
      • 🇮🇩Techcomfest
        • pemanasan
        • crackable
        • Kuli-ah forensik
    • 2023
      • 🇮🇩Cyber Jawara
        • daruma
      • 🇮🇩NCW
        • Simple (220 pts)
        • wangsaf (320 pts)
        • Sillyville Saga (220 pts)
        • Freminhelp (Solved after event)
      • 🇮🇩Hology 6
      • 🇮🇩SlashRoot 7
        • Summary (441 pts)
        • eeee (480 pts)
        • Zebra Cross (409 pts)
        • Waka Waka eh eh (185 pts)
        • ANABUL (250 pts)
      • 🇮🇩COMPFEST 15
        • not simply corrupted (316 pts)
        • Artificial secret (356 pts)
      • 🇮🇩Gemastik
        • easy AES
        • k-1
        • Gen Z
      • 🇮🇩TechnoFair 10
        • RSA Bwang
        • Marsah
        • rapsodi
        • Pengen Merch JKT 😢
        • space mono
        • file pemberian fans
        • bantu aku mencari sebuah rahasia
    • 2022
      • 🇮🇩NCW
        • sabeb64 (331 pts)
        • cakemath (451 pts)
        • Downloader (244 pts)
        • 199 passcode (Solved after event)
      • 🇮🇩TEDCTF
      • 🇮🇩Gemastik
      • 🇮🇩OSCCTF
      • 🇮🇩ARA
  • 🪦Old Hello
Powered by GitBook
On this page
  • Description
  • Validations
  • Bypassing server configurations
  • Obfuscating file extensions
  • Other types of flawed validation:
  • File upload race conditions
  • other exploit than RCE
  • using PUT requests
  • Lab solutions
  1. Practice
  2. PortSwigger

File Upload

Description

  • uploading some kind of script (in a file) into the server

  • triggering the script by requesting the file

  • for example, entering example.com/file.php would run the php file

  • we can put malicious code in the php file

  • most dangerous outcome from this attack is running a webshell (RCE), basically giving the hacker full-control of the target

Times where File Upload could be malicious:

  • uploading code then running it -> RCE

  • uploading large sized files -> DoS attack

  • uploading files very frequently -> rate-limit attack

In the wild, websites implement some kind of validations. The vulns are in the flaws of those validations. Interestingly, the validations might be applied inconsitently throughout a network and even between directories.

Validations

Usually, what are validated by the server?

  • file name

  • file extension (by name, by http header, by file header)

  • file size

  • file hash

  • file path (?)(to prevent directory traversal)

What a server would do to files? non-exe -> send the file contents exe & run -> get input from the request, then send output exe & don't run -> give error & maybe send the code as plaintext

Bypassing server configurations

By default, servers won't execute files unless configured to do so. Devs have to add this to the /etc/apache2/apache2.conf file:

LoadModule php_module /usr/lib/apache2/modules/libphp.so
    AddType application/x-httpd-php .php

There's also special configurations for directories. In apache, it uses the .htaccess file. The "language" used in .htaccess is the same with apache2.conf. Meanwhile, IIS servers use web.config file. This one allows json:

<staticContent>
    <mimeMap fileExtension=".json" mimeType="application/json" />
    </staticContent>

Usually, you can't access config files via http requests (forbidden error), but some servers don't prevent you to upload one.

Obfuscating file extensions

Original file name: exploit.php

  • validation code do case sensitive, while MIME mapper don't: exploit.pHp

  • multiple extensions: exploit.php.jpg

  • trailing chars: exploit.php.

  • URL encoding; works if the decode is server-side: exploit%2Ephp

  • if validation code use high-level (PHP/Java) but the server uses lower-level (C/C++), we can manipulate the filename ending with ; or null byte: exploit.asp;.jpg or exploit.asp%00.jpg

  • multibyte unicode characters, like xC0 x2E, xC4 xAE or xC0 xAE may be translated to x2E in UTF-8: exploit%C0%2Ephp

  • string stripping: exploit.p.phphp

Other types of flawed validation:

  • checking if the file has dimensions (to make sure it's an image)

  • checking the file signature, usually using the file program on linux (much better, but still not foolproof)

File upload race conditions

  • some apps dont upload files directly to the filesystem, but uses temporary place and randomize name to prevent overwriting

  • only then would they validate and send it to the actual filesystem

  • some system doesn't; a system may let the file sit in the file system, then remove it if it doesn't pass the validation. this is usually the case if they rely on some antivirus program.

  • if the file upload is using url, the server has to fetch from the internet and then perform the validation. when the file is loaded using http, devs cant use framework built in functions, so they make their own implementation in storing and validating the file

  • we can bruteforce the directory name if it's generated using pseudo-random functions like PHP's uniqid(). we can lengthen the time window of file processing by uploading large file and putting the payload at the beginning

other exploit than RCE

  • client-side scripts: upload html or svg, then use the <script> tag for XSS (restricted by the same origin policy)

  • vulns specific to the parsing or processing of diff file formats (exp. xml-based files like .doc and .xls might be a potential vector for XXE injection attacks)

using PUT requests

  • check by sending OPTIONS requests

  • put can upload files, even when it's not possible in the web interface

PUT /images/exploit.php HTTP/1.1
Host: vulnerable-website.com
Content-Type: application/x-httpd-php
Content-Length: 49

<?php echo file_get_contents('/path/to/file'); ?>

Lab solutions

Lab 1

Just upload the file

Lab 2

Upload, then change content-type to image/png

Lab 3

  • change file name utilizing directory traversal -> filename="../exploit.php"

  • notice the msg The file avatars/exploit.php has been uploaded., meaning that the payload has been stripped

  • obfuscate payload with url encode, filename="%2e%2e%2fexploit.php"

  • script is uploaded, now GET files/exploit.php

Lab 4

  • upload .htaccess with this setting: AddType application/x-httpd-php .evil ('evil' is an arbitrary file ext)

  • it basically will read any file ending in .evil as php file, so it's able to run exploit.evil as php

  • it will bypass the php filter, and will show like this in the http header:

Content-Disposition: form-data; name="avatar"; filename="exploit.evil"
Content-Type: application/octet-stream
  • after exploit.evil is uploaded, just do access/http request to the file

Lab 5

It seems that the validator only accepts a file if it ends in .png, and the MIME type mapping is based on the file extension. Here we can still put the .png while cutting the filename in the middle with a null byte. Payload: filename="exploit.php%00.png"

Lab 6

Get a png file and append the php code at the end of the file, then upload. php code would still run even when there's gibberish at the beginning. Payload: filename="exploit.php" and Content-Type: application/x-php

PreviousPath TraversalNextSSRF Attacks

Last updated 6 months ago

🏴
🌐