From edd94bc7517aebc0b91b2931bcccfccc65fe1fa4 Mon Sep 17 00:00:00 2001 From: gpotter2 Date: Wed, 8 Feb 2017 11:28:10 +0100 Subject: [PATCH] [Windows] Whois support (#474) * Fix whois on Windows * Better whois server and parsing * Small fixes --- scapy/base_classes.py | 3 ++- scapy/layers/inet.py | 8 +++++++- scapy/utils.py | 39 +++++++++++++++++++++++++++++++++++---- 3 files changed, 44 insertions(+), 6 deletions(-) diff --git a/scapy/base_classes.py b/scapy/base_classes.py index 62997889a..8cd7e19b0 100644 --- a/scapy/base_classes.py +++ b/scapy/base_classes.py @@ -77,7 +77,8 @@ class Net(Gen): self.repr=net self.parsed,self.netmask = self._parse_net(net) - + def __str__(self): + return self.repr def __iter__(self): for d in xrange(*self.parsed[3]): diff --git a/scapy/layers/inet.py b/scapy/layers/inet.py index 54fffc481..a6c4eb394 100644 --- a/scapy/layers/inet.py +++ b/scapy/layers/inet.py @@ -16,6 +16,7 @@ from scapy.base_classes import Gen from scapy.data import * from scapy.layers.l2 import * from scapy.config import conf +from scapy.consts import WINDOWS from scapy.fields import * from scapy.packet import * from scapy.volatile import * @@ -23,6 +24,7 @@ from scapy.sendrecv import sr,sr1,srp1 from scapy.plist import PacketList,SndRcvList from scapy.automaton import Automaton,ATMT from scapy.error import warning +from scapy.utils import whois import scapy.as_resolvers @@ -36,7 +38,11 @@ class IPTools(object): """Add more powers to a class with an "src" attribute.""" __slots__ = [] def whois(self): - os.system("whois %s" % self.src) + """whois the source and print the output""" + if WINDOWS: + print whois(self.src) + else: + os.system("whois %s" % self.src) def ottl(self): t = [32,64,128,255]+[self.ttl] t.sort() diff --git a/scapy/utils.py b/scapy/utils.py index c163cfdc3..34c78dd46 100644 --- a/scapy/utils.py +++ b/scapy/utils.py @@ -7,10 +7,10 @@ General utility functions. """ -import os,sys,socket,types -import random,time -import gzip,zlib,cPickle -import re,struct,array +import os, sys, socket, types +import random, time +import gzip, zlib, cPickle +import re, struct, array import subprocess import tempfile @@ -1275,3 +1275,34 @@ def make_lined_table(*args, **kargs): def make_tex_table(*args, **kargs): __make_table(lambda l: "%s", lambda l: "& %s", "\\\\", seplinefunc=lambda a,x:"\\hline", *args, **kargs) +############################################### +### WHOIS CLIENT (not available on windows) ### +############################################### + +def whois(ip_address): + """Whois client for Python""" + whois_ip = str(ip_address) + try: + query = socket.gethostbyname(whois_ip) + except: + query = whois_ip + s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + s.connect(("whois.ripe.net", 43)) + s.send(query + "\r\n") + answer = '' + while True: + d = s.recv(4096) + answer += d + if not d: + break + s.close() + ignore_tag = "remarks:" + # ignore all lines starting with the ignore_tag + lines = [ line for line in answer.split("\n") if not line or (line and not line.startswith(ignore_tag))] + # remove empty lines at the bottom + for i in range(1, len(lines)): + if not lines[-i].strip(): + del lines[-i] + else: + break + return "\n".join(lines[3:])