From 0a80b69db56d11a5dccfa7e62d490060acc9bc33 Mon Sep 17 00:00:00 2001 From: Cyril Jaquier Date: Tue, 8 Mar 2005 21:11:07 +0000 Subject: [PATCH] =?UTF-8?q?-=20Removed=20the=20"host"=20command=20dependen?= =?UTF-8?q?cy.=20Used=20Python=20socket=20module=20instead.=20Thanks=20to?= =?UTF-8?q?=20K=EF=BF=BDvin=20Drapel?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit git-svn-id: https://fail2ban.svn.sourceforge.net/svnroot/fail2ban/trunk@89 a942ae1a-1317-0410-a47c-b1dcaea8d605 --- utils/dns.py | 35 +++++++++++------------------------ 1 file changed, 11 insertions(+), 24 deletions(-) diff --git a/utils/dns.py b/utils/dns.py index 91b1fdca..7b21daa5 100644 --- a/utils/dns.py +++ b/utils/dns.py @@ -24,33 +24,16 @@ __date__ = "$Date$" __copyright__ = "Copyright (c) 2004 Cyril Jaquier" __license__ = "GPL" -import os, re +import os, re, socket def dnsToIp(dns): - """ Convert a DNS into an IP address using the "host" command. - We make sure that there is no malicious usage of this function. + """ Convert a DNS into an IP address using the Python socket module. + Thanks to Kévin Drapel. """ - ipList = list() - # Check for command injection - if checkInjection(dns): - return ipList - result = os.popen("host "+dns, 'r') - for i in result.readlines(): - match = re.search("(?:\d{1,3}\.){3}\d{1,3}", i) - if match: - ipList.append(match.group()) - return ipList - -def checkInjection(command): - """ Check that command could not be used to inject shell commands. - """ - # Characters which have nothing to do in "command" - invalid = "<|>|;|\&|\||`|!" - match = re.search(invalid, command) - if match: - return True - else: - return False + try: + return socket.gethostbyname_ex(dns)[2] + except socket.gaierror: + return list() def textToDns(text): """ Search for possible DNS in an arbitrary text. @@ -90,4 +73,8 @@ def textToIp(text): if __name__ == "__main__": print textToIp("jlkjlk 123.456.789.000 jlkjl rhost=lslpc49.epfl.ch www.google.ch") + try: + print socket.gethostbyname_ex("www.google")[2] + except socket.gaierror: + print "Error" \ No newline at end of file