Hướng dẫn php trick ctf - thủ thuật php ctf

Kon hènichiwa folks. Tôi đã dành rất nhiều thời gian để chơi CTFS năm ngoái (2019), đặc biệt là những thách thức trên web. Tôi thấy chúng rất hấp dẫn khi sự hồi hộp mà bạn nhận được sau khi bắt những lá cờ không thể được mô tả bằng lời, rằng adrenaline Rush là thiên đường đối với tôi. Đối với tôi CTF là cách tốt nhất để thực hành, cải thiện và kiểm tra kỹ năng hack của bạn..I spent lot a time playing CTFs last year(2019), especially Web Challenges. I find them very fascinating as the thrill you get after capturing the flags cannot be described in words , That adrenaline rush is heaven for me. For me CTFs are the best way to practice,improve and test your hacking skills.

Trong bài viết này, tôi sẽ đưa tin về một số thách thức web dựa trên PHP mà tôi đã giải quyết trong các CTF khác nhau và một số thủ thuật.

1- Một sự khởi động bình thường

Mô tả thử thách cho chúng ta một gợi ý rất quan trọng, tức là gợi ý: Xem cách thức hoạt động của preg_replaceHINT : see how preg_replace works

Nó cũng nói rằng hãy cố gắng tiếp cận Super_Secret_Function (). Bây giờ hãy xem mã nguồn.Try to reach super_secret_function(). Now lets see the source code.

index.php

Hãy phân tích mã nguồn.

> anime_is_bae : It is a GET parameter. we can supply its value as:https://asm0d3us-ctf.herokuapp.com/Challenge1/index.php?anime_is_bae=[some-text-here]> your_entered_string : Our supplied input via anime_is_bae parameter is stored in this variable.> intermediate_string : it contains a string ‘hellotherehooman’.> final_string : It contains the result of the preg_replace function.Then a final check is taking place, if final_string is equals to intermediate_string(i.e. hellotherehooman ) then the super_secret_function() is getting called and eventually we will get the flag.

Bây giờ hãy tập trung vào hàm preg_replace, nó đang thực hiện ba đối số intermediate_string, '', your_entered_string.preg_replace function , it is taking three arguments intermediate_string, '', your_entered_string .

Hướng dẫn sử dụng PHP cho preg_replace có thể được tìm thấy ở đây: https://www.php.net/manual/en/function.preg-replace.php

Sau khi đọc hướng dẫn sử dụng chức năng preg_replace, rõ ràng là đầu vào được cung cấp của chúng tôi (hoặc nền tảng của đầu vào của chúng tôi) sẽ được so sánh với chuỗi Hellotherehooman, và nếu chúng được cung cấp đầu vào của chúng tôi sẽ được thay thế bằng nothing/blank/''. Khá gọn gàng? ugh. Nhưng trong Final_String, chúng ta cần Hellotherehooman, làm thế nào có thể khi nó được thay thế bằng nothing/blank/''?.hellotherehooman, and if they are equalthen our supplied input is getting replaced with nothing/blank/'' . Pretty neat? ugh. But in the final_string we need hellotherehooman , how it is possible when it is getting replaced with nothing/blank/'' ?.

Cho phép chạy mã của chúng tôi và kiểm tra các đầu vào đã làm thủ công của chúng tôi.

Tôi đã cung cấp Hellotherehooman làm đầu vào của chúng tôi, Hellotherehooman đang được so sánh với Hellotherehooman và nó được thay thế bằng

1 - when your_entered_string is : hello$php main.php
Final String is : hello
No Success
2 - when your_entered_string is : hellothere$php main.php
Final String is : hellothere
No Success
3 - when our supplied input is : hellotherehooman$php main.php
Final String is :
No Success
0.hellotherehooman as our input , hellotherehooman is getting compared with hellotherehooman and it is replaced with
1 - when your_entered_string is : hello$php main.php
Final String is : hello
No Success
2 - when your_entered_string is : hellothere$php main.php
Final String is : hellothere
No Success
3 - when our supplied input is : hellotherehooman$php main.php
Final String is :
No Success
0 .

