Skip to content
Permalink
845cc67f18
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
87 lines (71 sloc) 2.26 KB
#!/usr/bin/python3
import socket
import random
import os
import select
portOptions=[37000+x*100+x*2 for x in range(9)]
port=random.choice(portOptions)
hostname=socket.gethostname()
serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
serversocket.bind((hostname, port))
os.system("""ifconfig eth0|grep inet[^6]|cut -d " " -f 10""")
#print(f"Serving on port {port}")
serversocket.listen()
response1=b"57 65 6c 63 6f 6d 65 20 74 6f 20 57 79 64 61 68"
class WydSock:
def __init__(self, sock):
self.sock = sock
def send(self, msg):
if isinstance(msg,str):
msg=str.encode(msg)
self.sock.sendall(msg)
def rcv(self):
chunks = []
bytes_recd = 0
chunks=[]
while bytes_recd <=0:
chunk = self.sock.recv(1024)
chunks.append(chunk)
bytes_recd = len(chunk)
return b''.join(chunks)
def close(self):
self.sock.shutdown(socket.SHUT_RDWR)
self.sock.close()
def status(self):
try:
ready_to_read, ready_to_write, in_error = select.select([self.sock,], [self.sock,], [], 5)
return len(ready_to_read)
except select.error:
return False
rdata="Q29uZ3JhdHVsYXRpb25zLCB5b3UgZm91bmQgYSBmbGFnLiAiZmxhZzpoZWF2ZWhvIg=="
while True:
(clientsocket, address) = serversocket.accept()
print(f"Got a connection from {address}")
s=WydSock(clientsocket)
connected=True
s.send(response1)
while connected:
status=s.status()
if status is False:
connected=False
elif status>0:
data=s.rcv().strip().upper()
print(f"Revieved data: {data}")
if(data==b"CLOSE"):
connected=False
elif data==b"SHADOW":
data="Unable to open"
try:
f = open("/etc/shadow",mode='r')
data = f.read()
f.close()
except Exception as e:
print(e)
data=str(e)
s.send(data)
else:
s.send(rdata)
s.close()
serversocket.shutdown(socket.SHUT_RDWR)
serversocket.close()
break