# OverTheWire - Bandit ## Bandit Level 0 ```! The goal of this level is for you to log into the game using SSH. The host to which you need to connect is bandit.labs.overthewire.org, on port 2220. The username is bandit0 and the password is bandit0. Once logged in, go to the Level 1 page to find out how to beat Level 1. ``` Level này ta chỉ cần login được vào ssh server với host là `bandit.labs.overthewire.org` account `bandit0:bandit0` ở port 2220 ![image](https://hackmd.io/_uploads/SJ3XGLHalx.png) Login thành công: ![image](https://hackmd.io/_uploads/H1D8f8STel.png) ## Bandit Level 0 → Level 1 **Level Goal** ```! The password for the next level is stored in a file called readme located in the home directory. Use this password to log into bandit1 using SSH. Whenever you find a password for a level, use SSH (on port 2220) to log into that level and continue the game. ``` Ta chỉ cần đọc file `readme` ở thư mục home là xong ![image](https://hackmd.io/_uploads/ryBpzUBaxx.png) > Password: ZjLjTmM6FvvyRnrb2rfNWOZOTa6ip5If ## Bandit Level 1 → Level 2 **Level Goal** ```! The password for the next level is stored in a file called - located in the home directory ``` Login lại ssh server với user là `bandit1` và password lấy từ level trước, sẽ có một file là `-` ![image](https://hackmd.io/_uploads/rkEfr8Spex.png) Vì file này có tên là 1 ký tự đặc biệt nên để xem file ta dùng lệnh: ``` cat ./- ``` ![image](https://hackmd.io/_uploads/BkPhrLH6ll.png) > Password:263JGJPfgU6LtdEvgfWU1XP5yac29mFx ## Bandit Level 2 → Level 3 **Level Goal** ```! The password for the next level is stored in a file called --spaces in this filename-- located in the home directory ``` Ssh vào server sẽ thấy môt file có tên là `spaces in this filename` ![image](https://hackmd.io/_uploads/Bk_4PISagl.png) Để đọc file này có rất nhìu cách, ví dụ: dùng `\` để escape dấu hoặc có thể dùng wildcard để đọc tất cả file trong thư mục home … ![image](https://hackmd.io/_uploads/HkQZ_ISplx.png) > Password: MNk8KNH3Usiio41PRUEoDFPqfxLPlSmx ## Bandit Level 3 → Level 4 **Level Goal** ```! The password for the next level is stored in a hidden file in the inhere directory. ``` Có một thư mục tên là `inhere` ở thư mục home. Và nhiệm vụ của ta là đọc được file ẩn trong thư mục này. Để list ra tất cả các file trong thư mục hiện tại sử dụng lệnh `ls -al` và đọc nó: ![image](https://hackmd.io/_uploads/ByIp_8HTxx.png) > Password: 2WmrDFRmJIq3IPxneAaMGhap0pFhF3NJ ## Bandit Level 4 → Level 5 **Level Goal** ```! The password for the next level is stored in the only human-readable file in the inhere directory. Tip: if your terminal is messed up, try the “reset” command. ``` List ra các file trong thư mục `inhere` ![image](https://hackmd.io/_uploads/S1gG5IrTex.png) Dùng command `file` để check thuộc tính của các file, và lấy được mật khẩu ![image](https://hackmd.io/_uploads/r1KDqIH6xl.png) > Password:4oQYVPkxZOOEOO5pTW81FB8j8lxXGUQw ## Bandit Level 5 → Level 6 **Level Goal** ```! The password for the next level is stored in a file somewhere under the inhere directory and has all of the following properties: human-readable 1033 bytes in size not executable ``` Trong folder `inhere` bao gồm nhiều folder khác ![image](https://hackmd.io/_uploads/ByGUjISTll.png) Dùng command `find` với các option như sau để thỏa yêu cầu của đề: ![image](https://hackmd.io/_uploads/S1ql2IBagg.png) Cuối cùng chỉ cần cat file này ra là có password ![image](https://hackmd.io/_uploads/BJQSnIr6el.png) > Password: HWasnPhtq9AVKe0dmk45nxy20cvUa6EG ## Bandit Level 6 → Level 7 **Level Goal** ```! The password for the next level is stored somewhere on the server and has all of the following properties: - owned by user bandit7 - owned by group bandit6 - 33 bytes in size ``` Sử dụng lệnh find để tìm file đúng điều kiện và liệt kê thông tin(tìm thêm thông tin câu lệnh find ở [đây](https://manpages.ubuntu.com/manpages/noble/man1/find.1.html)): ``` find / -type f -user bandit7 -group bandit6 -size 33c -ls 2>/dev/null ``` ![image](https://hackmd.io/_uploads/SyXwyDHple.png) > Password:morbNTDkSW6jIlUc0ymOdMaLnOlFVAaj ## Bandit Level 7 → Level 8 **Level Goal** ```! The password for the next level is stored in the file data.txt next to the word millionth ``` Có một file là `data.txt` nằm ở thư mục home, tuy nhiên size khá lớn. Đề gợi ý pass nằm kế string `millionth` nên mình có thể dùng command `grep` để tìm string: ![image](https://hackmd.io/_uploads/SJtIbDHpex.png) > Password:dfwvzFQi4mU0wfNbFOe9RoWskMLg7eEc ## Bandit Level 8 → Level 9 **Level Goal** ```! The password for the next level is stored in the file data.txt and is the only line of text that occurs only once ``` Trong thư mục home có file` data.txt` như đã mô tả Dùng `sort + uniq -c` để tìm dòng xuất hiện 1 lần - password: ![image](https://hackmd.io/_uploads/rk4qrvSaeg.png) > Password: 4CKMh1JI91bUIZZPXDqGanal4xvAg0JM ## Bandit Level 9 → Level 10 **Level Goal** ```! The password for the next level is stored in the file data.txt in one of the few human-readable strings, preceded by several ‘=’ characters. ``` Dùng `grep` với `^=` để tìm các dòng bắt đầu với = trong file data.txt kết hợp với dùng thêm command strings ![image](https://hackmd.io/_uploads/HJiTKPBpge.png) > Password:FGUW5ilLVJrxX9kMYMmlN4MgbpfMiqey ## Bandit Level 10 → Level 11 **Level Goal** ``` The password for the next level is stored in the file data.txt, which contains base64 encoded data ``` File `data.txt` nằm ở thư mục home, thử cat file này ra thì ta thấy nó đã bị base64 encode ![image](https://hackmd.io/_uploads/BkGu5Drpeg.png) Decode file ta thu được password ![image](https://hackmd.io/_uploads/SkFA9DBTlx.png) > Password:dtR173fZKb0RRsDFSGsg2RWnpNVj3qRr ## Bandit Level 11 → Level 12 **Level Goal** ```! The password for the next level is stored in the file data.txt, where all lowercase (a-z) and uppercase (A-Z) letters have been rotated by 13 positions ``` Cat thử file `data.txt`, dựa theo mô tả của đề thì có vẻ như nó đã bị encrypt với `rot13` ![image](https://hackmd.io/_uploads/S1ZJpvB6xg.png) Sử dụng tool rot13 để decrypt và thu được password: ![image](https://hackmd.io/_uploads/BJe0hPSaxg.png) > Password:7x16WNeHIi5YkIhWsfFIqoognUTyj9Q4 ## Bandit Level 12 → Level 13 **Level Goal** ```! The password for the next level is stored in the file data.txt, which is a hexdump of a file that has been repeatedly compressed. For this level it may be useful to create a directory under /tmp in which you can work. Use mkdir with a hard to guess directory name. Or better, use the command “mktemp -d”. Then copy the datafile using cp, and rename it using mv (read the manpages!) ``` file `data.txt`là một bản hexdump (dạng dữ liệu thập lục phân) của một tệp đã bị nén nhiều lần: ![image](https://hackmd.io/_uploads/B1HpTwSaee.png) Tạo một thư mục tạm trong /tmp để làm việc: `mktemp -d`. Sau đó, sao chép tệp `data.txt` vào thư mục đó bằng lệnh: `cp data.txt /tmp/tenthumuc`: ![image](https://hackmd.io/_uploads/r1J7CDH6gg.png) Sau đó ta reverse lại file hexdump này với `xxd` và check thuộc tinh với cmd `file`: ![image](https://hackmd.io/_uploads/rkD9aQ86ll.png) Đổi tên, thêm extension cho file và gz decompress, ta được một file mới là dump ![image](https://hackmd.io/_uploads/SJ7g0XUTle.png) Tiếp tục check thuộc tinh của file này: ![image](https://hackmd.io/_uploads/SkcfAXUaxl.png) Thêm extension cho file và tiếp đục lặp các bước ![image](https://hackmd.io/_uploads/rkiHxN8Tee.png) Ta thu được password ![image](https://hackmd.io/_uploads/ryRUl4U6xx.png) > Password:FO5dwFsc0cbaIiH0h8J2eUks2vdTDwAn ## Bandit Level 13 → Level 14 **Level Goal** ```! The password for the next level is stored in /etc/bandit_pass/bandit14 and can only be read by user bandit14. For this level, you don’t get the next password, but you get a private SSH key that can be used to log into the next level. Note: localhost is a hostname that refers to the machine you are working on ``` ![image](https://hackmd.io/_uploads/H1l2DVIagl.png) Thấy rằng máy đang chứa 1 tệp tin sshkey.private, tệp này được dùng để kết nối đến level tiếp theo. Tiến hành đọc doc để xem cách dùng tệp key này với lệnh ssh như nào. ![image](https://hackmd.io/_uploads/HJusPVLaxx.png) ![image](https://hackmd.io/_uploads/HJAtPNI6xg.png) Option `-i` cho phép chúng ta dùng tệp key để kết nối. Kết hợp với dữ kiện đề cho là localhost là hostname cần kết nối đến. ![image](https://hackmd.io/_uploads/ByNiU4U6ex.png) Tiến hành đọc passwork theo đường dẫn đề yêu cầu ![image](https://hackmd.io/_uploads/S1jk_NUpxl.png) > Password:MU4VWeTyJk8ROof1qqmcBPaLh7lDCPvS ## Bandit Level 14 → Level 15 **Level Goal** ```! The password for the next level can be retrieved by submitting the password of the current level to port 30000 on localhost. ``` Sử dụng lệnh `netcat` để gửi password đã lấy được ở lab trước đến cổng 30000 localhost để lấy password lab này: ![image](https://hackmd.io/_uploads/rJl-FVIpxe.png) > Password:8xCjnmgoKbGLhHFAZlGE5Tmu4M2tKJQo ## Bandit Level 15 → Level 16 **Level Goal** ```! The password for the next level can be retrieved by submitting the password of the current level to port 30001 on localhost using SSL/TLS encryption. Helpful note: Getting “DONE”, “RENEGOTIATING” or “KEYUPDATE”? Read the “CONNECTED COMMANDS” section in the manpage. ``` Đề bài yêu cầu rằng cần gửi password của level hiện tại được mã hoá SSL qua cổng 30001 của localhost. Xem manual của lệnh ncat và search google ta thấy option `--ssl` ![image](https://hackmd.io/_uploads/Syh6q4U6le.png) Tiến hành kết nối đến localhost với mã hoá ssl và gửi password ![image](https://hackmd.io/_uploads/Hy1XsNUplg.png) > Password:kSkvUpMQ7lBYyCM4GBPvCvT1BfWRy0Dx ## Bandit Level 16 → Level 17 **Level Goal** ```! The credentials for the next level can be retrieved by submitting the password of the current level to a port on localhost in the range 31000 to 32000. First find out which of these ports have a server listening on them. Then find out which of those speak SSL/TLS and which don’t. There is only 1 server that will give the next credentials, the others will simply send back to you whatever you send to it. Helpful note: Getting “DONE”, “RENEGOTIATING” or “KEYUPDATE”? Read the “CONNECTED COMMANDS” section in the manpage. ``` Chúng ta cần tìm ra xem cổng nào trong đoạn 31000 đến 32000 sẽ trả về thứ chúng ta cần, là credentials cho level tiếp theo. Sử dụng nmap để scan ta được 5 port đang hoạt động trong đó có 2 port có chứa service ssl: ![image](https://hackmd.io/_uploads/S1_QZBUplg.png) tiến hành gửi từng port để xem port nào gửi về thứ ta cần: ![image](https://hackmd.io/_uploads/rJ48WSLpgg.png) Cổng 31790 trả về thứ cần tìm ![image](https://hackmd.io/_uploads/rkKBbHLael.png) ## Bandit Level 17 → Level 18 **Level Goal** ```! There are 2 files in the homedirectory: passwords.old and passwords.new. The password for the next level is in passwords.new and is the only line that has been changed between passwords.old and passwords.new NOTE: if you have solved this level and see ‘Byebye!’ when trying to log into bandit18, this is related to the next level, bandit19 ``` Đề cho ta 2 tệp và yêu cầu tìm ra dòng khác nhau ở tệp password.new so với tệp password.old: ![image](https://hackmd.io/_uploads/B1IRHrL6gx.png) > Password:x2gLTTjFwMOhQ8oWNbMN362QKxfRqGlO ## Bandit Level 18 → Level 19 **Level Goal** ```! The password for the next level is stored in a file readme in the homedirectory. Unfortunately, someone has modified .bashrc to log you out when you log in with SSH. ``` Khi đăng nhập thì bị break ra =)) ![image](https://hackmd.io/_uploads/rJstRS86ll.png) để né được vấn đề do tệp .bashrc gây ra, bạn cần tránh việc mở một phiên làm việc tương tác (interactive shell). Cách giải quyết chính là sử dụng một tính năng của SSH cho phép bạn thực thi một lệnh duy nhất ngay khi kết nối, thay vì mở một shell đầy đủ. ![image](https://hackmd.io/_uploads/rynAkLIpxg.png) ![image](https://hackmd.io/_uploads/H17xx88pxe.png) > Password:cGWpMaKXVwDUNgPAVJbWYuGHVn9zl3j8 ## Bandit Level 19 → Level 20 **Level Goal** ```! To gain access to the next level, you should use the setuid binary in the homedirectory. Execute it without arguments to find out how to use it. The password for this level can be found in the usual place (/etc/bandit_pass), after you have used the setuid binary. ``` Access vào ta có một tệp tin bandit20-do được highlight màu đỏ ![image](https://hackmd.io/_uploads/r1eYxIUaee.png) Thực thi thử thì thấy đây là file giúp ta thực thi dưới quyền là user bandit20 ![image](https://hackmd.io/_uploads/rkpS-IU6el.png) Tiến hành lấy password ![image](https://hackmd.io/_uploads/SypdZIUTle.png) > Password:0qXahG8ZjOVMN9Ghs7iOWsCfZyXOUbYO > ## Bandit Level 20 → Level 21 **Level Goal** ```! There is a setuid binary in the homedirectory that does the following: it makes a connection to localhost on the port you specify as a commandline argument. It then reads a line of text from the connection and compares it to the password in the previous level (bandit20). If the password is correct, it will transmit the password for the next level (bandit21). NOTE: Try connecting to your own network daemon to see if it works as you think ``` Đọc đề bài ta thấy rằng, tại home sẽ có một tệp setuid binary sẽ kết nối đến cổng mà ta chỉ định tại localhost và sẽ đọc 1 dòng text từ đó và so sánh xem nó có giống với password ở level 20 không, nếu có thì nhả ra password. ![image](https://hackmd.io/_uploads/H1cE9UU6el.png) Đầu tiên ta cần tạo 1 listener tại localhost để lắng nghe kết nối đến. Và sẽ gửi password của level 20 cho bất kì kết nối nào kết nối đến nó. Và đưa nó xuống chạy nền. Và kiểm tra xem có tồn tại hay không ![image](https://hackmd.io/_uploads/Skr55UU6gx.png) Dùng tệp suconnect được cho để kết nối đến và lấy pass ![image](https://hackmd.io/_uploads/rySp588Tlg.png) > Password:EeoULMCra2q0dSkYj561DX7s1CpBuOBt ## Bandit Level 21 → Level 22 **Level Goal** ```! A program is running automatically at regular intervals from cron, the time-based job scheduler. Look in /etc/cron.d/ for the configuration and see what command is being executed. ``` Kiểm tra thư mục `/etc/cron.d/` ta có ![image](https://hackmd.io/_uploads/SkSFjU8Tge.png) Có các thư mục, mục tiêu của chúng ta là tìm ra password của level 22, cat `cronjob_bandit22` tìm hiểu tệp này mần cái gì ![image](https://hackmd.io/_uploads/rkTtnLIpgx.png) Tệp này sẽ set cho câu lệnh `/usr/bin/cronjob_bandit22.sh` thực thi mỗi lần khởi động và mỗi 1 phút. Có thêm manh mối, vào tệp `cronjob_bandit22.sh` xem nó có cái gì. ![image](https://hackmd.io/_uploads/HyjjhIIagl.png) Thấy rằng nó làm 2 việc, set quyền cho tệp `/tmp/t7O6lds9S0RqQh9aMcz6ShpAoZKF7fgv` và truyền password của bandit22 vào tệp này. Đọc tệp này là ta sẽ có được password cần tìm ![image](https://hackmd.io/_uploads/S1lp2LI6xl.png) > Password:tRae0UfB9v0UzbCdn9cY0gQnds9GF58Q ## Bandit Level 22 → Level 23 **Level Goal** ```1 A program is running automatically at regular intervals from cron, the time-based job scheduler. Look in /etc/cron.d/ for the configuration and see what command is being executed. NOTE: Looking at shell scripts written by other people is a very useful skill. The script for this level is intentionally made easy to read. If you are having problems understanding what it does, try executing it to see the debug information it prints. ``` Kiểm tra thư mục `/etc/cron.d/` để xem tập tin cấu hình và xác định lệnh nào đang được thực thi. ![image](https://hackmd.io/_uploads/ryq7TUL6xl.png) Làm tương tự level trước ta có: ![image](https://hackmd.io/_uploads/Hymi68Ipge.png) `echo I am user $myname`: Sẽ in ra chuỗi `"I am user bandit23"`. `md5sum`: Chuỗi này được chuyển đến lệnh md5sum để tính giá trị băm MD5 của nó. `cut -d ' ' -f 1`: Kết quả của md5sum (ví dụ: HASH_VALUE -) được chuyển đến lệnh cut. Lệnh này sẽ tách chuỗi bằng dấu cách (-d ' ') và lấy phần đầu tiên (-f 1), tức là chỉ lấy giá trị hash. Kết luận: Biến `$mytarget` sẽ chứa giá trị hash MD5 của chuỗi `"I am user bandit23"`. ![image](https://hackmd.io/_uploads/Bkny1v86ex.png) Sau đó cat để lấy password: ![image](https://hackmd.io/_uploads/Hy1QkPL6ex.png) > Password: 0Zf11ioIjMVN551jX3CmStKLYqjk54Ga ## Bandit Level 23 → Level 24 **Level Goal** ```! A program is running automatically at regular intervals from cron, the time-based job scheduler. Look in /etc/cron.d/ for the configuration and see what command is being executed. NOTE: This level requires you to create your own first shell-script. This is a very big step and you should be proud of yourself when you beat this level! NOTE 2: Keep in mind that your shell script is removed once executed, so you may want to keep a copy around… ``` kiểm tra thư mục `/etc/cron.d/` để xem tập tin cấu hình và xác định lệnh nào đang được thực thi. ![image](https://hackmd.io/_uploads/Sy449mD6ll.png) Phân tích ta thấy rằng đoạn script này sẽ cd `/var/spool/bandit24/foo` và thực thi các tệp có dạng . với quyền sở hữu là của bandit23. Ý tưởng khai thác là chúng ta sẽ viết một script để cho bandit24 thực thi và gửi password về cho chúng ta ở thư mục ta chỉ định thuộc `/tmp` ![image](https://hackmd.io/_uploads/rk8U6XDaxe.png)