-->

  • TryHackMe Server side template injection






    [Task 1] Introduction



    There are quite a lot of vulnerabilities that affect web applications. This leads to the problem of having quite a bit of material to cover, so it's fair to only cover the bigger/more common vulnerabilities in lieu of the smaller/less common vulnerabilities. This room is dedicated to some of the smaller web vulnerabilities, that may not be large enough to get a room on this site but are still worth knowing about. TryHackMe Server side template injection


    The vulnerabilities that will be discussed are:

    SSTI


    Task 2] Methodology



    This room will be divided into sections, each section talking about a specific vulnerability. The sections will follow this format: an introduction on what the vulnerable thing is, what the vulnerability(s) is/are(their may be multiple tasks on this), a guided exploitation in which I show pictures of how it's exploited, and finally a virtual machine where you will be asked to exploit it and collect a flag.

    Since this room is divided into sections that don't follow one another, you can do this room in any order you please.


    [Task 3] [Section 1 - SSTI] - What is SSTI



    A template engine allows developers to use static HTML pages with dynamic elements. Take for instance a static profile.html page, a template engine would allow a developer to set a username parameter, that would always be set  to the current user's username


    Server Side Template Injection, is when a user is able to pass in a parameter that can control the template engine that is running on the server.


    For example take the code





















    [Task 4] [Section 1 - SSTI] - Manual exploitation of SSTI.



    Turning the code earlier into a full flask application, gives us this page. It takes a prompt for a name, and then returns Hello <name>!.










    Fortunately, we don't have to do much recon as we already know this is vulnerable to SSTI, lets try injecting some basic template code







    Boom! That's template injection. We can use the wonderful repository PayloadsAllTheThings, to find some payloads for flask's template engine. The repo says we can use the code


    {{ ''.__class__.__mro__[2].__subclasses__()[40]()(<file>).read()}}  to read files on the server. Effectively all that payload does is load the file object in python, from there we can use basic file operations. Let's try to read /etc/passwd using this method














    We have LFI! Unfortunately(or fortunately depending on how you view it), that is not the extent of this vulnerability. The same repo, also includes a payload for remote code execution.


    We can use the code


    {{config.__class__.__init__.__globals__['os'].popen(<command>).read()}} to execute commands on the server. All that payload does is import the os module, and run a command using the popen method.









    From there, an attacker has already won, they can use this ability to gain a shell on the server.



    #1 How would a hacker(you :) ) cat out /etc/passwd on the server(using cat with the rce payload)
     
    Ans :-  {{ ''.__class__.__mro__[2].__subclasses__()[40]()(<file>).read()}}





    #2 What about reading in the contents of the user test's private ssh key.(use the read file one not the rce one)

    Ans :- {{ ''.__class__.__mro__[2].__subclasses__()[40]()(/home/test/.ssh/id_rsa).read()}}






    [Task 5] [Section 1 - SSTI]: Automatic Exploitation of SSTI



    Fortunately, we don't have to go searching for payloads to see how we can use SSTI to our advantage, because there is a tool known as Tplmap that does that for us! The tool can be found here.


    Note: use python2 to install the requirements. python2 -m pip



    The basic syntax for tplmap is different depending on whether you're using get or post



    • GET    tplmap -u <url>/?<vulnparam>
    • POST    tplmap -u <url> -d '<vulnparam>'



    Since our code operates via a form, the post syntax will be used.







    From there we can effectively do everything we did in the manual exploitation task, from a command line. Let's try running id using tplmap.






     #1 How would I cat out /etc/passwd using tplmap on the ip:port combo 10.10.10.10:5000, with the vulnerable param "noot".


    Ans :- tplmap -u http://10.10.10.10.5000/ -d 'noot' --os-cmd ' cat /etc/passwd'






    [Task 6][Section 1 - SSTI]: Challenge

     
    I've created a vulnerable machine for you to test your SSTI skills on! I've placed a flag in /flag aswell, good luck and have fun!


    # What is the flag?

    Hint - ./tplmap.py -u http://10.10.314.20/ -d 'name' --os-shell

    Ans :- cooctus







    Disclaimer



    This was written for educational purpose and pentest only.
    The author will not be responsible for any damage ..!
    The author of this tool is not responsible for any misuse of the information.
    You will not misuse the information to gain unauthorized access.
    This information shall only be used to expand knowledge and not for causing  malicious or damaging attacks. Performing any hacks without written permission is illegal ..!


    All video’s and tutorials are for informational and educational purposes only. We believe that ethical hacking, information security and cyber security should be familiar subjects to anyone using digital information and computers. We believe that it is impossible to defend yourself from hackers without knowing how hacking is done. The tutorials and videos provided on www.hackingtruth.in is only for those who are interested to learn about Ethical Hacking, Security, Penetration Testing and malware analysis. Hacking tutorials is against misuse of the information and we strongly suggest against it. Please regard the word hacking as ethical hacking or penetration testing every time this word is used.


    All tutorials and videos have been made using our own routers, servers, websites and other resources, they do not contain any illegal activity. We do not promote, encourage, support or excite any illegal activity or hacking without written permission in general. We want to raise security awareness and inform our readers on how to prevent themselves from being a victim of hackers. If you plan to use the information for illegal purposes, please leave this website now. We cannot be held responsible for any misuse of the given information.



    - Hacking Truth by Kumar Atul Jaiswal



    I hope you liked this post, then you should not forget to share this post at all.
    Thank you so much :-)



  • 0 comments:

    Post a Comment

    For Any Tech Updates, Hacking News, Internet, Computer, Technology and related to IT Field Articles Follow Our Blog.