{"id":375,"date":"2014-01-07T16:57:06","date_gmt":"2014-01-07T23:57:06","guid":{"rendered":"http:\/\/somethingk.com\/main\/?p=375"},"modified":"2014-01-20T18:04:44","modified_gmt":"2014-01-21T01:04:44","slug":"detecting-new-network-devices-with-python-and-tkinter","status":"publish","type":"post","link":"http:\/\/somethingk.com\/main\/detecting-new-network-devices-with-python-and-tkinter\/","title":{"rendered":"Detecting New Network Devices with Python and Tkinter"},"content":{"rendered":"<p><span style=\"text-decoration: underline;\">UPDATE: I made a better version of this tool with server implementation <a title=\"IMPROVEMENTS: Detecting New Network Devices with Python and Tkinter\" href=\"http:\/\/somethingk.com\/main\/?p=456\">here<\/a>.<\/span><\/p>\n<p>Today I felt like building a python 2.7 script that would enumerate a network along with alert me to the presence of a new device.<\/p>\n<p>I limited my project to functions in the standard library.<\/p>\n<p>So something lightweight and okay fast is a ping sweep. From an early <a title=\"Ping Sweep\" href=\"http:\/\/somethingk.com\/main\/?p=51\">post<\/a> I included the Linux command for a sweep. I used this command along with the python commands to execute the ping sweep along with storing the results in a variable.<\/p>\n<p><code>new = commands.getoutput('for i in {'+MIN+'..'+MAX+'}; do ping -c 1 -t 1 '+PREFIX+'.$i | grep \"from\"; done')<\/code><\/p>\n<p><code><\/code>Following, I used some regular expressions to pull out the IP addresses detected in a given prefix range.<\/p>\n<p><code>tmp = re.findall(PREFIX+\"\\.(\\d+)\", str(new)) #Pull out IP addresses from the ping results<\/code><\/p>\n<p>Put that in a loop with some comparison data and you have a script that prints an alert whenever a new device is detected.<\/p>\n<table border=\"1\">\n<tbody>\n<tr>\n<td><code>import commands, re<br \/>\nPREFIX = \"192.168.1\" #Network prefix<br \/>\nMIN = \"0\" #Starting network address, eg 192.168.1.0<br \/>\nMAX = \"12\" #Closing network address, e.g. 192.168.1.55<br \/>\nresults = []<br \/>\nwhile 1:<br \/>\nnew = commands.getoutput('for i in {'+MIN+'..'+MAX+'}; do ping -c 1 -t 1 '+PREFIX+'.$i | grep \"from\"; done') #Ping sweep the network to find connected devices<br \/>\ntmp = re.findall(PREFIX+\"\\.(\\d+)\", str(new)) #Pull out IP addresses from the ping results<br \/>\nif tmp != results:<br \/>\nfor t in tmp:<br \/>\nif t not in results:<br \/>\nprint \"New device at\" + PREFIX + \".\" + str(t)<br \/>\nresults = tmp<\/code><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>There are a few short comings in the code but that&#8217;s the basic idea.<\/p>\n<p>Now take this further, I hooked it up to a GUI with enumeration information! The new beastly application constantly flips through NMAP scan results of devices found connected to the network and displays the results in a GUI.\u00a0<span style=\"line-height: 1.5em;\">I even placed a picture in the GUI. I call this app, the Hindenburg, its kind of hacked together.<\/span><\/p>\n<figure style=\"width: 300px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/somethingk.com\/main\/wp-content\/uploads\/2014\/01\/Screen-Shot-2014-01-07-at-5.53.39-PM.png\"><img fetchpriority=\"high\" decoding=\"async\" title=\"The Hindenburg!\" alt=\"The Hindenburg!\" src=\"http:\/\/somethingk.com\/main\/wp-content\/uploads\/2014\/01\/Screen-Shot-2014-01-07-at-5.53.39-PM-300x229.png\" width=\"300\" height=\"229\" \/><\/a><figcaption class=\"wp-caption-text\">The Hindenburg!<\/figcaption><\/figure>\n<table border=\"1\">\n<tbody>\n<tr>\n<td><code><code>from Tkinter import *<br \/>\nimport time, commands, re<br \/>\nPREFIX = \"192.168.1\" #Network prefix<br \/>\nMIN = \"0\" #Starting network address, eg 192.168.1.0<br \/>\nMAX = \"12\" #Closing network address, eg 192.168.1.12<br \/>\nclass flipGUI(Tk):<br \/>\ndef __init__(self,*args, **kwargs): #Setup the GUI and make it pretty<br \/>\nTk.__init__(self, *args, **kwargs)<br \/>\nself.label1 = Label(self, width= 65, justify=CENTER, padx=5, pady=5, text=\"Guests\") #Text label<br \/>\nself.label2 = Label(self, text=\"Guests\") #Photo label<br \/>\nself.label2.grid(row=0, column=1, sticky=W+E+N+S, padx=5, pady=5)<br \/>\nself.label1.grid(row=0, column=0)<br \/>\nself.flipping()<\/code><\/code>\u00a0\u00a0\u00a0\u00a0def flipping(self): #Flip through NMAP scans of detected devices<br \/>\nt = self.label1.cget(&#8220;text&#8221;)<br \/>\nt = self.label2.cget(&#8220;image&#8221;)<br \/>\nfound = scanNetwork()<br \/>\nphoto = PhotoImage(file=&#8221;picture.gif&#8221;)<br \/>\nfor f in found[:-1]: #Loop through all but the last item<br \/>\nself.label1.config(text=f)<br \/>\nself.label1.update()<br \/>\nself.label2.config(image=photo)<br \/>\nself.label2.update()<br \/>\ntime.sleep(15)<br \/>\nself.label1.config(text=found[-1]) #the last item doesn&#8217;t require the sleep, it takes enough time to run the scans<br \/>\nself.label1.update()<br \/>\nself.label2.config(image=photo)<br \/>\nself.label2.update()<br \/>\nself.after(1, flipping())<\/p>\n<p>def scanNetwork():<br \/>\nfound = []<br \/>\nnew = commands.getoutput(&#8216;for i in {&#8216;+MIN+&#8217;..&#8217;+MAX+&#8217;}; do ping -c 1 -t 1 &#8216;+PREFIX+&#8217;.$i | grep &#8220;from&#8221;; done&#8217;) #Ping sweep the network to find connected devices<br \/>\ntmp = re.findall(PREFIX+&#8221;\\.(\\d+)&#8221;, str(new)) #Pull out IP addresses from the ping results<br \/>\nfor ip in tmp: #Loop through each found IP<br \/>\nfound.append(commands.getoutput(&#8216;nmap -v -A -Pn &#8216;+PREFIX+&#8217;.&#8217;+ip))<br \/>\nreturn found<\/p>\n<p>app = flipGUI()<br \/>\napp.mainloop()<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>It&#8217;s ideal for an environment where it can just sit on the screen without much of any type of activity going on. If you are enumerating the entire network, there will be a lag&#8230; it happens.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>UPDATE: I made a better version of this tool with server implementation here. Today I felt like building a python 2.7 script that would enumerate a network along with alert me to the presence of a new device. I limited my project to functions in the standard library. So something lightweight and okay fast is a ping sweep. From an [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23,39,93,72,38],"tags":[104,32,18,153,105],"class_list":["post-375","post","type-post","status-publish","format-standard","hentry","category-enumeration","category-fingerprinting","category-networking","category-python","category-vulnerability-scanner","tag-devices","tag-nmap","tag-ping-sweep","tag-python-2-7","tag-tkinter"],"_links":{"self":[{"href":"http:\/\/somethingk.com\/main\/wp-json\/wp\/v2\/posts\/375","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/somethingk.com\/main\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/somethingk.com\/main\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/somethingk.com\/main\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/somethingk.com\/main\/wp-json\/wp\/v2\/comments?post=375"}],"version-history":[{"count":18,"href":"http:\/\/somethingk.com\/main\/wp-json\/wp\/v2\/posts\/375\/revisions"}],"predecessor-version":[{"id":642,"href":"http:\/\/somethingk.com\/main\/wp-json\/wp\/v2\/posts\/375\/revisions\/642"}],"wp:attachment":[{"href":"http:\/\/somethingk.com\/main\/wp-json\/wp\/v2\/media?parent=375"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/somethingk.com\/main\/wp-json\/wp\/v2\/categories?post=375"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/somethingk.com\/main\/wp-json\/wp\/v2\/tags?post=375"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}