{"id":12947,"date":"2022-01-31T09:56:51","date_gmt":"2022-01-31T08:56:51","guid":{"rendered":"https:\/\/inlab.fib.upc.edu\/uncategorized-ca\/pentesting-what-it-and-what-its-procedure-0\/2022\/"},"modified":"2023-05-24T09:17:28","modified_gmt":"2023-05-24T08:17:28","slug":"pentesting-what-it-and-what-its-procedure-0","status":"publish","type":"post","link":"https:\/\/inlab.fib.upc.edu\/en\/news\/pentesting-what-it-and-what-its-procedure-0","title":{"rendered":"Pentesting \u2013 What is it and what its procedure is"},"content":{"rendered":"<p class=\"rtejustify\">In order to mitigate possible failures in different network environments or systems, in recent years, companies have been forced to periodically test their own infrastructures to correct anomalies and holes that endanger the integrity, availability and confidentiality of their assets. This set of tests is what is called <em>penetration testing<\/em>.<\/p>\n<p class=\"rtejustify\">The philosophy of <em>pentesting<\/em> is based on identifying vulnerabilities and weak points in a system before third-party entities with malicious intention do so.<\/p>\n<p class=\"rtejustify\">But how do you do a <em>pentest<\/em>? This question does not have an answer applicable to all cases, since each pentest works in a different environment (infrastructure, software&#8230;). However, we can extract an applicable methodology in most <em>pentests<\/em>.<\/p>\n<p class=\"rtejustify\">This methodology is explained below with an example based on a server owned by HackTheBox, an organization that designs very realistic machines, with the aim of training people in the field of cybersecurity. It should be noted that at no time has it been intended to make a guide on how to complete this machine, certain important steps have been omitted to focus on the general process of the <em>pentest<\/em>.<\/p>\n<h2>Pre-Engagement Interactions&nbsp;<\/h2>\n<p class=\"rtejustify\">In this first phase of a <em>pentest<\/em>, the client indicates the objectives and the attack area. Depending on the information received from the client, it will be a black box <em>pentest&nbsp;<\/em>(if nothing is known about the system to be attacked), a white box <em>pentest<\/em> (if everything is known about the system to be attacked) or a gray box <em>pentest<\/em> (if only some data about the system to be attacked is known).<\/p>\n<h2>Information Gathering<\/h2>\n<p class=\"rtejustify\">Process that is based on obtaining as much information as possible in order to generate an accurate profile of the target.<\/p>\n<p class=\"rtejustify\">Below are examples of one of the most used tools during this phase: Nmap, which is a port scanner with many features.<\/p>\n<pre class=\"rtejustify\">\r\nsudo nmap -p- &lt;IP&gt; <span style=\"font-family: sans-serif, Arial, Verdana, &quot;Trebuchet MS&quot;;\">command that lists the ports open via TCP on a system.<\/span><\/pre>\n<p class=\"rtecenter\"><img fetchpriority=\"high\" decoding=\"async\" class=\" size-full wp-image-12920\" alt=\"\" src=\"https:\/\/inlab.fib.upc.edu\/wp-content\/uploads\/2022\/01\/pentest-1.png\" style=\"width: 887px; height: 310px;\" width=\"887\" height=\"310\" srcset=\"https:\/\/inlab.fib.upc.edu\/wp-content\/uploads\/2022\/01\/pentest-1.png 887w, https:\/\/inlab.fib.upc.edu\/wp-content\/uploads\/2022\/01\/pentest-1-300x105.png 300w, https:\/\/inlab.fib.upc.edu\/wp-content\/uploads\/2022\/01\/pentest-1-768x268.png 768w\" sizes=\"(max-width: 887px) 100vw, 887px\" \/><\/p>\n<p class=\"rtejustify\">Ports 22 and 80 are shown to be open via TCP. It also tells us which service usually goes to each port.<\/p>\n<p>Here is an example to scan the open ports of the system that use the UDP protocol.<\/p>\n<pre>\r\nsudo nmap -sU -oN nmap\/udp.nmap &lt;IP&gt;\u200b<\/pre>\n<p class=\"rtecenter\"><img decoding=\"async\" class=\" size-full wp-image-12923\" alt=\"\" src=\"https:\/\/inlab.fib.upc.edu\/wp-content\/uploads\/2022\/01\/pentest-2.png\" style=\"width: 887px; height: 200px;\" width=\"887\" height=\"200\" srcset=\"https:\/\/inlab.fib.upc.edu\/wp-content\/uploads\/2022\/01\/pentest-2.png 887w, https:\/\/inlab.fib.upc.edu\/wp-content\/uploads\/2022\/01\/pentest-2-300x68.png 300w, https:\/\/inlab.fib.upc.edu\/wp-content\/uploads\/2022\/01\/pentest-2-768x173.png 768w\" sizes=\"(max-width: 887px) 100vw, 887px\" \/><\/p>\n<p class=\"rtejustify\">We can see that ports 161 and 3389 are open, interestingly port 161 handles SNMP.<\/p>\n<p>In this case, the investigation of port 161 is prioritized, a critical port that should not be accessible from the outside. Due to a misconfiguration, some credentials can be obtained that allow us to know that the server uses a local version of Pandora on port 80, which can be accessed by redirecting the ports.<\/p>\n<p class=\"rtecenter\"><img decoding=\"async\" class=\" size-full wp-image-12926\" alt=\"\" src=\"https:\/\/inlab.fib.upc.edu\/wp-content\/uploads\/2022\/01\/pentest-3.png\" style=\"width: 887px; height: 400px;\" width=\"887\" height=\"400\" srcset=\"https:\/\/inlab.fib.upc.edu\/wp-content\/uploads\/2022\/01\/pentest-3.png 887w, https:\/\/inlab.fib.upc.edu\/wp-content\/uploads\/2022\/01\/pentest-3-300x135.png 300w, https:\/\/inlab.fib.upc.edu\/wp-content\/uploads\/2022\/01\/pentest-3-768x346.png 768w\" sizes=\"(max-width: 887px) 100vw, 887px\" \/><\/p>\n<p class=\"rtejustify\">At the bottom of the page you can see that the version of Pandora is v7.0NG.742_FIX_PERL2020, a version with known vulnerabilities<\/p>\n<h2>Exploiting<\/h2>\n<p class=\"rtejustify\">This phase is based on exploiting the vulnerabilities previously found and, if more are found, and seeing how far it can go.<\/p>\n<p>Within the known vulnerabilities of Pandora version v7.0NG.742_FIX_PERL2020, there is a SQL injection, documented in Mitre (CVE-2021-32099).<\/p>\n<p>An SQL injection is a vulnerability that is based on injecting SQL code in a given field to make arbitrary calls to a database. This is because the data that a user enters into a given field is not checked correctly.<\/p>\n<p>Thus, we obtain the necessary form credentials by sending a POST request to the location mentioned in the CVE description with a malicious session_id parameter, which allows us to bypass server authentication:<\/p>\n<pre class=\"rtejustify\">\r\nsession_id=666' UNION SELECT 1,2,data FROM tsessions_php WHERE data LIKE '%user%' -- xxx \u200b<\/pre>\n<p class=\"rtecenter\"><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-12929\" alt=\"\" src=\"https:\/\/inlab.fib.upc.edu\/wp-content\/uploads\/2022\/01\/pentest-4.png\" style=\"width: 885px; height: 370px;\" width=\"885\" height=\"370\" srcset=\"https:\/\/inlab.fib.upc.edu\/wp-content\/uploads\/2022\/01\/pentest-4.png 885w, https:\/\/inlab.fib.upc.edu\/wp-content\/uploads\/2022\/01\/pentest-4-300x125.png 300w, https:\/\/inlab.fib.upc.edu\/wp-content\/uploads\/2022\/01\/pentest-4-768x321.png 768w\" sizes=\"(max-width: 885px) 100vw, 885px\" \/><\/p>\n<p class=\"rtejustify\">Once inside the interface, we will exploit another vulnerability, also documented in Mitre (CVE-2020-13851), an RCE (<em>Remote Code Execution<\/em>), which allows commands to be executed remotely. This vulnerability is used to generate a Reverse Shell and gain access to the system as a low level user.<\/p>\n<p class=\"rtecenter\"><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-12932\" alt=\"\" src=\"https:\/\/inlab.fib.upc.edu\/wp-content\/uploads\/2022\/01\/pentest-5.png\" style=\"width: 887px; height: 414px;\" width=\"887\" height=\"414\" srcset=\"https:\/\/inlab.fib.upc.edu\/wp-content\/uploads\/2022\/01\/pentest-5.png 887w, https:\/\/inlab.fib.upc.edu\/wp-content\/uploads\/2022\/01\/pentest-5-300x140.png 300w, https:\/\/inlab.fib.upc.edu\/wp-content\/uploads\/2022\/01\/pentest-5-768x358.png 768w\" sizes=\"(max-width: 887px) 100vw, 887px\" \/><\/p>\n<p class=\"rtecenter\"><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-12935\" alt=\"\" src=\"https:\/\/inlab.fib.upc.edu\/wp-content\/uploads\/2022\/01\/pentest-6.png\" style=\"width: 887px; height: 227px;\" width=\"887\" height=\"227\" srcset=\"https:\/\/inlab.fib.upc.edu\/wp-content\/uploads\/2022\/01\/pentest-6.png 887w, https:\/\/inlab.fib.upc.edu\/wp-content\/uploads\/2022\/01\/pentest-6-300x77.png 300w, https:\/\/inlab.fib.upc.edu\/wp-content\/uploads\/2022\/01\/pentest-6-768x197.png 768w\" sizes=\"(max-width: 887px) 100vw, 887px\" \/><\/p>\n<p class=\"rtejustify\">Once the system has been accessed, through an enumeration script called linPEAS, the following can be observed:<\/p>\n<p class=\"rtecenter\"><span style=\"text-align: justify;\"><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-12938\" alt=\"\" src=\"https:\/\/inlab.fib.upc.edu\/wp-content\/uploads\/2022\/01\/pentest-7.png\" style=\"width: 885px; height: 181px;\" width=\"885\" height=\"181\" srcset=\"https:\/\/inlab.fib.upc.edu\/wp-content\/uploads\/2022\/01\/pentest-7.png 885w, https:\/\/inlab.fib.upc.edu\/wp-content\/uploads\/2022\/01\/pentest-7-300x61.png 300w, https:\/\/inlab.fib.upc.edu\/wp-content\/uploads\/2022\/01\/pentest-7-768x157.png 768w\" sizes=\"(max-width: 885px) 100vw, 885px\" \/><\/span><\/p>\n<p class=\"rtejustify\">In the last line you can see very interesting information, a file that has SUID permissions, and that can also be executed by the user that we have obtained.<\/p>\n<p><span style=\"text-align: justify;\">A binary with SUID permissions can change its privileges to those of the owner of the file. Therefore, if we execute it with the user that we have obtained previously, the binary will be able to obtain administrator permissions, since root is the owner of the file.<\/span><\/p>\n<p><span style=\"text-align: justify;\">Examining the system calls it executes, you can see that the tar binary is called without the absolute path. This means that if you change the $PATH environment variable and create a file called \u201ctar\u201d, you will be able to run this file with administrator privileges.<\/span><\/p>\n<p><span style=\"text-align: justify;\">The following commands do the same thing and run the binary:<\/span><\/p>\n<pre>\r\n<span style=\"text-align: justify;\">export PATH=.:$PATH<\/span>\r\n\r\n<span style=\"text-align: justify;\">echo '\/bin\/bash' &gt; tar<\/span>\r\n\r\n<span style=\"text-align: justify;\">chmod +x tar<\/span>\r\n\r\n<span style=\"text-align: justify;\">\/usr\/bin\/pandora_backup<\/span><\/pre>\n<p class=\"rtecenter\"><span style=\"text-align: justify;\"><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-12941\" alt=\"\" src=\"https:\/\/inlab.fib.upc.edu\/wp-content\/uploads\/2022\/01\/pentest-8.png\" style=\"width: 887px; height: 212px;\" width=\"887\" height=\"212\" srcset=\"https:\/\/inlab.fib.upc.edu\/wp-content\/uploads\/2022\/01\/pentest-8.png 887w, https:\/\/inlab.fib.upc.edu\/wp-content\/uploads\/2022\/01\/pentest-8-300x72.png 300w, https:\/\/inlab.fib.upc.edu\/wp-content\/uploads\/2022\/01\/pentest-8-768x184.png 768w\" sizes=\"(max-width: 887px) 100vw, 887px\" \/><\/span><\/p>\n<p class=\"rtejustify\">This is an example of privilege escalation, gaining administrator permissions and thus full control of the server.<\/p>\n<p><span style=\"text-align: justify;\">Finally, it is necessary to clarify that a <em>pentest<\/em> is not based solely on gaining access to a machine, but rather consists of trying to find all the vulnerabilities or misconfigurations that can jeopardize the security and integrity of a service.<\/span><\/p>\n<h2 class=\"rtejustify\"><span style=\"text-align: justify;\">Reporting<\/span><\/h2>\n<p class=\"rtejustify\">This final phase is based on grouping everything that has been found and documented in order to explain everything clearly and firmly. The final document must have a compilation of all the vulnerabilities found, the respective mitigations and the way in which the first ones were found. In this way, when the developers have to fix the vulnerabilities, they will be able to replicate them to ensure that they have been resolved correctly.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In order to mitigate possible failures in different network environments or systems, in recent years, companies have been forced to periodically test their own infrastructures to correct anomalies and holes that endanger the integrity, availability and confidentiality of their assets. This set of tests is what is called penetration testing. The philosophy of pentesting is [&hellip;]<\/p>\n","protected":false},"author":1211,"featured_media":12917,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[571],"tags":[],"experteses":[],"class_list":["post-12947","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-news"],"acf":[],"_links":{"self":[{"href":"https:\/\/inlab.fib.upc.edu\/en\/wp-json\/wp\/v2\/posts\/12947","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/inlab.fib.upc.edu\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/inlab.fib.upc.edu\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/inlab.fib.upc.edu\/en\/wp-json\/wp\/v2\/users\/1211"}],"replies":[{"embeddable":true,"href":"https:\/\/inlab.fib.upc.edu\/en\/wp-json\/wp\/v2\/comments?post=12947"}],"version-history":[{"count":1,"href":"https:\/\/inlab.fib.upc.edu\/en\/wp-json\/wp\/v2\/posts\/12947\/revisions"}],"predecessor-version":[{"id":20817,"href":"https:\/\/inlab.fib.upc.edu\/en\/wp-json\/wp\/v2\/posts\/12947\/revisions\/20817"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/inlab.fib.upc.edu\/en\/wp-json\/wp\/v2\/media\/12917"}],"wp:attachment":[{"href":"https:\/\/inlab.fib.upc.edu\/en\/wp-json\/wp\/v2\/media?parent=12947"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/inlab.fib.upc.edu\/en\/wp-json\/wp\/v2\/categories?post=12947"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/inlab.fib.upc.edu\/en\/wp-json\/wp\/v2\/tags?post=12947"},{"taxonomy":"experteses","embeddable":true,"href":"https:\/\/inlab.fib.upc.edu\/en\/wp-json\/wp\/v2\/experteses?post=12947"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}