Advertisement
RootOfTheNull

Python Threaded Service / CTF Challenge "Abyss"

Jan 5th, 2018
600
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.11 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. import SocketServer
  4. import time
  5. import threading
  6. import random
  7.  
  8. class Service(SocketServer.BaseRequestHandler):
  9.     def handle( self ):
  10.        
  11.         print "Someone connected!"
  12.  
  13.         handle = open('/dev/urandom')
  14.  
  15.         offset = random.randint( 60, 120 )
  16.         starting_time = int(time.time())  
  17.  
  18.         while ( True ):
  19.            
  20.             if ( int(time.time()) != starting_time + offset ):
  21.                 # if it is not the special time to send the flag, send the garbage...
  22.                 self.send( handle.read(1), newline = False )
  23.             else:
  24.                 # if it the special time, send the flag!
  25.                 flag = 'USCGA{you_can_see_through_the_abyss}'
  26.                 self.send( flag, newline = False )
  27.  
  28.                 # now reset the offset and the clockto get another random time.
  29.                 starting_time = int(time.time())
  30.                 offset = random.randint( 60, 120 )
  31.  
  32.     def send( self, string, newline = True ):
  33.         if newline: string = string + "\n"
  34.         self.request.sendall(string)  # this `request` object is internal to the BaseRequestHandler that we inherit.
  35.  
  36.     def receive( self, prompt = " > " ):
  37.         self.send( prompt, newline = False )
  38.         return self.request.recv( 4096 ).strip()
  39.  
  40.  
  41. # this class literally doesn't need to do anything, but we need it to exist
  42. # to make the threaded service and serve it up.
  43. class ThreadedService( SocketServer.ThreadingMixIn, SocketServer.TCPServer, SocketServer.DatagramRequestHandler ):
  44.     pass
  45.  
  46. def main():
  47.  
  48.     port = 6667       # we obviously need a port to run on...
  49.     host = '0.0.0.0'  # the 0.0.0.0 host makes it visible to LAN
  50.  
  51.     service = Service # not an object, but at least use the class...
  52.    
  53.     # now we can create the server object, using the host and port that we define
  54.     # and hosting our service (the class we will keep working on very soon!)
  55.     server = ThreadedService( ( host, port ), service )
  56.     server.allow_reuse_address = True
  57.  
  58.     server_thread = threading.Thread( target = server.serve_forever )
  59.  
  60.     server_thread.daemon = True
  61.     server_thread.start()
  62.  
  63.     print "Server started on port", port
  64.  
  65.     # Now let the thread just do its thing. We'll wait and do nothing...
  66.     while ( True ): time.sleep(60)
  67.  
  68.  
  69. if ( __name__ == "__main__" ):
  70.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement