Dans cet article je vais vous faire une petite démonstration d’extraction d’informations pour un CTF ou un test d’intrusion. Grâce a python2 ainsi que les modules Beautifulsoup et urllib2 , vous allez extraire d’un code source différentes informations comme (liste non exhaustive):
- Les métatags
- Les lien
- Les scripts
Pour l’exemple je vous propose d’utiliser Mutillidae en passant par Metasploitable ou cet environnement d’apprentissage est installé et accessible directement. Pour ma part j’ai installé Metasploitable sur une machine virtuelle VirtualBox, mais vous pouvez également y accéder en passant par les CTF all the day chez Root-Me.
Voici donc le script. L’URL devra être remplacée par celle que vous fournira soit Root-me ou Mutillidaé sur VirtualBox.
- Dans le cas de VirtualBox ce sera une URL de type : http://192.168.1.17/mutillidae/
- Dans le cas de CTF all the day ce sera une url de type: ctfXY.root-me.org
import urllib2 from bs4 import BeautifulSoup url = "http://192.168.1.17/mutillidae/" page = urllib2.urlopen(url) soup = BeautifulSoup(page) #page entiere
Vous pouvez des a présent extraire différentes informations grâce a l’intégration des différentes balises HTML et par exemple vérifier s’il y a des balises « meta » et quel est leur contenu.
meta = soup.find_all('meta') for elements in meta: print (elements) print ("\n\n")
Effectivement il y a un retour.
<meta content="text/html; charset=utf-8" http-equiv="content-type"/>
La présence des différents liens pourrait surement aussi vous intéresser en intégrant la balises « a » et l’attribut « href« .
ancre = soup.find_all("a") for elements in ancre: print elements.get("href")
Cette partie du script retournes effectivement une belle liste.
index.php?page=home.php ./index.php?page=login.php ./index.php?do=toggle-hints&page=home.php ./index.php?do=toggle-security&page=home.php set-up-database.php ./index.php?page=show-log.php ./index.php?page=captured-data.php # index.php?page=home.php ./index.php?page=login.php ./index.php?do=toggle-security&page=home.php set-up-database.php ./index.php?page=show-log.php ./index.php?page=credits.php # http://www.owasp.org/index.php/Top_10_2010-A1 ./index.php?page=user-info.php ./index.php?page=login.php ./index.php?page=register.php ./index.php?page=login.php ./index.php?page=user-info.php ./index.php?page=view-someones-blog.php ./index.php?page=user-info.php ?page=add-to-your-blog.php ./index.php?page=site-footer-xss-discussion.php index.php?page=html5-storage.php index.php?page=capture-data.php ./index.php?page=dns-lookup.php ./index.php ./index.php?page=password-generator.php&username=anonymous ./index.php?page=user-poll.php ./index.php?page=set-background-color.php ./index.php?page=pen-test-tool-lookup.php http://www.owasp.org/index.php/Top_10_2010-A2 ./index.php?page=dns-lookup.php ./index.php?page=pen-test-tool-lookup.php ./index.php?page=text-file-viewer.php ./index.php?page=user-info.php ./index.php?page=set-background-color.php ./index.php?page=html5-storage.php ./index.php?page=capture-data.php ?page=add-to-your-blog.php ?page=view-someones-blog.php ?page=show-log.php index.php?page=html5-storage.php ?page=add-to-your-blog.php ?page=view-someones-blog.php ?page=show-log.php ?page=text-file-viewer.php ./index.php?page=dns-lookup.php ?page=user-info.php ./index.php ./index.php?page=set-background-color.php ./index.php?page=pen-test-tool-lookup.php ./index.php?page=browser-info.php ./index.php?page=show-log.php ./index.php?page=site-footer-xss-discussion.php ./index.php?page=html5-storage.php ./index.php index.php?page=html5-storage.php ./index.php?page=pen-test-tool-lookup.php index.php?page=capture-data.php http://www.owasp.org/index.php/Top_10_2010-A3 index.php ?page=login.php http://www.owasp.org/index.php/Top_10_2010-A4 index.php?page=text-file-viewer.php index.php?page=source-viewer.php index.php?page=credits.php index.php index.php?page=arbitrary-file-inclusion.php http://www.owasp.org/index.php/Top_10_2010-A5 index.php?page=add-to-your-blog.php ./index.php?page=register.php http://www.owasp.org/index.php/Top_10_2010-A6 index.php?page=home.php ./index.php?page=user-info.php http://www.owasp.org/index.php/Top_10_2010-A7 index.php?page=user-info.php index.php?page=html5-storage.php http://www.owasp.org/index.php/Top_10_2010-A8 index.php?page=secret-administrative-pages.php http://en.wikipedia.org/wiki/Robots_exclusion_standard http://www.owasp.org/index.php/Top_10_2010-A9 ?page=login.php ?page=user-info.php http://www.owasp.org/index.php/Top_10_2010-A10 ?page=credits.php # # http://www.owasp.org/index.php/Top_10_2007-A3 ?page=text-file-viewer.php ?page=source-viewer.php http://www.owasp.org/index.php/Top_10_2007-A6 index.php index.php index.php index.php?page=framing.php framer.html index.php?page=html5-storage.php ?page=text-file-viewer.php ?page=show-log.php index.php?page=login.php index.php?page=add-to-your-blog.php index.php?page=html5-storage.php index.php?page=capture-data.php index.php?page=captured-data.php # index.php?page=change-log.htm index.php?page=installation.php /mutillidae/documentation/mutillidae-installation-on-xampp-win7.pdf index.php?page=documentation/vulnerabilities.php index.php?page=documentation/how-to-access-Mutillidae-over-Virtual-Box-network.php # https://www.owasp.org/index.php/Top_Ten http://samurai.inguardians.com/ https://addons.mozilla.org/en-US/firefox/collections/jdruin/pro-web-developer-qa-pack/ http://www.irongeek.com/i.php?page=security/mutillidae-deliberately-vulnerable-php-owasp-top-10 http://www.hackersforcharity.org/ghdb/ https://www.owasp.org https://addons.mozilla.org/en-US/firefox/collections/jdruin/pro-web-developer-qa-pack/ Tweets by webpwnized http://www.youtube.com/user/webpwnized http://www.irongeek.com http://www.irongeek.com/i.php?page=security/mutillidae-deliberately-vulnerable-php-owasp-top-10 ./index.php?page=installation.php ./index.php?page=usage-instructions.php ./index.php?page=php-errors.php ./index.php?page=change-log.htm ./index.php?page=notes.php http://www.backtrack-linux.org/ http://samurai.inguardians.com/ http://www.eclipse.org/pdt/ http://www.php.net/ http://www.quest.com/toad-for-mysql/ http://www.hackersforcharity.org/ http://www.irongeek.com/i.php?page=security/mutillidae-deliberately-vulnerable-php-owasp-top-10 http://irongeek.com >>>
Il pourrait être intéressant de jeter un coup d’œil aux différents scripts présents.
scripts = soup.find_all('script') for script in scripts: print (script) print ("\n")
Et les voici.
<script src="./javascript/bookmark-site.js" type="text/javascript"></script> <script src="./javascript/ddsmoothmenu/ddsmoothmenu.js" type="text/javascript"></script> <script src="./javascript/ddsmoothmenu/jquery.min.js" type="text/javascript"> /*********************************************** * Smooth Navigational Menu- (c) Dynamic Drive DHTML code library (www.dynamicdrive.com) * This notice MUST stay intact for legal use * Visit Dynamic Drive at http://www.dynamicdrive.com/ for full source code ***********************************************/ </script> <script type="text/javascript"> ddsmoothmenu.init({ mainmenuid: "smoothmenu1", //menu DIV id orientation: 'v', //Horizontal or vertical menu: Set to "h" or "v" classname: 'ddsmoothmenu', //class added to menu's outer DIV //customtheme: ["#cccc44", "#cccccc"], contentsource: "markup" //"markup" or ["container_id", "path_to_menu_file"] }); </script> <script type="text/javascript"> var onLoadOfBody = function(theBody){ var l_securityLevel = '0'; var l_hintsStatus = 'Disabled (0 - I try harder)'; switch(l_securityLevel){ case "0": var l_securityLevelDescription = 'Hosed'; break; case "1": var l_securityLevelDescription = 'Arrogent'; break; case "2": var l_securityLevelDescription = '2'; break; case "3": var l_securityLevelDescription = '3'; break; case "4": var l_securityLevelDescription = '4'; break; case "5": var l_securityLevelDescription = 'Secure'; break; }// end switch //document.getElementById("idSystemInformationHeading").innerHTML = l_loginMessage; document.getElementById("idHintsStatusHeading").innerHTML = 'Hints: ' + l_hintsStatus; document.getElementById("idSecurityLevelHeading").innerHTML = 'Security Level: ' + l_securityLevel + ' (' + l_securityLevelDescription + ')'; }// end function onLoadOfBody() </script>
Voici la fin de ce mini tutoriel traitant de l’utilisation de Python, Beautifulsoup et urllib pour extraire des données d’un un code-source. Il est évident que ce n’était qu’une infime partie de ce qu’il est possible de faire. Je vous invite a explorer, tester et bien sur modifier le script a votre guise grâce aux liens qui sont fournis dans cet article.
N’oubliez pas que l’utilisation de ce genre de techniques est limité a une utilisation dans un cadre légal. Pour rappel je vous invite a lire cet article: Rappel des textes en vigueur concernant les “atteintes aux systèmes de traitement automatisé de données”