<reponame>rami-github/appliedseclab
#!/usr/bin/python

##########################
# Simple server that listens for connection, starts thread per client.
##########################

import socket
import sys
import threading
import json
import subprocess
import ssl
from os import getcwd
from os.path import join

buf=4096
#home_dir ="/home/imovies/appliedseclab/"
home_dir="/home/diane/Documents/ETH/SecLab/project/code/appliedseclab/bash/"

class ClientThread(threading.Thread):

  def __init__(self, ip, port, socket):
    threading.Thread.__init__(self)
    self.ip = ip
    self.port = port
    self.socket = socket
    print "[+] New thread started for "+ip+":"+str(port)
  
  def parse(self, data):
    """
    The parse function, parses the data and runs the proper script.
    
    It returns the message to send back if all is well. Else -1.
    """
    #separate the flag from the json string
    parts = data.split("_",1)
    flag = parts[0]
    data = parts[1]
    
    #check if valid request
    if flag not in {"G", "R", "RA", "S"}:
	return
    #load the json string as a dictionary
    try:
      dict_=json.loads(data)
    except ValueError:
      print "Wrong JSON encoding"
      return -1
    except: 
      print "unexpected JSON decoding error"
      return -1
    """
    Here we check that the necessary arguments are given. 
    G arguments : 	uname
			email
			adminFlag = employee by default 
    R arguments: 	cert (contents of file to revoke)
			pkey (contents of corresponding pkey file)
    RA arguments: 	uname
    
    S arguments : 	just says {"stats":1}
    """
    if flag=="S":
      #TODO call bash stat script
      print flag
    elif flag=="G":
      if not dict_.has_key("uname") or not dict_.has_key("email"):
        #returning -1 as error
	return -1
      else:
	#TODO call bash generate script with arguments uname email employee
	exitcode=subprocess.call(home_dir+"create-key-and-csr.sh "+dict_["uname"]+" "+dict_["email"]+" employee", shell=True)
 	print exitcode
	return exitcode
    elif flag=="R":
      if not dict_.has_key("cert") or not dict_.has_key("pkey"):
	#returning -1 as error
	return -1
      else: #This means we want to delete specific key
        #TODO save cert and pkey temporarily to files
        cert_str = dict_["cert"]
	pkey_str = dict_["pkey"]
	fd=open(join(home_dir,"rev-cert"), "wb")
	fd.write(cert_str)
	fd.close()
	kd=open(join(home_dir,"rev-key"), "wb")
	kd.write(key_str)
	kd.close()
	#verify they match
	match=subprocess.call(home_dir+"verify-cert-key.sh "+home_dir+"rev-cert "+home_dir+"rev-key")
	print match
	#TODO call bash revoke script
	exitcode=subprocess.call(home_dir+"revoke-one.sh "+home_dir+"rev-cert")
	print exitcode
	return exitcode
    elif flag=="RA":
      if not dict_.has_key("uname"):
        #returning -1 as error
	return -1
      else: #We can revoke all certificates for user "uname"
        #TODO call bash script revoke-all.sh uname
	exitcode=subprocess.call(home_dir+"revoke-all.sh "+dict_["uname"])
	print exitcode
	return exitcode
    else: #Some unforseen error	  
      return -1

  def run(self):    
    print "Connection from : "+ip+":"+str(port)
      
    # receive and parse data
    data = self.socket.recv(buf)
    print "Received: "+data
    resp = self.parse(data)
    if resp==-1:
      resp = "error"
      print resp
    #send response
    self.socket.sendall(str(resp))
    ## end connection
    print "Disconnecting from client"
    self.socket.close()
    print "[-] Thread closed on "+ip+":"+str(port)


#########################################
#					#
#					#
#	Beginning of main thread	#
#					#
#					#
#########################################

HOST = '0.0.0.0' #Symbolic name meaning all available interfaces
PORT = 8888 #Arbitrary non privileged port
server_address = (HOST, PORT)
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print "[+]Socket created on host %s port %d" %server_address
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) #so no 'address already in use' after crash

sock = ssl.wrap_socket(s, certfile="/home/imovies/appliedseclab/certs/ca.crt", keyfile="/home/imovies/appliedseclab/certs/ca.key", server_side=True, do_handshake_on_connect=False)

#Bind socket to local host and port
try:
  sock.bind((HOST, PORT))
except socket.error as msg:
  print "[!!]Bind failed. Error Code: "+str(msg[0])+ " Message "+msg[1]
  sys.exit()

print "[+]Socket bind complete"

#Start listening on socket
sock.listen(10)
print "[+]Socket now listening"
k=0
threads = [] #to be able to wait for all connections to end

#Connection from client and open a thread
while True: 
  #wait to accept a connection - blocking call
  (clientSocket, (ip, port)) = sock.accept()
  #start a thread
  newThread = ClientThread(ip, port, clientSocket)
  newThread.start()
  threads.append(newThread)
  k+=1

for t in threads:
  t.join()

#Finish it all
print "[-]Closing socket on server at %s port %d" %server_address
s.close()
print "[-]That's all folks!"

#!/usr/bin/python

##########################
# Simple server that listens for connection, starts thread per client.
##########################

import socket
import sys
import threading
import json
import subprocess
import ssl
from os import getcwd
from os.path import join

buf=4096
#home_dir ="/home/imovies/appliedseclab/"
home_dir="/home/diane/Documents/ETH/SecLab/project/code/appliedseclab/bash/"

