%%html %%javascript IPython.OutputArea.auto_scroll_threshold = -1; %matplotlib inline import math import matplotlib.pyplot as plt from mpl_toolkits.basemap import Basemap import numpy as np from scipy import stats # figuresize = 20 plt.rcParams['figure.figsize'] = (1+math.sqrt(5))/2*figuresize, figuresize plt.rcParams['figure.dpi'] = 200 plt.rcParams['font.size'] = 20 plt.rcParams['font.family'] = "serif" plt.rcParams['text.usetex'] = "True" # land_color = 'gray' water_color = 'white' import geoip2.database reader = geoip2.database.Reader('./geolocate_ip_data/GeoLite2-City.mmdb') with open('./geolocate_ip_data/hacker_ips') as f: IPs = f.read().splitlines() x = [] y = [] missing = 0 for IP in IPs: try: response = reader.city(IP) x.append(float(response.location.longitude)) y.append(float(response.location.latitude)) except: print("IP address not found.") missing += 1 found = len(x) - missing # Set map axis limits. map_min_lat = -70 map_max_lat = 80 map_min_lon = -180 map_max_lon = 180 # Create map axes & base map. fig, ax = plt.subplots() map = Basemap(projection='merc', llcrnrlat=map_min_lat, urcrnrlat=map_max_lat, llcrnrlon=map_min_lon, urcrnrlon=map_max_lon, resolution='l') # Remap x and y coordinates into the map space projection. xm,ym = map(x,y) xmin, ymin = map(map_min_lon, map_min_lat) xmax, ymax = map(map_max_lon, map_max_lat) # Perform kernel density estimation calculation. X, Y = np.mgrid[xmin:xmax:500j, ymin:ymax:500j] positions = np.vstack([X.ravel(), Y.ravel()]) values = np.vstack([xm, ym]) kernel = stats.gaussian_kde(values) Z = np.reshape(kernel(positions).T, X.shape) # Create plots. map.imshow(np.flipud(np.rot90(Z)), cmap=plt.cm.Reds, extent=[xmin, xmax, ymin, ymax], alpha=0.6) map.fillcontinents(color=land_color, lake_color=water_color,zorder=0) map.plot(xm, ym,'k.',markersize=5) plt.title(r"Point \& Relative Density Plot of " + str(found) + r" SSH Penetration Attempts") fig = plt.gcf() plt.show() fig.savefig("test.png", bbox_inches=None, pad_inches=0.1) reader.close() import geoip2.database reader = geoip2.database.Reader('./geolocate_ip_data/GeoLite2-City.mmdb') x = [] y = [] missing = 0 found = 0 for IP in IPs: try: response = reader.city(IP) if(response.country.name == 'Canada'): x.append(float(response.location.longitude)) y.append(float(response.location.latitude)) found = found + 1 except: print("IP address not found.") missing += 1 print(found) # Set map axis limits. map_min_lat = 40 map_max_lat = 83 map_min_lon = -150 map_max_lon = -50 # Create map axes & base map. fig, ax = plt.subplots() map = Basemap(projection='merc', llcrnrlat=map_min_lat, urcrnrlat=map_max_lat, llcrnrlon=map_min_lon, urcrnrlon=map_max_lon, resolution='l') # Remap x and y coordinates into the map space projection. xm,ym = map(x,y) xmin, ymin = map(map_min_lon, map_min_lat) xmax, ymax = map(map_max_lon, map_max_lat) # Perform kernel density estimation calculation. X, Y = np.mgrid[xmin:xmax:500j, ymin:ymax:500j] positions = np.vstack([X.ravel(), Y.ravel()]) values = np.vstack([xm, ym]) kernel = stats.gaussian_kde(values) Z = np.reshape(kernel(positions).T, X.shape) # Create plots. map.imshow(np.flipud(np.rot90(Z)), cmap=plt.cm.Reds, extent=[xmin, xmax, ymin, ymax], alpha=0.6) map.fillcontinents(color=land_color, lake_color=water_color,zorder=0) map.plot(xm, ym,'k.',markersize=5) # Create label on point of interest. for i, xmm in enumerate(xm): if(y[i] > 55): plt.text(xmm, ym[i], str(x[i]) + ", " + str(y[i])) plt.title(r"Point \& Relative Density Plot of " + str(found) + r" SSH Penetration Attempts") fig = plt.gcf() plt.show() fig.savefig("test2.png", bbox_inches=None, pad_inches=0.1) reader.close()