.. _ownlinux: 4. Own one of the Linux based machines on Htb ===================================================== Introduction ^^^^^^^^^^^^^^ | HackTheBox is a platform used by red-team pentesters to hone their skills and generally have fun practicing their craft. They offer a virtual platform, in which there are various boxes or machines varying from difficulty. To be able to start these challenges though, one has to hack their way into the website first. I've already done a fair share of HackTheBox machines, thus I decided to choose a box rated as Hard for my HtB Linux write-up. .. image:: https://i.imgur.com/HRZ69OJ.png Lets get right into it ^^^^^^^^^^^^^^^^^^^^^^^^^ Reconnaisance ^^^^^^^^^^^^^^ 1. Nmap 10.10.11.110 .. image:: https://i.imgur.com/q0McK8T.png 2. Start VNC 3. Open browser + burp (disable proxy) 4. Add earlyaccess.htb to etc hosts 5. Open webpage 6. Run dirb & ffuf in the background | FFuf output .. image:: https://i.imgur.com/DBuo4s1.png 7. See register & login button 8. Create account test test@test testtest 9. Login 10. Browse contents of 'gamestore' webshop 11. Key-format: AAAAA-BBBBB-CCCC1-DDDDD-1234 12. Many OWASP attempts (in messaging) Exploitation ^^^^^^^^^^^^^ 13. Username works for XSS 14. Its read out when sending message to admin 15. There's a specific '``earlyaccess_session``' cookie token, in normal boxes if cookies arent the objective there's *usually* only a phpsessid 16. Using the built-in PHP engine to host a cookie stealer .. code-block:: bash $ php -S 0.0.0.0:8082 .. code-block:: python 17. Update name with XSS included .. code-block:: html ' 18. Send a message via contact us to admin, takes around 30 seconds for admin to read it 19. After 30 seconds got an Unsupported Request SSL error in my built in PHP server 20. Googling for a while and decided to just put up Apache with SSL on my Kali machine, if that didn't work I would have quit this machine .. code-block:: html ' 21. Tailed the log.txt (see above block) and saw the Cookie being injected into the file 22. Injected the cookies into the browser 23. Success! .. image:: https://i.imgur.com/FhHnLv6.png Recon.. again.. ^^^^^^^^^^^^^^^^^ 24. The most interesting parts of the admin panel are a page to download python code related to activating game keys | The code is described as being used as a back up if the API does not work. Back to exploitation ^^^^^^^^^^^^^^^^^^^^^^ | Here's the code found in validate.py. You can check your key using the file. .. code-block:: bash $ python3 validate.py (key) .. code-block:: python #!/usr/bin/env python3 import sys from re import match class Key: key = "" magic_value = "XP" # Static (same on API) magic_num = 346 # TODO: Sync with API (api generates magic_num every 30min) def __init__(self, key:str, magic_num:int=346): self.key = key if magic_num != 0: self.magic_num = magic_num @staticmethod def info() -> str: return f""" # Game-Key validator # Can be used to quickly verify a user's game key, when the API is down (again). Keys look like the following: AAAAA-BBBBB-CCCC1-DDDDD-1234 Usage: {sys.argv[0]} """ def valid_format(self) -> bool: return bool(match(r"^[A-Z0-9]{5}(-[A-Z0-9]{5})(-[A-Z]{4}[0-9])(-[A-Z0-9]{5})(-[0-9]{1,5})$", self.key)) def calc_cs(self) -> int: gs = self.key.split('-')[:-1] return sum([sum(bytearray(g.encode())) for g in gs]) def g1_valid(self) -> bool: g1 = self.key.split('-')[0] r = [(ord(v)< bool: g2 = self.key.split('-')[1] p1 = g2[::2] p2 = g2[1::2] return sum(bytearray(p1.encode())) == sum(bytearray(p2.encode())) def g3_valid(self) -> bool: # TODO: Add mechanism to sync magic_num with API g3 = self.key.split('-')[2] if g3[0:2] == self.magic_value: return sum(bytearray(g3.encode())) == self.magic_num else: return False def g4_valid(self) -> bool: return [ord(i)^ord(g) for g, i in zip(self.key.split('-')[0], self.key.split('-')[3])] == [12, 4, 20, 117, 0] def cs_valid(self) -> bool: cs = int(self.key.split('-')[-1]) return self.calc_cs() == cs def check(self) -> bool: if not self.valid_format(): print('Key format invalid!') return False if not self.g1_valid(): print('g1') return False if not self.g2_valid(): print('g2') return False if not self.g3_valid(): print('g3') return False if not self.g4_valid(): print('g4') return False if not self.cs_valid(): print('[Critical] Checksum verification failed!') return False return True if __name__ == "__main__": if len(sys.argv) != 2: print(Key.info()) sys.exit(-1) input = sys.argv[1] validator = Key(input) if validator.check(): print(f"Entered key is valid!") else: print(f"Entered key is invalid!") | I'm not very familiar with Python, thought with my experience as a software engineer it isn't very tricky for me to try and reverse engineer this machine. I held a discussion with my friend who knew more about Reverse Engineering, where I posed him the following question: - Is it possible to easily reverse engineer this Python code? (and I gave him an example listed below) .. code-block:: python r = [(ord(v)<