class ClientThread(threading.Thread):

  def __init__(self, ip, port, socket):
    threading.Thread.__init__(self)
    self.ip = ip
    self.port = port
    self.socket = socket
    print "[+] New thread started for "+ip+":"+str(port)
  
  def parse(self, data):
    """
    The parse function, parses the data and runs the proper script.
    
    It returns the message to send back if all is well. Else -1.
    """
    #separate the flag from the json string
    parts = data.split("_",1)
    flag = parts[0]
    data = parts[1]
    
    #check if valid request
    if flag not in {"G", "R", "RA", "S"}:
	return
    #load the json string as a dictionary
    try:
      dict_=json.loads(data)
    except ValueError:
      print "Wrong JSON encoding"
      return -1
    except: 
      print "unexpected JSON decoding error"
      return -1
    """
    Here we check that the necessary arguments are given. 
    G arguments : 	uname
			email
			adminFlag = employee by default 
    R arguments: 	cert (contents of file to revoke)
			pkey (contents of corresponding pkey file)
    RA arguments: 	uname
    
    S arguments : 	just says {"stats":1}
    """
    if flag=="S":
      #TODO call bash stat script
      print flag
    elif flag=="G":
      if not dict_.has_key("uname") or not dict_.has_key("email"):
        #returning -1 as error
	return -1
      else:
	#TODO call bash generate script with arguments uname email employee
	exitcode=subprocess.call(home_dir+"create-key-and-csr.sh "+dict_["uname"]+" "+dict_["email"]+" employee", shell=True)
 	print exitcode
	return exitcode
    elif flag=="R":
      if not dict_.has_key("cert") or not dict_.has_key("pkey"):
	#returning -1 as error
	return -1
      else: #This means we want to delete specific key
        #TODO save cert and pkey temporarily to files
        cert_str = dict_["cert"]
	pkey_str = dict_["pkey"]
	fd=open(join(home_dir,"rev-cert"), "wb")
	fd.write(cert_str)
	fd.close()
	kd=open(join(home_dir,"rev-key"), "wb")
	kd.write(key_str)
	kd.close()
	#verify they match
	match=subprocess.call(home_dir+"verify-cert-key.sh "+home_dir+"rev-cert "+home_dir+"rev-key")
	print match
	#TODO call bash revoke script
	exitcode=subprocess.call(home_dir+"revoke-one.sh "+home_dir+"rev-cert")
	print exitcode
	return exitcode
    elif flag=="RA":
      if not dict_.has_key("uname"):
        #returning -1 as error
	return -1
      else: #We can revoke all certificates for user "uname"
        #TODO call bash script revoke-all.sh uname
	exitcode=subprocess.call(home_dir+"revoke-all.sh "+dict_["uname"])
	print exitcode
	return exitcode
    else: #Some unforseen error	  
      return -1

  def run(self):    
    print "Connection from : "+ip+":"+str(port)
      
    # receive and parse data
    data = self.socket.recv(buf)
    print "Received: "+data
    resp = self.parse(data)
    if resp==-1:
      resp = "error"
      print resp
    #send response
    self.socket.sendall(str(resp))
    ## end connection
    print "Disconnecting from client"
    self.socket.close()
    print "[-] Thread closed on "+ip+":"+str(port)


#########################################
#					#
#					#
#	Beginning of main thread	#
#					#
#					#
#########################################

HOST = '0.0.0.0' #Symbolic name meaning all available interfaces
PORT = 8888 #Arbitrary non privileged port
server_address = (HOST, PORT)
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print "[+]Socket created on host %s port %d" %server_address
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) #so no 'address already in use' after crash

sock = ssl.wrap_socket(s, certfile="/home/imovies/appliedseclab/certs/ca.crt", keyfile="/home/imovies/appliedseclab/certs/ca.key", server_side=True, do_handshake_on_connect=False)

#Bind socket to local host and port
try:
  sock.bind((HOST, PORT))
except socket.error as msg:
  print "[!!]Bind failed. Error Code: "+str(msg[0])+ " Message "+msg[1]
  sys.exit()

print "[+]Socket bind complete"

#Start listening on socket
sock.listen(10)
print "[+]Socket now listening"
k=0
threads = [] #to be able to wait for all connections to end

#Connection from client and open a thread
while True: 
  #wait to accept a connection - blocking call
  (clientSocket, (ip, port)) = sock.accept()
  #start a thread
  newThread = ClientThread(ip, port, clientSocket)
  newThread.start()
  threads.append(newThread)
  k+=1

for t in threads:
  t.join()

#Finish it all
print "[-]Closing socket on server at %s port %d" %server_address
s.close()
print "[-]That's all folks!"
#!/usr/bin/python ########################## # Simple server that listens for connection, starts thread per client. ########################## #home_dir ="/home/imovies/appliedseclab/" The parse function, parses the data and runs the proper script.          It returns the message to send back if all is well. Else -1. #separate the flag from the json string #check if valid request #load the json string as a dictionary Here we check that the necessary arguments are given.      G arguments : 	uname 			email 			adminFlag = employee by default      R arguments: 	cert (contents of file to revoke) 			pkey (contents of corresponding pkey file)     RA arguments: 	uname          S arguments : 	just says {"stats":1} #TODO call bash stat script #returning -1 as error #TODO call bash generate script with arguments uname email employee #returning -1 as error #This means we want to delete specific key #TODO save cert and pkey temporarily to files #verify they match #TODO call bash revoke script #returning -1 as error #We can revoke all certificates for user "uname" #TODO call bash script revoke-all.sh uname #Some unforseen error # receive and parse data #send response ## end connection ######################################### #					# #					# #	Beginning of main thread	# #					# #					# ######################################### #Symbolic name meaning all available interfaces #Arbitrary non privileged port #so no 'address already in use' after crash #Bind socket to local host and port #Start listening on socket #to be able to wait for all connections to end #Connection from client and open a thread #wait to accept a connection - blocking call #start a thread #Finish it all