Hãy chạy mã của chúng tôi với các trường hợp/đầu vào kiểm tra khác nhau

1 - when your_entered_string is : hello$php main.php
Final String is : hello
No Success
2 - when your_entered_string is : hellothere$php main.php
Final String is : hellothere
No Success
3 - when our supplied input is : hellotherehooman$php main.php
Final String is :
No Success

Bây giờ chúng ta biết nó hoạt động như thế nào, nó chỉ kiểm tra xem Hellotherehooman có mặt trong chuỗi hay không, nếu nó có mặt thì nó đang thay thế Hellotherehooman bằng

1 - when your_entered_string is : hello$php main.php
Final String is : hello
No Success
2 - when your_entered_string is : hellothere$php main.php
Final String is : hellothere
No Success
3 - when our supplied input is : hellotherehooman$php main.php
Final String is :
No Success
0hellotherehooman is present in the string or not , if it is present then it is replacing hellotherehooman with
1 - when your_entered_string is : hello$php main.php
Final String is : hello
No Success
2 - when your_entered_string is : hellothere$php main.php
Final String is : hellothere
No Success
3 - when our supplied input is : hellotherehooman$php main.php
Final String is :
No Success
0

Vì vậy, chúng tôi có thể tạo một đầu vào cuối cùng như:

1 - when your_entered_string is : hello$php main.php
Final String is : hello
No Success
2 - when your_entered_string is : hellothere$php main.php
Final String is : hellothere
No Success
3 - when our supplied input is : hellotherehooman$php main.php
Final String is :
No Success
2, ở đây ngay cả khi điều đó làm nổi bật Hellotherehooman được thay thế bằng ____13, việc bắt đầu xin chào và kết thúc đóhellotherehooman gets replaced with
1 - when your_entered_string is : hello$php main.php
Final String is : hello
No Success
2 - when your_entered_string is : hellothere$php main.php
Final String is : hellothere
No Success
3 - when our supplied input is : hellotherehooman$php main.php
Final String is :
No Success
3 , the starting hello and ending therehooman will addup and make a new hellotherehooman

3 - when our supplied input is : hellohellotherehoomantherehooman
$php main.php

Final String is : hellotherehooman
Success

URL cuối cùng:

1 - when your_entered_string is : hello$php main.php
Final String is : hello
No Success
2 - when your_entered_string is : hellothere$php main.php
Final String is : hellothere
No Success
3 - when our supplied input is : hellotherehooman$php main.php
Final String is :
No Success
4

Yay

Được rồi, thử thách trên chỉ là một thử thách cơ bản, bây giờ hãy thảo luận về một số vấn đề với PHP.

2- loại tung hứng

PHP rất dễ dàng cho đến khi bạn bắt gặp các loại biến và bối cảnh trong đó biến được sử dụng. until you come across the variable types and context in which the variable is used.

Hiện tại, hãy tập trung vào bốn loại biến chính

