2016-07-26 3 views
1

ich direkt von dem Violent Python PDF arbeite, Seite 147.Violent Python - Haben Sie Probleme mit optparse die richtigen Informationen zur Ausgabe

Ich bin derzeit die pygeoip Modul mit dem Standort finden von IP-Adressen. Ich war in der Lage, diesen ersten Schritt ziemlich einfach zu machen und es wird durch den # 1-Hash im Code dargestellt.

Der zweite Schritt beinhaltet das Entnehmen von Daten aus einer pcap-Datei und das Abgleichen der entsprechenden IP-Adressen (sowohl Ziel- als auch Ursprungs-IPs) mit ihren Standortdaten. Aus irgendeinem Grund kann ich das Programm nicht dazu bringen, diese Information zurückzugeben. Stattdessen erhalte ich eine gedruckte Zeichenfolge von meiner optParse-Instanz.

meine aktuellen Code ist:

#1. pg 136 of Violent Python by TJ O'Connor 



#We are using the imported pygeoip module to search the database from 
#http://dev.maxmind.com/geoip/legacy/geolite/ and match it with an ip address 

import pygeoip 

GI = pygeoip.GeoIP('/home/cody/workspace/violent_python/opt/GeoIP/GeoLiteCity.dat') 

#output should be the location of the given ip; NOTE: does not work for IPV6 

gi = pygeoip.GeoIP('/home/cody/workspace/violent_python/opt/GeoIP/GeoLiteCity.dat') 
def printRecord(tgt): 
    rec = gi.record_by_name(tgt) 
    city = rec['city'] 
    region = rec['region_code'] 
    country = rec['country_name'] 
    long = rec['longitude'] 
    lat = rec['latitude'] 
    print '[*] Target: ' + tgt + ' Geo-located.' 
    print '[+] ' +str(city)+','+str(lat)+ ',longitude: '+str(long) 
tgt = '173.255.226.98' 
printRecord(tgt) 



#reading a pcap capture; NOTE: it would be useful to learn how to view live 
#traffic via studying pypcap 
import dpkt 
import socket 
def printPcap(pcap): 
    for (ts,buf) in pcap: 
     try: 
      eth = dpkt.ethernet.Ethernet(buf) 
      ip = eth.data 
      src = socket.inet_ntoa(ip.src) 
      dst = socket.inet_ntoa(ip.dst) 
      print '[+] Src: ' + src + ' --> Dst: ' + dst 
     except: 
      pass 

def main(): 
    f = open('geotest.pcap') 
    pcap = dpkt.pcap.Reader(f) 
    printPcap(pcap) 
if __name__ == '__main__': 
    main() 


#create a new function that returns a pyschial location for an IP address 
import dpkt, socket, pygeoip, optparse 

gi = pygeoip.GeoIP("/home/cody/workspace/violent_python/opt/GeoIP/GeoLiteCity.dat") 
def retGeoStr(ip): 
    try: 
     rec = gi.record_by_name(ip) 
     city = rec['city'] 
     country = rec['country_code3'] 
     if (city != ''): 
      geoLoc = city+' , '+country 
     else: 
      geoLoc = country 
     return geoLoc 
    except: 
     return 'Unregistered' 



#2. this is the entire set up put together 

import dpkt,socket,pygeoip,optparse 
gi = pygeoip.GeoIP("/home/cody/workspace/violent_python/opt/GeoIP/GeoLiteCity.dat") 
def retGeoStr(ip): 
    try: 
     rec = gi.record_by_name(ip) 
     city = rec['city'] 
     country = rec['country_code3'] 
     if city != '': 
      geoLoc = city + ',' + country 
     else: 
      geoLoc = country 
     return geoLoc 
    except: 
     return 'Unregistered' 
def printPcap(pcap): 
    for (ts, buf) in pcap: 
     try: 
      eth = dpkt.ethernet.Ethernet(buf) 
      ip = eth.data 
      src = socket.inet_ntoa(ip.src) 
      dst = socket.inet_ntoa(ip.dst) 
      print '[+] Src: ' + src + '----> Dst: ' + dst 
      print '[+] Src: ' +retGeoStr(src) + '----> Dst: ' + retGeoStr(dst) 
     except: 
      pass 

