cyphesis/rulesets/basic/metaserver.py

65 lines
1.6 KiB
Python
Raw Permalink Normal View History

#!/usr/bin/python
import socket
import struct
SKEEP_ALIVE = socket.htonl(1)
CKEEP_ALIVE = socket.htonl(2)
HANDSHAKE = socket.htonl(3)
SERVERSHAKE = socket.htonl(4)
CLIENTSHAKE = socket.htonl(5)
TERMINATE = socket.htonl(6)
LIST_REQ = socket.htonl(7)
LIST_RESP = socket.htonl(8)
PROTO_ERANGE = socket.htonl(9)
def metaquery():
ip = "80.68.90.68"
port = 8453
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.sendto(struct.pack('I', CKEEP_ALIVE), (ip, port))
data,addr = s.recvfrom(8)
res = struct.unpack('II', data)
if res[0] != HANDSHAKE:
2018-05-02 15:27:55 +02:00
print(socket.ntohl(res[0]), HANDSHAKE)
print("no handshake")
return
2018-05-02 15:27:55 +02:00
print("handshook")
2010-07-31 12:29:26 +01:00
shake = res[1]
s.sendto(struct.pack('II', CLIENTSHAKE, shake), (ip, port))
2018-05-02 15:27:55 +02:00
print("listing")
2010-07-31 12:29:26 +01:00
segment=0
while True:
s.sendto(struct.pack('II', LIST_REQ, socket.htonl(segment)), (ip, port))
data,addr = s.recvfrom(4096)
2018-05-02 15:27:55 +02:00
print(len(data))
2010-07-31 12:29:26 +01:00
res = struct.unpack('I', data[:4])
2018-05-02 15:27:55 +02:00
print(socket.ntohl(res[0]))
2010-07-31 12:29:26 +01:00
if res[0] == LIST_RESP:
payload = struct.unpack('II', data[4:12])
count = socket.ntohl(payload[1])
if len(data) != (12 + count * 4):
2018-05-02 15:27:55 +02:00
print("Error: ", len(data), count * 4)
2010-07-31 12:29:26 +01:00
return
for i in range(0, count):
entry = struct.unpack('BBBB', data[12+i*4:16+i*4])
2018-05-02 15:27:55 +02:00
print("%d.%d.%d.%d" % entry)
2010-07-31 12:29:26 +01:00
segment += count
2018-05-02 15:27:55 +02:00
print(segment, " got.")
2010-07-31 12:29:26 +01:00
if segment == socket.ntohl(payload[0]):
2018-05-02 15:27:55 +02:00
print("Done.")
2010-07-31 12:29:26 +01:00
return
metaquery()