1 - when your_entered_string is : hello$php main.php
Final String is : hello
No Success
2 - when your_entered_string is : hellothere$php main.php
Final String is : hellothere
No Success
3 - when our supplied input is : hellotherehooman$php main.php
Final String is :
No Success
5,
1 - when your_entered_string is : hello$php main.php
Final String is : hello
No Success
2 - when your_entered_string is : hellothere$php main.php
Final String is : hellothere
No Success
3 - when our supplied input is : hellotherehooman$php main.php
Final String is :
No Success
6,
1 - when your_entered_string is : hello$php main.php
Final String is : hello
No Success
2 - when your_entered_string is : hellothere$php main.php
Final String is : hellothere
No Success
3 - when our supplied input is : hellotherehooman$php main.php
Final String is :
No Success
7,
1 - when your_entered_string is : hello$php main.php
Final String is : hello
No Success
2 - when your_entered_string is : hellothere$php main.php
Final String is : hellothere
No Success
3 - when our supplied input is : hellotherehooman$php main.php
Final String is :
No Success
8.major types of variables
1 - when your_entered_string is : hello$php main.php
Final String is : hello
No Success
2 - when your_entered_string is : hellothere$php main.php
Final String is : hellothere
No Success
3 - when our supplied input is : hellotherehooman$php main.php
Final String is :
No Success
5 ,
1 - when your_entered_string is : hello$php main.php
Final String is : hello
No Success
2 - when your_entered_string is : hellothere$php main.php
Final String is : hellothere
No Success
3 - when our supplied input is : hellotherehooman$php main.php
Final String is :
No Success
6 ,
1 - when your_entered_string is : hello$php main.php
Final String is : hello
No Success
2 - when your_entered_string is : hellothere$php main.php
Final String is : hellothere
No Success
3 - when our supplied input is : hellotherehooman$php main.php
Final String is :
No Success
7 ,
1 - when your_entered_string is : hello$php main.php
Final String is : hello
No Success
2 - when your_entered_string is : hellothere$php main.php
Final String is : hellothere
No Success
3 - when our supplied input is : hellotherehooman$php main.php
Final String is :
No Success
8 .

Biến các biến ở khắp mọi nơi

Như bạn đã thấy ở trên rằng trong PHP không cần chỉ định các loại biến, thay vào đó, nó chỉ yêu cầu tên của biến có giá trị của nó, nếu có, được gán cho chúng.

Loại tung hứng không phải là một lỗ hổng hoặc lỗ hổng, thay vào đó là một tính năng của PHP. Như chúng ta đã thấy, PHP là một ngôn ngữ được gõ lỏng lẻo, và do đó, có khả năng lấy loại dữ liệu của giá trị, như của biến, mà giá trị đó được gán.is not a vulnerability or flaw,rather it is a feature of php. As we have seen already, PHP is a loosely typed language, and hence, capable of taking the data type of the value, as that of the variable, to which that value is assigned.

Thí dụ :

Chúng tôi đã nhận được câu trả lời của chúng tôi là 1338, Wierd? Không, nó không phải.! Đó là cách các công việc PHP. Vì một trong các toán hạng là số nguyên thì phần còn lại (chuỗi) sẽ tự động được chuyển đổi thành số nguyên. Mặc dù chuyển đổi thường thì số nguyên ở phía trước chuỗi được lấy làm giá trị của biến, nếu không có số nguyên phía trước chuỗi thì giá trị của nó sẽ là 0.1338 , wierd ? no it isnt .! thats how php works.We are given so much independence while working with variables, In the above above example $var1 is integer , while $var2 is a string . Since one of the operand is integer then the other one (string) will automatically be converted into integer. While conversion usually the integer in front of the string is taken as the value of the variable, if there is no integer in front of the string then its value will be 0.

Thí dụ

Trước khi tiếp tục, hãy hiểu một chút về so sánh trong PHP, có hai loại so sánh trong PHP, so sánh lỏng lẻo (== /! =) Và so sánh nghiêm ngặt (=== /! ==). Bản thân so sánh lỏng lẻo không phải là một lỗ hổng mà là tính năng, nhưng các nhà phát triển thường mắc lỗi trong khi xử lý các trường hợp cạnh của các so sánh lỏng lẻo. Và theo như tôi đã thấy sự so sánh lỏng lẻo+loại tung hứng loại sinh ra sự dễ bị tổn thương nghiêm trọng (tùy thuộc vào bối cảnh), trong những trường hợp tồi tệ nhất, chúng có thể dẫn đến việc xác thực Bỏ qua, một tài khoản tiếp quản tài khoản, phá vỡ các chức năng mật mã. Đáng sợ phải không?

So sánh lỏng lẻo

từ tài liệu PHP chính thức

So sánh nghiêm ngặt

từ tài liệu PHP chính thức

So sánh nghiêm ngặt Classic CTF examples.

Thí dụ

