├── PythonSnifferGui.py └── README.md /PythonSnifferGui.py: -------------------------------------------------------------------------------- 1 | # -*- coding: cp1252 -*- 2 | 3 | #Modules Required 4 | 5 | #USED FOR--> 6 | from Tkinter import * #GUI Development 7 | import time #time shown on ststus bar 8 | from tkFileDialog import askopenfilename,asksaveasfilename #Dialog box opened when open and save file using menu 9 | from pcapy import findalldevs,open_live #Finding all interfaces on the computer 10 | from impacket import ImpactDecoder, ImpactPacket #Decoding the Raw Packets Captured 11 | import thread #Threading the Sniffer so it will not freeze the mainloop of GUI 12 | import sys # 13 | import tkMessageBox #Popup box 14 | 15 | 16 | 17 | 18 | 19 | #GLOBAL VARIABLES 20 | 21 | var='' #var is the variable for checkboxes(select the interface) 22 | curtime = '' #time shown on the statusbar 23 | interface1='' #Interface for sniffing 24 | 25 | ####MAIN FUNCTION programming####### 26 | 27 | def main(): #Main function 28 | def sel(): #A function for Selecting the interface 29 | global interface1 #global keyword is used for accesing global variable 30 | interface1 = str(var1.get()) #retriving value from Radiobutton variable 'var' using get method 31 | 32 | devices = findalldevs(); #Find all Devices "eth0,lo, wlan0 ..etc" All values are in array 'devices' 33 | 34 | eth0=str(devices[0]) # saving the devices value in linux style [ethernet cable] 35 | wlan0=str(devices[1]) # """"""""""""""""""""""""""""""""""""""" [Wireless LAN] 36 | nflog=str(devices[2]) # """"""""""""""""""""""""""""""""""""""" [Kernel...High level] 37 | lo=str(devices[3]) # """"""""""""""""""""""""""""""""""""""" [Local host] 38 | 39 | 40 | try : # Exceptional Handling [try keyword] 41 | def sniffme(): # Function for beginning sniffer 42 | 43 | global interface1 #Accesing global variable 44 | print "Sniffer Initiated " #printing message 45 | for i in range(10): #for loop is used to write 10 number of dots before printing ant result [just for fun ] No offence 46 | sys.stdout.write('.') # use of sys module to access stdout "which prints ['.']" 47 | time.sleep(.3) #sleep for 300ms after printing '.' 48 | sys.stdout.write('Here we Go -->') #Same as Before 49 | time.sleep(.3) 50 | pc = open_live(interface1, 65536, True, 1000) ####OPen_live is a function in Pcapy for sniffing the real time packets. Arguments passed-->"Name of the Interface",Number of Bytes to capture,"Promiscus mode on/off","time to read" 51 | pc.setfilter('tcp') #Default packet filter is set to TCP 52 | 53 | 54 | ####DECODING OF RAW PACKET 55 | def processPacket(hdr, data): #A Function name processPacket 56 | decoder = ImpactDecoder.EthDecoder() #use of Decoder module and method 57 | packet=decoder.decode(data) #here data is the raw data capture by pcapy's open_live function 58 | ippacket=packet.child() #ip packets 59 | tcppacket=packet.child() #TCP packets 60 | print tcppacket #Final capture Packets are printed on console 61 | packet_limit = -1 #local variable set for looping the printing of live packet captures 62 | pc.loop(packet_limit, processPacket) #pc.loop [here pc is the pcapy open_live function output] 63 | except(KeyboardInterrupt, SystemExit): #If any Keyboard interrupt happens then the sniffer stops and exits 64 | cleanup_stop_thread(); #Stoping thread 65 | 66 | 67 | 68 | 69 | ####################GRAPHICAL USER INTERFACE ######################## 70 | 71 | root=Tk() #Main Window [Whole] 72 | root.minsize(700,500) #minimum size is set too 700 wide and 500 height 73 | root.maxsize(700,500) #Maximum """"""""""""""""""""""""""""""""""""""" 74 | root.geometry("700x500") #Normal Geometry [size] when program run 75 | root.title("Packet Sniffer 0.1") # Title of the Window [Titlebar] 76 | 77 | 78 | 79 | 80 | def callback(): #Test Function to print a message 81 | print "called the callback!" 82 | 83 | def OpenFile(): 84 | name = askopenfilename(filetypes=[("PCAP file","*.pcap")],title="Open PCAP file") #dialog box for opening a file 85 | 86 | def SaveFile(): 87 | asksaveasfilename(filetypes=[("PCAP file","*.pcap")],title="Save PCAP file") #Dialog Box for Saving a file 88 | 89 | def quitGui(): # Quiting the Program 90 | tkMessageBox.showinfo(title = 'Quit', message = "Do you really Want to Exit? ") #Message box asking the action 91 | root.destroy() #Gui Closed 92 | 93 | menu= Menu(root) #Menubar Created under main'root' window 94 | root.config(menu=menu) #name of the menubar is menu 95 | 96 | filemenu=Menu(menu) #first parent menu 'filemenu' in menubar named 'menu' 97 | menu.add_cascade(label="File",menu=filemenu) #name of the parent menu is 'File' 98 | filemenu.add_command(label="Open",command=OpenFile) #submenu with action[command to execute] 99 | filemenu.add_command(label="Save",underline=0,background='white',activebackground='orange',command=SaveFile) #submenu with action""""""""""""""""""" 100 | 101 | filemenu.add_separator() # 102 | filemenu.add_command(label="Exit",command=root.quit) # 103 | helpmenu=Menu(menu) #Same function as above 104 | menu.add_cascade(label="Help", menu=helpmenu) # 105 | helpmenu.add_command(label="About...",command=callback) # 106 | 107 | 108 | ################################################################ 109 | 110 | fm = Frame(root, width=500, height=500,bg= "#374a89") #Frame is created under root with given Dimentions 111 | xf2=Frame(root,height=200,width=500,bg="green") #anather frame with attributes 'bg is background' 112 | #Note:Attributes are Case Sensitive 113 | xf=Frame(fm, relief="solid", borderwidth=2,pady=8,bg='grey') #pady is padding in y direction 114 | Label(xf, text="SELECT INTERFACE",relief='flat',bg='#526aba', fg='white').pack(pady=3,padx=10,side=LEFT) #side is the "where to place in respect of previous one" 115 | 116 | var1=StringVar() 117 | Radiobutton(xf, text='eth0', variable=var1, 118 | value=eth0,command=sel).pack(side=LEFT, anchor=W,padx=3) 119 | Radiobutton(xf, text='wlan0', variable=var1, 120 | value=wlan0,command=sel).pack(side=LEFT, anchor=W,padx=3) 121 | Radiobutton(xf, text='lo', variable=var1, 122 | value=lo,command=sel).pack(side=LEFT, anchor=W,padx=3) 123 | Radiobutton(xf, text='nflog', variable=var1, 124 | value=nflog,command=sel).pack(side=LEFT, anchor=W,padx=3) 125 | 126 | 127 | label1=Label(xf,text='ENTER MANUALLY',relief='flat',bg='#526aba', fg='white').pack(side=LEFT,anchor=W) 128 | 129 | InterfaceText=StringVar() 130 | InterfaceEntry=Entry(xf,textvariable=InterfaceText) 131 | InterfaceEntry.focus_set() 132 | InterfaceEntry.pack(side=LEFT,anchor=W,padx=15) 133 | 134 | 135 | def setInterface(): interface1=InterfaceText 136 | bt1=Button(xf,text="Okay",relief='ridge',width=15, command=setInterface,bg='#526aba', fg='white').pack(side=LEFT,anchor=W,padx=5,pady=2) 137 | xf.pack(side=TOP , anchor=NW,expand=NO,fill=BOTH) 138 | #f.pack(side=TOP, anchor=NW) 139 | 140 | 141 | 142 | 143 | 144 | # t2 = Toplevel(root) 145 | #Label(t2, text='Result will be shown Here').pack(padx=10, pady=10) 146 | #t2.transient(root) 147 | 148 | # create a toolbar 149 | toolbar = Frame(root, bg="#374a89" , relief='raised') 150 | 151 | 152 | 153 | openbutton = Button(toolbar, text="Open", width=6, command=OpenFile) 154 | openbutton.pack(side=LEFT, padx=2, pady=2) 155 | 156 | b = Button(toolbar, text="Save", width=6, command=SaveFile) 157 | b.pack(side=LEFT, padx=2, pady=2) 158 | 159 | saveandexit = Button(toolbar, text=" Exit ", command=quitGui) 160 | saveandexit.pack(side=RIGHT, padx=2, pady=2,anchor=N) 161 | 162 | clock = Label(toolbar,bg='#374a89',fg='white') 163 | clock.pack(anchor=CENTER,padx=0) 164 | 165 | 166 | def tick(): 167 | global curtime 168 | newtime = time.strftime('%H:%M:%S') 169 | if newtime != curtime: 170 | curtime = newtime 171 | clock.config(text=curtime) 172 | clock.after(200, tick) 173 | 174 | tick() 175 | 176 | 177 | 178 | toolbar.pack(side=TOP, fill=BOTH,expand=NO) 179 | 180 | 181 | ################################################# 182 | Label(fm, text='FILTER',relief='solid',bg="black",fg="white",height=1,width=7,bd=5).pack(side=LEFT,anchor=NW,padx=1,pady=6) 183 | 184 | SelectProtocol=Frame(fm,bg="#91d46a",relief='ridge',bd=2) 185 | 186 | 187 | 188 | 189 | class Dummy: pass 190 | var = Dummy() 191 | 192 | for castmember, row, col, status in [ 193 | ('TCP', 0,0,NORMAL), ('ICMP', 0,1,NORMAL), 194 | ('IP', 0,2,NORMAL)]: 195 | setattr(var, castmember, IntVar()) 196 | Checkbutton(SelectProtocol, text=castmember, state=status, anchor=W, 197 | variable = getattr(var, castmember),height=1,relief='flat',bg='#91d46a').grid(row=row, column=col, sticky=W) 198 | 199 | 200 | 201 | SelectProtocol.pack(side=LEFT,anchor=NW,padx=1,pady=6) 202 | 203 | 204 | 205 | 206 | ################################################## 207 | CountFrame=Frame(fm,relief='ridge',bg='#91d46a',bd=1) 208 | Label(fm, text='NUMBER OF BYTES TO CAPTURE',relief='solid',bg="black",fg="white",height=1,width=30,bd=5).pack(side=LEFT,anchor=NW,padx=5,pady=6) 209 | 210 | var = StringVar() 211 | entry = Entry(CountFrame, textvariable=var,width=5,relief='ridge',bd=1) 212 | entry.focus_set() 213 | entry.pack(side=LEFT,padx=3) 214 | var.set(root.title()) 215 | def changeTitle(): root.title(var.get()) 216 | var.set(1024) 217 | 218 | Button(CountFrame, text="SET", command=changeTitle,bg='#004e00', fg='white').pack() 219 | CountFrame.pack(side=LEFT,anchor=NW,pady=6,padx=1) 220 | ############################################################################################### 221 | scrollbar = Scrollbar(xf2) 222 | scrollbar.pack(side=RIGHT, fill=Y) 223 | 224 | listbox = Text(xf2, yscrollcommand=scrollbar.set,bg='black',fg='red') 225 | Z=""" ..:::::::::.. 226 | ..:::aad8888888baa:::.. 227 | .::::d:?88888888888?::8b::::. 228 | .:::d8888:?88888888??a888888b:::. 229 | .:::d8888888a8888888aa8888888888b:::. 230 | ::::dP::::::::88888888888::::::::Yb:::: 231 | ::::dP:::::::::Y888888888P:::::::::Yb:::: 232 | ::::d8:::::::::::Y8888888P:::::::::::8b:::: 233 | .::::88::::::::::::Y88888P::::::::::::88::::. 234 | :::::Y8baaaaaaaaaa88P:T:Y88aaaaaaaaaad8P::::: 235 | :::::::Y88888888888P::|::Y88888888888P::::::: 236 | ::::::::::::::::888:::|:::888:::::::::::::::: 237 | `:::::::::::::::8888888888888b::::::::::::::' 238 | :::::::::::::::88888888888888:::::::::::::: 239 | :::::::::::::d88888888888888::::::::::::: 240 | ::::::::::::88::88::88:::88:::::::::::: 241 | `::::::::::88::88::88:::88::::::::::' 242 | `::::::::88::88::P::::88::::::::' 243 | `::::::88::88:::::::88::::::' 244 | ``:::::::::::::::::::'' 245 | Packet Sniffer ``:::::::::'' Beta* 246 | 247 | Gathering Realtime Data in the Network """ 248 | listbox.insert(END,Z) 249 | listbox.pack(side=LEFT, fill=BOTH,expand=YES) 250 | 251 | scrollbar.config(command=listbox.yview) 252 | 253 | 254 | 255 | ###################################################################################### 256 | 257 | 258 | print "[+]Please Select the Interface " 259 | ############################################################################################################## 260 | def threadone(): 261 | try: 262 | thread.start_new_thread(sniffme,()) 263 | except (KeyboardInterrupt, SystemExit): 264 | cleanup_stop_thread(); 265 | sys.exit() 266 | 267 | 268 | def threadinterrupt(): 269 | thread.interrupt_main() 270 | 271 | MainButton1=Button(fm,text="Stop Capture",bg="Red", fg="white",height=2,relief='ridge',command=quitGui,activebackground='#eb0000',activeforeground='white').pack(side=RIGHT,anchor=NE,pady=1,expand=YES,fill=X) 272 | MainButton=Button(fm,text="Start Capture",bg="#004e00", fg="white",height=2,relief='ridge',command=threadone,activebackground='#003a00',activeforeground='white').pack(side=RIGHT,anchor=NE,pady=1,padx=2,expand=YES,fill=X) 273 | 274 | 275 | ############################################################################## 276 | 277 | 278 | ####################################################################### 279 | fm.pack(side=TOP, expand=YES, fill=X) 280 | xf2.pack() 281 | 282 | root.mainloop() 283 | if __name__=="__main__": 284 | 285 | main() 286 | 287 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Sniffer 2 | python packet sniffer 3 | 4 | ![Image_figure](http://i.imgur.com/CSSkJfT.jpg) 5 | --------------------------------------------------------------------------------