def main(): 
    parser = optparse.OptionParser('usage%prog -p <pcap file>') 
    parser.add_option('-p',dest='pcapFile',type='string',\ 
    help='specify pcap filename') 
(options,args) = parser.parse_args() 
    if options.pcapFile == None: 
     print parser.usage 
     exit(0) 
    pcapFile = options.pcapFile 
    f = open(pcapFile) 
    pcap = dpkt.pcap.Reader(f) 
if __name__ == '__main__': 
    main() 

''' 
Desiered output: 


analyst# python geoPrint.py -p geotest.pcap 
[+] Src: 110.8.88.36 --> Dst: 188.39.7.79 
[+] Src: KOR --> Dst: London, GBR 
[+] Src: 28.38.166.8 --> Dst: 21.133.59.224 
[+] Src: Columbus, USA --> Dst: Columbus, USA 
[+] Src: 153.117.22.211 --> Dst: 138.88.201.132 
[+] Src: Wichita, USA --> Dst: Hollywood, USA 
[+] Src: 1.103.102.104 --> Dst: 5.246.3.148 
[+] Src: KOR --> Dst: Unregistered 
[+] Src: 166.123.95.157 --> Dst: 219.173.149.77 
[+] Src: Washington, USA --> Dst: Kawabe, JPN 
[+] Src: 8.155.194.116 --> Dst: 215.60.119.128 
[+] Src: USA --> Dst: Columbus, USA 
[+] Src: 133.115.139.226 --> Dst: 137.153.2.196 
[+] Src: JPN --> Dst: Tokyo, JPN 
[+] Src: 217.30.118.1 --> Dst: 63.77.163.212 
[+] Src: Edinburgh, GBR --> Dst: USA 
[+] Src: 57.70.59.157 --> Dst: 89.233.181.180 
[+] Src: Endeavour Hills, AUS --> Dst: Prague, CZE 
''' 

#3. we are going to build the kml document to map to google maps 

Meine tatsächliche Ausgabe:

[*] Target: 173.255.226.98 Geo-located. 
[+] Newark,40.7357,longitude: -74.1724 
[+] Src: 110.8.88.36 --> Dst: 188.39.7.79 
[+] Src: 28.38.166.8 --> Dst: 21.133.59.224 
[+] Src: 153.117.22.211 --> Dst: 138.88.201.132 
[+] Src: 1.103.102.104 --> Dst: 5.246.3.148 
[+] Src: 166.123.95.157 --> Dst: 219.173.149.77 
[+] Src: 8.155.194.116 --> Dst: 215.60.119.128 
[+] Src: 133.115.139.226 --> Dst: 137.153.2.196 
[+] Src: 217.30.118.1 --> Dst: 63.77.163.212 
[+] Src: 57.70.59.157 --> Dst: 89.233.181.180 
usage%prog -p <pcap file> 

Bitte helfen Sie mir! Das kann ich nicht herausfinden, aber ich denke, es ist etwas zu tun mit meinem Parser hat

+0

Das ist eine Menge von Code ist die nicht wirklich sollte in einer Datei leben. (Es sind wirklich ein paar separate Programme) Beschränken Sie den Code zunächst nur auf den Teil, den Sie bearbeiten möchten. Dann strippe es auf nur den Teil ab, mit dem du Probleme hast. Dann finden Sie die Lösung oder stellen Sie eine spezifischere Frage erneut. Gerade jetzt haben Sie einen wirklich unordentlichen Code und Sie zeigen nicht einmal, wie er ausgeführt wird, aber Sie fragen nach Problemen mit Parametern. – viraptor

Antwort

1

Wie viraptor sagte, trennen Sie die Übungen in verschiedenen Skriptdateien und dann erneut versuchen. Die Übungen wurden nicht nur in separaten Skripts geschrieben, es ist viel einfacher, einen Fehler zu finden, wenn Sie weniger Code zum Durchschauen haben (und weniger Code, der möglicherweise schief gehen kann).

+0

ok Ich habe versucht, das ein bisschen deutlicher zu machen und darauf hinzuweisen, woran ich festhalte: –