Trước khi tiếp tục, hãy hiểu một chút về so sánh trong PHP, có hai loại so sánh trong PHP, so sánh lỏng lẻo (== /! =) Và so sánh nghiêm ngặt (=== /! ==). Bản thân so sánh lỏng lẻo không phải là một lỗ hổng mà là tính năng, nhưng các nhà phát triển thường mắc lỗi trong khi xử lý các trường hợp cạnh của các so sánh lỏng lẻo. Và theo như tôi đã thấy sự so sánh lỏng lẻo+loại tung hứng loại sinh ra sự dễ bị tổn thương nghiêm trọng (tùy thuộc vào bối cảnh), trong những trường hợp tồi tệ nhất, chúng có thể dẫn đến việc xác thực Bỏ qua, một tài khoản tiếp quản tài khoản, phá vỡ các chức năng mật mã. Đáng sợ phải không?You Win Boi ,The only variable we can control is

1 - when your_entered_string is : hello$php main.php
Final String is : hello
No Success
2 - when your_entered_string is : hellothere$php main.php
Final String is : hellothere
No Success
3 - when our supplied input is : hellotherehooman$php main.php
Final String is :
No Success
9, this variable
1 - when your_entered_string is : hello$php main.php
Final String is : hello
No Success
2 - when your_entered_string is : hellothere$php main.php
Final String is : hellothere
No Success
3 - when our supplied input is : hellotherehooman$php main.php
Final String is :
No Success
9is getting converted to its md5 hash and then it is compared with 0 if true then the message You Win Boi will be printed. However it seems impossible as md5 hashes are of 16bytes (32 bytes if you hexlify them), In short practically they can never be 0. If we look closely at the source code we can see that that there is a loose comparison (==) between the md5 hash and 0(In loose comparison only value is checked not the type of the variable), we can exploit this comparison to print our win message. So somehow we need to find a value whose md5 hash starts with
3 - when our supplied input is : hellohellotherehoomantherehooman
$php main.php

Final String is : hellotherehooman
Success
1 (e is exponential operator in php) then the whole md5 hash will be treated as 0,(all thanks to type juggling and php loose comparison).
3 - when our supplied input is : hellohellotherehoomantherehooman
$php main.php

Final String is : hellotherehooman
Success
2 has its md5 hash starting with
3 - when our supplied input is : hellohellotherehoomantherehooman
$php main.php

Final String is : hellotherehooman
Success
1 , hence we can set
3 - when our supplied input is : hellohellotherehoomantherehooman
$php main.php

Final String is : hellotherehooman
Success
4 variable to
3 - when our supplied input is : hellohellotherehoomantherehooman
$php main.php

Final String is : hellotherehooman
Success
2 , then our win message will be printed.

3 - when our supplied input is : hellohellotherehoomantherehooman
$php main.php

Final String is : hellotherehooman
Success
6

Giành chiến thắng

Một vi dụ khac

Mục tiêu một lần nữa là giống nhau (thông báo win in), lần này là băm MD4, thông báo thắng sẽ được in nếu giá trị của

3 - when our supplied input is : hellohellotherehoomantherehooman
$php main.php

Final String is : hellotherehooman
Success
4 sẽ bằng với băm MD4 của nó, có vẻ không thể nhưng nhờ vào so sánh lỏng lẻo của PHP và loại tung hứng chúng tôi Có thể làm điều đó, chúng ta chỉ cần tìm ra một giá trị bắt đầu bằng
3 - when our supplied input is : hellohellotherehoomantherehooman
$php main.php

Final String is : hellotherehooman
Success
1 và băm MD4 của nó cũng phải bắt đầu với
3 - when our supplied input is : hellohellotherehoomantherehooman
$php main.php

Final String is : hellotherehooman
Success
9 đến nỗi trong so sánh lỏng lẻo, hai giá trị này sẽ được coi là 0 và 0 == 0 là đúng do đó, thông báo chiến thắng của chúng ta sẽ được in .0==0 is True hence our win message will be printed.

Tôi đã tìm thấy một giá trị bắt đầu bằng

