Advertisement
NetPwn

Kapy Tutorial 16 (Port Scan - 1 Un threaded)

Apr 28th, 2015
676
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 16.42 KB | None | 0 0
  1. from kivy.uix.boxlayout import BoxLayout
  2. from kivy.uix.label import Label
  3. from kivy.uix.button import Button
  4. from kivy.uix.textinput import TextInput
  5. from kivy.lang import Builder
  6. from kivy.app import App
  7. from kivy.uix.image import Image
  8. from scapy.all import *
  9. from kivy.uix.scrollview import ScrollView
  10. from kivy.uix.screenmanager import ScreenManager, Screen
  11. from kivy.uix.switch import Switch #TUT 7
  12. from kivy.uix.popup import Popup
  13. from kivy.clock import Clock
  14. from subprocess import check_output
  15. import threading
  16. import socket
  17. import re
  18.  
  19.  
  20. Builder.load_string('''
  21. <MITMs1>:
  22.    BoxLayout:
  23.        canvas:
  24.            Rectangle:
  25.                source: 'images/kapy-bg3.jpg'
  26.                size: self.size
  27.                pos: self.pos
  28.        BoxLayout:
  29.            id: client_box
  30.            orientation: 'vertical'
  31.            size_hint: 0.5, 1
  32.            spacing: 10
  33.            Image:
  34.                ## Tut 7
  35.                source: 'zips/pyspin.zip'
  36.                anim_delay: 1/80
  37.                allow_stretch: False
  38.                pos_hint: {'x': -.3}
  39.            Label:
  40.                id: device_lbl
  41.                markup: True
  42.                size_hint: 1, 1
  43.            BoxLayout:
  44.                TextInput:
  45.                    id: iface_name
  46.                    size_hint: .3, 0.2
  47.                    text: 'wlan2'
  48.                    multiline: False
  49.                Switch:
  50.                    size_hint: .3, .1
  51.            BoxLayout:
  52.                Button:
  53.                    size_hint: .5, .4
  54.                    text: '[color=00ff00]Run ifconfig[/color]'
  55.                    on_release: root.run_ifconfig()
  56.                    markup: True
  57.                    background_normal: 'images/buttonBG.png'
  58.                Button:
  59.                    id: scnLAN
  60.                    size_hint: .5, .4
  61.                    text: '[color=00ff00]ARP Scan[/color]'
  62.                    on_release: root.ARPscan()
  63.                    background_normal: 'images/buttonBG.png'
  64.                    disabled: True
  65.                    markup: True
  66.                Button:
  67.                    id: save_btn
  68.                    text: '[color=00ff00]Save Results[/color]'
  69.                    size_hint: .5, .4
  70.                    background_normal: 'images/buttonBG.png'
  71.                    markup: True
  72.                    disabled: True
  73.                    on_press: root.saveResults()
  74.        ScrollView:
  75.            size_hint: 0.5, 1
  76.            do_scroll_x: False
  77.            bar_width: 10
  78.            BoxLayout:
  79.                id: nodes
  80.                orientation: 'vertical'
  81.                size_hint: (1, None)
  82. <MITMs2>:
  83.    BoxLayout:
  84.        canvas:
  85.            Rectangle:
  86.                source: 'images/kapy-bg3.jpg'
  87.                size: self.size
  88.                pos: self.pos
  89.        BoxLayout:
  90.            orientation: 'vertical'
  91.            BoxLayout:
  92.                orientation: 'vertical'
  93.                id: target_box
  94.            BoxLayout:
  95.                Button:
  96.                    text: '[color=00ff00]Back[/color]'
  97.                    size_hint: 1, .2
  98.                    on_press: root.main_screen()
  99.                    markup: True
  100.                    background_normal: 'images/buttonBG.png'
  101.                Button:
  102.                    text: '[color=00ff00]Port Scan[/color]'
  103.                    size_hint: 1, .2
  104.                    markup: True
  105.                    background_normal: 'images/buttonBG.png'
  106.                    on_press: root.portScan()
  107.                Button:
  108.                    text: '[color=00ff00]Save Results[/color]'
  109.                    size_hint: 1, .2
  110.                    on_press: root.main_screen()
  111.                    markup: True
  112.                    background_normal: 'images/buttonBG.png'
  113.        BoxLayout:
  114.            orientation: 'vertical'
  115.            canvas:
  116.                Rectangle:
  117.                    source: 'images/portBG.png'
  118.                    pos: self.pos
  119.                    size: self.size
  120.  
  121.            Label:
  122.                id: portscanResults
  123.                text: '[color=00ff00]Port Scan[/color]'
  124.                markup: True
  125.  
  126. ''')
  127.  
  128. class MITMs1(Screen):
  129.     def __init__(self, **kwargs):
  130.         super(MITMs1, self).__init__(**kwargs)
  131.  
  132.         self.save_btn = self.ids['save_btn']
  133.         self.iface_name = self.ids['iface_name']
  134.         self.device_lbl = self.ids['device_lbl']
  135.         self.client_box = self.ids['client_box']
  136.         self.nodes = self.ids['nodes']
  137.         self.my_ip = '' #Empty string # TUT 5
  138.         self.scnLAN = self.ids['scnLAN']
  139.         self.node_list = [] #list to adjust scrollview size, append nodes
  140.         MITMs1.targetd = {} # Dictionary that will hold our target IP, MAC and hostname
  141.  
  142.  
  143.         self.logos = [['Apple', 'iphone', 'mac', 'ipad'],
  144.                       ['Android', 'nexus'],
  145.                       ['Windows', 'user', '192.168.10.1', 'vista', 'xp'],
  146.                       ['Gateway', '192.168.10.254', 'netgear'],
  147.                       ['Linux', 'apache', 'ubuntu']]
  148.  
  149.     def run_ifconfig(self):
  150.         self.scnLAN.disabled = False
  151.         try:
  152.             self.ifconfig = subprocess.check_output(['ifconfig', self.iface_name.text]) #Add wlan0 for interface.
  153.  
  154.             self.iface, self.my_ip,self.MAC, self.Bcast, self.Nmask, self.ipv6 = (self.ifconfig.split()[i] for i in (0,6,4,7,8,11))# TUT 4
  155.  
  156.             self.device_lbl.text = ('[color=00ff00][i][b]My Device[/b][/i][/color]'+ '\n\n' + 'Interface: '# TUT 4
  157.                 + '[color=00ff00][i]{0}[/i][/color] '.format(self.iface) + '\n\n' + 'IP: '# TUT 4
  158.                 + '[color=00ff00][i]{0}[/i][/color] '.format(self.my_ip[5:]) + '\n\n' +  'MAC: '# TUT 4
  159.                 + '[color=00ff00][i]{0}[/i][/color] '.format(self.MAC) + '\n\n' +  'Bcast: '# TUT 4
  160.                 + '[color=00ff00][i]{0}[/i][/color] '.format(self.Bcast[6:]) + '\n\n' +  'Nmask: '# TUT 4
  161.                 + '[color=00ff00][i]{0}[/i][/color] '.format(self.Nmask[5:]) + '\n\n' +  'IPv6: '# TUT 4
  162.                 + '[color=00ff00][i]{0}[/i][/color] '.format(self.ipv6 ))# TUT 4
  163.         except:
  164.             pass
  165.  
  166.  
  167.  
  168.     def ARPscan(self): #TUT 7
  169.         '''Called to scan the LAN'''
  170.  
  171.         self.save_btn.disabled = False
  172.         self.nodes.clear_widgets()
  173.         self.node_list = [] #make sure list is empty
  174.         try:
  175.             for ip in xrange(255):
  176.                 scanNode = ARPthread(ip,self.nodes,self.iface_name.text,
  177.                                      self.node_list,self.logos, MITMs1.targetd) # Create a thread
  178.                 scanNode.start() # Start the thread
  179.                 print 'Started thread: %s ' %ip
  180.         except:
  181.             pass
  182.  
  183.     def timeDate(self):
  184.         now = time.localtime(time.time())
  185.         return time.asctime(now) # Get the date time
  186.  
  187.     def saveResults(self):
  188.         self.box = BoxLayout(orientation='vertical')
  189.         self.tinput = TextInput(text='') # Name the file
  190.         self.svebtn = Button(text='Save File!', on_press=self.saveFile) # Button to save file
  191.         self.lbl = Label(text='') # Label to display to the user the file is saved
  192.  
  193.         self.box.add_widget(self.tinput)
  194.         self.box.add_widget(self.svebtn)
  195.         self.box.add_widget(self.lbl)
  196.         # Create the popup #
  197.  
  198.         self.pop = Popup(content=self.box, title='Save Scan Results', size_hint=(1, .5))
  199.  
  200.         self.pop.open()
  201.  
  202.     def saveFile(self, i):
  203.         f = open(self.tinput.text + '.txt', 'a')
  204.         f.write('[] LAN Scan [] \n\n' + self.timeDate() + '\n\n' +
  205.         '%s Hosts Up!\n\n' %len(self.node_list))
  206.  
  207.         for i in self.node_list:
  208.             f.write('IP: ' + i.psrc + '\n' + 'MAC: ' + i.hwsrc + '\n\n')
  209.         f.close() # Close the file
  210.         self.lbl.text = 'File Saved!'
  211.  
  212.     def change_screen(self):
  213.         sm.current = 'MITMs2'
  214.         MITMs1.target = self.text # Take the text from the button pressed [NODE]
  215.         MITMs1.logo = self.background_normal # Take the logo assigned to identify the node
  216.  
  217.  
  218.     def load_target(self):
  219.         '''This will be called by the on_enter method of screen 2'''
  220.         self.target_box.clear_widgets() # Clear the box first before adding info
  221.  
  222.         if MITMs1.logo == 'images/gatewayNode.png':
  223.             self.target_box.add_widget(Image(source='zips/gateSpin.zip', anime_delay=1/30,
  224.                                              size_hint=(1,.5), pos_hint={'x': -.3}))
  225.         elif MITMs1.logo == 'images/appleNode.png':
  226.             self.target_box.add_widget(Image(source='zips/appleSpin.zip', anime_delay=1/30,
  227.                                              size_hint=(1,.5), pos_hint={'x': -.3}))
  228.         if MITMs1.logo == 'images/windowsNode.png':
  229.             self.target_box.add_widget(Image(source='zips/winSpin.zip', anime_delay=1/30,
  230.                                              size_hint=(1,.5), pos_hint={'x': -.3}))
  231.         if MITMs1.logo == 'images/linuxNode.png':
  232.             self.target_box.add_widget(Image(source='zips/linuxSpin.zip', anime_delay=1/30,
  233.                                              size_hint=(1,.5), pos_hint={'x': -.3}))
  234.         if MITMs1.logo == 'images/androidNode.png':
  235.             self.target_box.add_widget(Image(source='zips/andSpin.zip', anime_delay=1/30,
  236.                                              size_hint=(1,.5), pos_hint={'x': -.3}))
  237.         elif MITMs1.logo == 'images/unknownNode.png':
  238.             self.target_box.add_widget(Image(source='zips/unkSpin.zip', anime_delay=1/30,
  239.                                              size_hint=(1,.5), pos_hint={'x': -.3}))
  240.  
  241.         for i in MITMs1.targetd:
  242.             if re.findall('\\b'+i+'\\b', MITMs1.target):
  243.                 self.target_box.add_widget(Label(text='[color=00ff00]IP: {0}[/color]'.format(MITMs1.targetd[i][0]) +
  244.                 '\n[color=00ff00]MAC: {0}[/color]'.format(MITMs1.targetd[i][1]) +
  245.                 '\n[color=00ff00]{0}[/color]'.format(MITMs1.targetd[i][2]),
  246.                 markup=True, size_hint=(1,1), pos_hint={'x':-.2}))
  247.  
  248. class ARPthread(threading.Thread):
  249.     '''Threaded ARP scan'''
  250.     def __init__(self,ip,nodes,iface,node_list,logos, targetd):
  251.         self.targetd = targetd
  252.         self.logos = logos
  253.         self.node_list = node_list
  254.         self.ip = ip # host to scan
  255.         self.nodes = nodes # the scrollview box
  256.         self.iface = iface # the text from the textinput for the NIC
  257.         threading.Thread.__init__(self)
  258.     try:
  259.         def run(self):
  260.             arprequest = Ether(dst='ff:ff:ff:ff:ff:ff')/ARP(pdst='192.168.10.'+str(self.ip), hwdst='ff:ff:ff:ff:ff:ff')
  261.             arpresponse = srp1(arprequest, timeout=2, verbose=0, iface=self.iface)
  262.             if arpresponse:
  263.                 self.node_list.append(arpresponse) #add node to the list
  264.                 try:
  265.                     MITMs1.hostname = socket.gethostbyaddr(arpresponse.psrc)[0]
  266.                 except:
  267.                     pass
  268.                 MITMs1.targetd[arpresponse.psrc] = arpresponse.psrc, arpresponse.hwsrc, MITMs1.hostname
  269.                 choose_button = ''
  270.                 for i in self.logos:
  271.                     for make in i:
  272.                         if make.lower() in MITMs1.hostname:
  273.                             choose_button = i[0] #Assign the first item.
  274.  
  275.  
  276.                 if choose_button == 'Android':
  277.                          self.nodes.add_widget(Button(text='[color=00ff00][i]Host is up\n{0}[/i][/color]'.format(MITMs1.targetd[arpresponse.psrc][2])+
  278.                          '\n[color=00ff00]IP: [i]{0}[/i][/color]'.format(MITMs1.targetd[arpresponse.psrc][0])+'\n' +
  279.                     '[color=00ff00]MAC: {0}[/color]'.format(MITMs1.targetd[arpresponse.psrc][1] + '\n' ), markup=True, font_size='15sp',
  280.                     height='150sp', background_normal='images/androidNode.png', valign='middle', halign='center',
  281.                     on_press=MITMs1.change_screen))
  282.  
  283.                 elif choose_button == 'Apple':
  284.                          self.nodes.add_widget(Button(text='[color=00ff00][i]Host is up\n{0}[/i][/color]'.format(MITMs1.targetd[arpresponse.psrc][2])+
  285.                          '\n[color=00ff00]IP: [i]{0}[/i][/color]'.format(MITMs1.targetd[arpresponse.psrc][0])+'\n' +
  286.                     '[color=00ff00]MAC: {0}[/color]'.format(MITMs1.targetd[arpresponse.psrc][1] + '\n' ), markup=True, font_size='15sp',
  287.                     height='150sp', background_normal='images/appleNode.png', valign='middle', halign='center',
  288.                     on_press=MITMs1.change_screen))
  289.  
  290.                 elif choose_button == 'Windows':
  291.                          self.nodes.add_widget(Button(text='[color=00ff00][i]Host is up\n{0}[/i][/color]'.format(MITMs1.targetd[arpresponse.psrc][2])+
  292.                          '\n[color=00ff00]IP: [i]{0}[/i][/color]'.format(MITMs1.targetd[arpresponse.psrc][0])+'\n' +
  293.                     '[color=00ff00]MAC: {0}[/color]'.format(MITMs1.targetd[arpresponse.psrc][1] + '\n' ), markup=True, font_size='15sp',
  294.                     height='150sp', background_normal='images/windowsNode.png', valign='middle', halign='center',
  295.                     on_press=MITMs1.change_screen))
  296.  
  297.                 elif choose_button == 'Linux':
  298.                          self.nodes.add_widget(Button(text='[color=00ff00][i]Host is up\n{0}[/i][/color]'.format(MITMs1.targetd[arpresponse.psrc][2])+
  299.                          '\n[color=00ff00]IP: [i]{0}[/i][/color]'.format(MITMs1.targetd[arpresponse.psrc][0])+'\n' +
  300.                     '[color=00ff00]MAC: {0}[/color]'.format(MITMs1.targetd[arpresponse.psrc][1] + '\n' ), markup=True, font_size='15sp',
  301.                     height='150sp', background_normal='images/linuxNode.png', valign='middle', halign='center',
  302.                     on_press=MITMs1.change_screen))
  303.  
  304.                 elif choose_button == 'Gateway':
  305.                          self.nodes.add_widget(Button(text='[color=00ff00][i]Host is up\n{0}[/i][/color]'.format(MITMs1.targetd[arpresponse.psrc][2])+
  306.                          '\n[color=00ff00]IP: [i]{0}[/i][/color]'.format(MITMs1.targetd[arpresponse.psrc][0])+'\n' +
  307.                     '[color=00ff00]MAC: {0}[/color]'.format(MITMs1.targetd[arpresponse.psrc][1] + '\n' ), markup=True, font_size='15sp',
  308.                     height='150sp', background_normal='images/gatewayNode.png', valign='middle', halign='center',
  309.                     on_press=MITMs1.change_screen))
  310.  
  311.                 if choose_button == '':
  312.                          self.nodes.add_widget(Button(text='[color=00ff00][i]Host is up\n{0}[/i][/color]'.format(MITMs1.targetd[arpresponse.psrc][2])+
  313.                          '\n[color=00ff00]IP: [i]{0}[/i][/color]'.format(MITMs1.targetd[arpresponse.psrc][0])+'\n' +
  314.                     '[color=00ff00]MAC: {0}[/color]'.format(MITMs1.targetd[arpresponse.psrc][1] + '\n' ), markup=True, font_size='15sp',
  315.                     height='150sp', background_normal='images/unknownNode.png', valign='middle', halign='center',
  316.                     on_press=MITMs1.change_screen))
  317.             self.nodes.height = len(self.node_list * 150)
  318.     except:
  319.         pass
  320.  
  321. class MITMs2(Screen):
  322.     def __init__(self, **kwargs):
  323.         super(MITMs2, self).__init__(**kwargs)
  324.         self.target_box = self.ids['target_box']
  325.         self.bind(on_enter=MITMs1.load_target)
  326.  
  327.         self.portscanResults = self.ids['portscanResults']
  328.  
  329.     def portScan(self):
  330.         self.ports_list = []
  331.         for i in MITMs1.targetd:
  332.             if i in MITMs1.target:
  333.                 for port in xrange(5):
  334.  
  335.                     pkt = sr1(IP(dst=MITMs1.targetd[i][0])/TCP(sport=123, dport=78+port, flags='S'), timeout=1)
  336.                     try:
  337.                         if pkt.getlayer(TCP).flags == 18L:
  338.                             self.ports_list.append(pkt.getlayer(TCP).sport)
  339.                             print 'Port is OPeN!'
  340.                         elif pkt.getlayer(TCP).flags == 20L:
  341.                             print 'Port is closed'
  342.  
  343.                     except:
  344.                         pass
  345.  
  346.  
  347.  
  348.         if len(self.ports_list) > 0:
  349.             for i in self.ports_list:
  350.                 self.portscanResults.text = '[color=00ff00]Port {0} is open![/color]'.format(str(i))
  351.  
  352.         else:
  353.             self.portscanResults.text = '[color=00ff00] No open ports found [/color]'
  354.  
  355.  
  356.  
  357.     def main_screen(self):
  358.         sm.current = 'MITMs1'
  359.  
  360. sm = ScreenManager()
  361. sm.add_widget(MITMs1(name='MITMs1'))
  362. sm.add_widget(MITMs2(name='MITMs2'))
  363.  
  364.  
  365.  
  366.  
  367.  
  368. class MITMtool(App):
  369.     def build(self):
  370.         return sm #Return the Screen (Screen 1)
  371.  
  372.  
  373. if __name__=='__main__':
  374.     MITMtool().run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement