Php ctf

Konnichiwa Folks. Tôi đã dành rất nhiều thời gian để chơi CTF vào năm ngoái (2019), đặc biệt là Web Challenges. Tôi thấy chúng rất hấp dẫn vì cảm giác hồi hộp mà bạn có được sau khi chiếm được những lá cờ không thể diễn tả bằng lời, Cơn sốt adrenaline đó là thiên đường đối với tôi. Đối với tôi, CTF là cách tốt nhất để luyện tập, cải thiện và kiểm tra kỹ năng hack của bạn

Show

Trong bài viết này, tôi sẽ trình bày hướng dẫn 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- Khởi động thông thường

Mô tả thử thách cho chúng ta một gợi ý rất quan trọng. e. GỢI Ý. xem cách hoạt động của preg_replace

Nó cũng nói Cố gắng đạt được super_secret_function(). Bây giờ hãy xem mã nguồn

mục lục. 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 lấy ba đối số

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
1

Có thể tìm thấy hướng dẫn sử dụng PHP cho preg_replace tại đây. https. //www. php. net/thủ công/vi/chức năng. thay thế trước. php

Sau khi đọc hướng dẫn sử dụng hàm preg_replace, rõ ràng là đầu vào được cung cấp của chúng tôi (hoặc chuỗi con của đầu vào của chúng tôi) đang được so sánh với chuỗi hellotherehooman và nếu chúng bằng nhau thì đầu vào được cung cấp của chúng tôi sẽ đượ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
2. Khá gọn gàng? . Nhưng trong final_string chúng ta cần hellotherehooman , làm sao có thể thực hiện được khi 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
2 ?

Hãy chạy mã của chúng tôi và kiểm tra các đầu vào 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

Hãy chạy mã của chúng tôi với các trường hợp thử nghiệm/Đầu vào 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ỉ đang kiểm tra xem hellotherehooman có trong chuỗi hay không, nếu có thì nó sẽ 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
0

Vì vậy, chúng ta 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 hellotherehooman được đánh dấu đó đượ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
3 , hellotherehooman bắt đầu và kết thúc therehooman sẽ cộng lại và tạo thành một hellotherehooman mới

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

vâng

Được rồi, Thử thách trên chỉ là 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- Kiểu tung hứng

PHP thật dễ dàng cho đến khi bạn bắt gặp các loại biến và ngữ cảnh mà biến được sử dụng

Bây giờ, 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

biến biến ở khắp mọi nơi

Như bạn đã thấy ở trên, trong php không cần chỉ định loại biến, Thay vào đó, nó chỉ yêu cầu tên của biến cùng với giá trị của nó, nếu có, để gán cho chúng

Type Juggling không phải là một lỗ hổng hay lỗ hổng, mà nó 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 kiểu dữ liệu của giá trị, làm kiểu dữ liệu của biến, mà giá trị đó được gán

Ví dụ

Chúng tôi nhận được câu trả lời của bạn là 1338, lạ không? . đó là cách php hoạt động. Chúng tôi được trao rất nhiều độc lập khi làm việc với các biến, Trong ví dụ trên $var1 là số nguyên, trong khi $var2 là một chuỗi. Vì một trong các toán hạng là số nguyên nên toán hạng còn lại (chuỗi) sẽ tự động được chuyển đổi thành số nguyên. Trong khi 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

Ví 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 chặt chẽ (=== /. ==). Bản thân so sánh lỏng lẻo không phải là một lỗ hổng mà nó là một tính năng, nhưng các nhà phát triển thường mắc lỗi khi xử lý các trường hợp cạnh tranh của so sánh lỏng lẻo. Và theo như tôi đã thấy, so sánh lỏng lẻo + tung hứng kiểu sẽ sinh ra lỗ hổng nghiêm trọng (tất nhiên tùy thuộc vào ngữ cảnh), Trong trường hợp xấu nhất, chúng có thể dẫn đến “Bỏ qua xác thực”, “Chiếm đoạt 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

Bây giờ hãy lấy một số ví dụ CTF cổ điển

Ví dụ

Trong ví dụ trên, mục tiêu của chúng tôi là in thông báo You Win Boi, Biến duy nhất chúng tôi có thể kiểm soát là

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, biến này
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 đang được chuyển đổi thành hàm băm md5 của nó và sau đó nó được so sánh với 0 nếu đúng thì thông báo You Win Boi . Tuy nhiên, có vẻ như không thể vì băm md5 là 16 byte (32 byte nếu bạn hexlify chúng), Nói tóm lại, thực tế chúng không bao giờ có thể bằng 0. Nếu chúng ta xem kỹ mã nguồn, chúng ta có thể thấy rằng có một so sánh lỏng lẻo (==) giữa hàm băm md5 và 0 (Trong so sánh lỏng lẻo, chỉ giá trị được kiểm tra chứ không phải loại biến), chúng ta có thể khai thác so sánh này để . Vì vậy, bằng cách nào đó, chúng ta cần tìm một giá trị có hàm băm md5 bắt đầu bằng
3 - when our supplied input is : hellohellotherehoomantherehooman
$php main.php

Final String is : hellotherehooman
Success
1 (e là toán tử hàm mũ trong php) thì toàn bộ hàm băm md5 sẽ được coi là 0, (tất cả là nhờ vào kiểu tung hứng và phép so sánh lỏng lẻo của php).
3 - when our supplied input is : hellohellotherehoomantherehooman
$php main.php

Final String is : hellotherehooman
Success
2 có hàm băm md5 bắt đầu bằng
3 - when our supplied input is : hellohellotherehoomantherehooman
$php main.php

Final String is : hellotherehooman
Success
1 , do đó chúng tôi có thể đặt biến
3 - when our supplied input is : hellohellotherehoomantherehooman
$php main.php

Final String is : hellotherehooman
Success
4 thành
3 - when our supplied input is : hellohellotherehoomantherehooman
$php main.php

Final String is : hellotherehooman
Success
2 , sau đó thông báo chiến thắng của chúng tôi sẽ được in

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ột lần nữa, mục tiêu vẫn như cũ(in thông báo thắng), lần này là hàm 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 bằng với hàm băm md4 của nó, điều này dường như là không thể nhưng nhờ vào sự so sánh lỏng lẻo của php và cách tung hứng kiểu mà chúng tôi

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à hàm 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ừ CTF Katana của John Hammond

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

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

Hãy cố gắng để có được lá cờ ở đây

phân tích mã

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

Không thể có hai thực thể không bằng nhau có cùng hàm băm SHA1, cũng cần lưu ý rằng có một phép so sánh chặt chẽ (===) không phải là một phép so sánh 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 loại mảng thì chúng tôi có thể bỏ qua kiểm tra SHA1

Khai thác

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

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

Tìm ra cách bạn có thể nhận được cờ, DM cho tôi câu trả lời. đây

4- Bỏ qua Regex bằng cách sử dụng Null Byte trong ereg (Hiện 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 để khớp với biểu thức chính quy được đưa ra trong
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
54 theo cách phân biệt chữ hoa chữ thường. (Chức năng này KHÔNG ĐƯỢC DÙNG trong PHP 5. 3. 0 và ĐƯỢC LOẠI BỎ trong PHP 7. 0. 0. ). Vì vậy, bất cứ khi nào bạn thấy ereg được sử dụng trong thử thách CTF php thì có gì đó đáng ngờ