3 - when our supplied input is : hellohellotherehoomantherehooman
$php main.php

Final String is : hellotherehooman
Success
1 và băm MD4 của nó cũng bắt đầu bằng
3 - when our supplied input is : hellohellotherehoomantherehooman
$php main.php

Final String is : hellotherehooman
Success
1 từ John Hammond, CTF KatanaJohn Hammond’s CTF Katana

plaintext :  0e001233333333333334557778889
md4 hash : 0e434041524824285414215559233446
Win thắng

3- Kiểm soát các loại biến

Hãy cố gắng lấy cờ ở đây

Mã phân hủy:

> A file flag.php is included which stores our flag.> First if condition uses isset() functions which is just for making sure that the name and password variables are not empty.> Second if(nested one) makes sure that they are not equal.> 'else if' computes the SHA1 hashes of both name and password , and if they are equal then we will get our flag. 

Không thể có hai thực thể không bình đẳng có cùng băm SHA1, cũng cần lưu ý rằng có một so sánh nghiêm ngặt (===) không phải là một loại lỏng lẻo. (Vì vậy, thủ thuật

3 - when our supplied input is : hellohellotherehoomantherehooman
$php main.php

Final String is : hellotherehooman
Success
1 của chúng tôi sẽ không hoạt động ở đây). Các giá trị (tên và mật khẩu) đang được nhập thông qua tham số yêu cầu Get và do đó chúng tôi có thể kiểm soát giá trị cũng như loại biến, nếu chúng tôi gửi các biến (tên và mật khẩu) của mảng loại thì chúng tôi có thể bỏ qua kiểm tra SHA1 , vì kiểm tra Sha1 đó sẽ chỉ được thực thi nếu loại biến (tên và mật khẩu) là chuỗi, hãy nhớ kiểm tra so sánh nghiêm ngặt cho cả loại và giá trị và nếu loại không khớp thì nó sẽ đơn giản bỏ qua điều kiện và chúng ta sẽ nhận được Lá cờ của chúng tôi.type of the variable , if we send variables (name and password) of type array then we can bypass the SHA1 check , as that SHA1 check will only be executed if the type of the variables (name and password) is string , remember strict comparison checks for both type and value, and if the type doesnt matches then it will simply ignore the condition and we will get our flag.

Khai thác :

http://vulnerable/?name[]=x&password[]=y

Đảm bảo rằng các giá trị của tên và mật khẩu là khác nhau (x và y), vì thứ hai nếu (kiểm tra xem các giá trị có bằng hoặc không) vẫn được thực thi.

Tìm hiểu làm thế nào bạn có thể nhận được cờ, DM cho tôi câu trả lời: Ở đâyDM me the answer : here

4- Bỏ qua sử dụng null byte trong Ereg (không dùng nữa)

Ereg () tìm kiếm một

1 - when your_entered_string is : hello$php main.php
Final String is : hello
No Success
2 - when your_entered_string is : hellothere$php main.php
Final String is : hellothere
No Success
3 - when our supplied input is : hellotherehooman$php main.php
Final String is :
No Success
7 cho các trận đấu với biểu thức chính quy được đưa ra trong
plaintext :  0e001233333333333334557778889
md4 hash : 0e434041524824285414215559233446
4 theo cách nhạy cảm trường hợp. .

plaintext :  0e001233333333333334557778889
md4 hash : 0e434041524824285414215559233446
5

Trong ví dụ trên, Regex có thể được bỏ qua bằng cách sử dụng null byte (%00)

Tham khảo: https://bugs.php.net/bug.php?id=44366 (không phải là một lỗi tho)

5- Thực thi mã

Dưới đây là một số chức năng PHP có thể được sử dụng để đạt được thực thi mã trực tiếp.

eval();

assert();

system();

exec();

shell_exec();

passthru();

escapeshellcmd();

pcntl_exec();

Sayonara

Chăm sóc ❤

Twitter của tôi: https://twitter.com/0xasm0d3us