# TAMUCTF ## forgotten-password - Bài này cũng có thể nói là một trick dùng phising và viết bằng RUBY- oke view qua source của nó tí nào(mình đọc cả buổi chiều :))) ![image](https://hackmd.io/_uploads/SkU_Va7xR.png) - Nhìn có vẻ loằng ngoằng thì mình giải thích một chút nha. - Đầu tiên vào trong trang web có chức năng đăng nhập. Và sever khum cung cấp mật khẩu cho mình vì vậy mình phải forget-password để sever gửi mail vào tài khoản của mình. ![image](https://hackmd.io/_uploads/SJNJSTXeA.png) - Sau khi vào trang forget sẽ có giao diện như này: ![image](https://hackmd.io/_uploads/BJ-ZBpmx0.png) - Nếu nhập sai thì nó sẽ báo hoặc nếu mà nhập các email không có trong database. - Đây là phần controller của nó: ![image](https://hackmd.io/_uploads/HJcHr6mxA.png) - Và nếu nhập đúng thì bạn sẽ được sent email là flag nhá:> ![image](https://hackmd.io/_uploads/Bk_dBT7eA.png) - Khum hiểu sao khi build docker mình không build lại được cho nên mình sẽ nói về hướng giải quyết nha: - Để ý một chút trong file `auth_controller.rb` ``` class AuthController < ApplicationController def login end def forget end def recover user_found = false User.all.each { |user| if params[:email].include?(user.email) user_found = true break end } if user_found RecoveryMailer.recovery_email(params[:email]).deliver_now redirect_to forgot_password_path, notice: 'Password reset email sent' else redirect_to forgot_password_path, alert: 'You are not a registered user!' end end end ``` - Có thể thấy là đoạn này sever set mặc định `user_found=false` và kiểm tra tất cả các email có trong database nếu có thì `set user_found=true` lúc này mới gọi đến phương thức `recovery_email` của class `RecoveryMailer` ![image](https://hackmd.io/_uploads/H1d58p7g0.png) - Trick ở đây là mình sẽ sử dụng dấu `;` thì lúc này sever sẽ chỉ check một email ![image](https://hackmd.io/_uploads/B1RMv6mlC.png) `b8500763@gmail.com` Payload : `b8500763@gmail.com;your-email@gmail.com` Flag : ![image](https://hackmd.io/_uploads/rJLtwamgA.png) ## Cereal - Đây tiếp tục là một bài easy với mã nguồn php liên quan đến serialize mà sqli:> ![image](https://hackmd.io/_uploads/rJK4Oa7gC.png) - Trang web có chức năng đăng nhập và lưu lại username và password với giá trị deserialize. - Đây là đoạn code quan trọng của bài: ``` <?php class User { public $username = ''; public $id = -1; protected $password = ''; protected $profile; public function setPassword($pass) { $this->password = $pass; } public function sendProfile() { return $this->profile; } public function refresh() { // Database connection $conn = new PDO('sqlite:../important.db'); $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $query = "select username, email, favorite_cereal, creation_date from users where `id` = '" . $this->id . "' AND `username` = '" . $this->username . "'"; $stmt = $conn->prepare($query); $stmt->execute(); $row = $stmt->fetch(); $this->profile = $row; } public function validate() { // Database connection $conn = new PDO('sqlite:../important.db'); $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $query = "select * from users where `username` = :username"; $stmt = $conn->prepare($query); $stmt->bindParam(':username', $this->username); $stmt->execute(); $row = $stmt->fetch(); if (md5($row['password']) !== $this->password) { header('Location: logout.php'); exit; } } public function __wakeup() { $this->validate(); $this->refresh(); } } ?> ``` - Như ta có thể thấy thì magic method __wakeup() sẽ luôn được gọi khi giá trị được deserialize. - Lúc này 2 phương thức validate() và refresh chắc chắn được gọi. - Đề bài cho chúng ta tài khoảng user để đăng nhập và vào cookie chúng ta cũng thấy giá trị. - Ta có thể thấy nếu mà đúng thì 2 phương thức này được gọi với người dùng `lương thiện` còn ta muốn dùng không lương thiện thì khá hóc nhưng mà khá dễ thôi mục tiêu là ta có thể lợi dụng refresh để lấy lại giá trị password của admin với sqli chắc chắn dính ở đây. - Vượt qua validate thì ta để nguyên username và password như cũ. - Đến refresh mình khá `ngu` vì ban đầu mình sqli được nhưng mà sử dụng `and` với `or` bị detect mình cứ tưởng là do ``$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); `` - Sau test lại theo lời chỉ giáo của anh `Ngọc` thì đã được:((( FLAG : ![image](https://hackmd.io/_uploads/HJCS5TQlC.png) ## Remote - Tiếp tục là một bài white box với mã nguồn php filter khá `cẩn thận` bằng thư viện.