1
0
Fork 0
mirror of https://github.com/de2tla2f/pymaster.git synced 2024-11-23 19:41:27 +00:00

use version by @xdettlaff

This commit is contained in:
ghost 2023-12-26 18:58:55 +02:00
parent 75487e36e8
commit ba2acb1f71

View file

@ -6,264 +6,247 @@ import traceback
import logging import logging
import os import os
from optparse import OptionParser from optparse import OptionParser
from struct import pack, unpack from struct import pack
from time import time from time import time
from ipaddress import ip_address, ip_network
from server_entry import ServerEntry from server_entry import ServerEntry
from protocol import MasterProtocol from protocol import MasterProtocol
import ipfilter
LOG_FILENAME = 'pymaster.log' LOG_FILENAME = "pymaster.log"
MAX_SERVERS_FOR_IP = 14 MAX_SERVERS_FOR_IP = 14
CHALLENGE_SEND_PERIOD = 10
def log(msg):
logging.debug(msg)
class RateLimitItem:
def __init__(self, resetAt):
self.reset(resetAt)
def reset(self, resetAt):
self.resetAt = resetAt
self.logs = self.calls = 0
def inc(self):
self.calls = self.calls + 1
self.logs = self.logs + 1
def shouldReset(self, curtime):
return curtime > self.resetAt
class IPRateLimit:
def __init__(self, type, period, maxcalls):
self.type = type
self.period = period
self.maxcalls = maxcalls
self.maxlogs = maxcalls + 2
self.ips = {}
def ratelimit(self, ip):
curtime = time()
if ip not in self.ips:
self.ips[ip] = RateLimitItem(curtime + self.period)
elif self.ips[ip].shouldReset(curtime):
self.ips[ip].reset(curtime + self.period)
self.ips[ip].inc()
if self.ips[ip].calls > self.maxcalls:
if self.ips[ip].logs < self.maxlogs:
log('Ratelimited %s %s' % (self.type, ip))
return True
return False
class PyMaster: class PyMaster:
def __init__(self, ip, port): def __init__(self, ip, port):
self.serverList = [] self.serverList = []
self.serverRL = IPRateLimit('server', 60, 30) self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self.clientRL = IPRateLimit('client', 60, 120) self.sock.bind((ip, port))
self.ipfilterRL = IPRateLimit('filterlog', 60, 10)
self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self.sock.bind((ip, port))
log("Welcome to PyMaster!") logging.debug("Welcome to PyMaster!")
log("I ask you again, are you my master?") logging.debug("I ask you again, are you my master? @-@")
log("Running on %s:%d" % (ip, port)) logging.debug("Running on %s:%d" % (ip, port))
def serverLoop(self):
data, addr = self.sock.recvfrom(1024)
if ip_address(addr[0]) in ipfilter.ipfilter: def server_loop(self):
if not self.ipfilterRL.ratelimit(addr[0]): data, addr = self.sock.recvfrom(1024)
log('Filter: %s:%d' % (addr[0], addr[1])) data = data.decode("latin_1")
return
if len(data) == 0: match data[0]:
return case MasterProtocol.clientQuery:
self.client_query(data, addr)
case MasterProtocol.challengeRequest:
self.send_challenge_to_server(data, addr)
case MasterProtocol.addServer:
self.add_server_to_list(data, addr)
case MasterProtocol.removeServer:
self.remove_server_from_list(data, addr)
case other:
logging.debug("Unknown message: {0} from {1}:{2}".format(data, addr[0], addr[1]))
# only client stuff
if data.startswith(MasterProtocol.clientQuery):
if not self.clientRL.ratelimit(addr[0]):
self.clientQuery(data, addr)
return
# only server stuff def client_query(self, data, addr):
if not self.serverRL.ratelimit(addr[0]): region = data[1] # UNUSED
if data.startswith(MasterProtocol.challengeRequest): data = data.strip("1" + region)
self.sendChallengeToServer(data, addr) try:
elif data.startswith(MasterProtocol.addServer): query = data.split("\0")
self.addServerToList(data, addr) except ValueError:
elif data.startswith(MasterProtocol.removeServer): logging.debug(traceback.format_exc())
self.removeServerFromList(data, addr) return
else:
log('Unknown message: %s from %s:%d' % (str(data), addr[0], addr[1]))
def clientQuery(self, data, addr): queryAddr = query[0] # UNUSED
data = data.decode('latin_1') rawFilter = query[1]
data = data.strip('1' + data[1])
info = data.split('\0')[1].strip('\\')
split = info.split('\\')
protocol = None # Remove first \ character
gamedir = 'valve' rawFilter = rawFilter.strip("\\")
clver = None split = rawFilter.split("\\")
nat = 0
for i in range(0, len(split), 2): # Use NoneType as undefined
try: gamedir = "valve" # halflife, by default
k = split[i] clver = None
v = split[i + 1] nat = 0
if k == 'gamedir':
gamedir = v.lower() # keep gamedir in lowercase
elif k == 'nat':
nat = int(v)
elif k == 'clver':
clver = v
elif k == 'protocol':
protocol = int(v)
# somebody is playing :)
elif k == 'thisismypcid' or k == 'heydevelopersifyoureadthis':
self.fakeInfoForOldVersions(gamedir, addr)
return
else:
log('Client Query: %s:%d, invalid infostring=%s' % (addr[0], addr[1], rawFilter))
except IndexError:
pass
if( clver == None ): # Probably an old vulnerable version for i in range(0, len(split), 2):
self.fakeInfoForOldVersions(gamedir, addr) try:
return key = split[i + 1]
if split[i] == "gamedir":
gamedir = key.lower() # keep gamedir in lowercase
elif split[i] == "nat":
nat = int(key)
elif split[i] == "clver":
clver = key
else:
logging.debug(
"Unhandled info string entry: {0}/{1}. Infostring was: {2}".format(
split[i], key, split
)
)
except IndexError:
pass
packet = MasterProtocol.queryPacketHeader if clver is None: # Probably an old vulnerable version
for i in self.serverList: self.fake_info_for_old_versions(gamedir, addr)
if time() > i.die: return
self.serverList.remove(i)
continue
if not i.check: packet = MasterProtocol.queryPacketHeader
continue for i in self.serverList:
if time() > i.die:
self.serverList.remove(i)
continue
if nat != i.nat or gamedir != i.gamedir: if not i.check:
continue continue
if protocol != None and protocol != i.protocol: if nat != i.nat:
continue continue
if nat: if gamedir is not None and gamedir != i.gamedir:
# Tell server to send info reply continue
data = ('\xff\xff\xff\xffc %s:%d' % (addr[0], addr[1])).encode('latin_1')
self.sock.sendto(data, i.addr)
# Use pregenerated address string if nat:
packet += i.queryAddr reply = "\xff\xff\xff\xffc {0}:{1}".format(addr[0], addr[1])
packet += b'\0\0\0\0\0\0' # Fill last IP:Port with \0 data = reply.encode("latin_1")
self.sock.sendto(packet, addr) # Tell server to send info reply
self.sock.sendto(data, i.addr)
def fakeInfoForOldVersions(self, gamedir, addr): # Use pregenerated address string
def sendFakeInfo(sock, warnmsg, gamedir, addr): packet += i.queryAddr
baseReply = b"\xff\xff\xff\xffinfo\n\host\\" + warnmsg.encode('utf-8') + b"\map\\update\dm\\0\\team\\0\coop\\0\\numcl\\32\maxcl\\32\\gamedir\\" + gamedir.encode('latin-1') + b"\\"
sock.sendto(baseReply, addr)
sendFakeInfo(self.sock, "This version is not", gamedir, addr) packet += b"\0\0\0\0\0\0" # Fill last IP:Port with \0
sendFakeInfo(self.sock, "supported anymore", gamedir, addr) self.sock.sendto(packet, addr)
sendFakeInfo(self.sock, "Please update Xash3DFWGS", gamedir, addr)
sendFakeInfo(self.sock, "From GooglePlay or GitHub", gamedir, addr)
sendFakeInfo(self.sock, "Эта версия", gamedir, addr)
sendFakeInfo(self.sock, "устарела", gamedir, addr)
sendFakeInfo(self.sock, "Обновите Xash3DFWGS c", gamedir, addr)
sendFakeInfo(self.sock, "GooglePlay или GitHub", gamedir, addr)
def removeServerFromList(self, data, addr):
pass
def sendChallengeToServer(self, data, addr): def _send_fake_info(sock, warnmsg, gamedir, addr):
count = 0 baseReply = (
s = None b"\xff\xff\xff\xffinfo\n\host\\"
for i in self.serverList: + warnmsg.encode("utf-8")
if addr[0] != i.addr[0]: + b"\map\\update\dm\\0\\team\\0\coop\\0\\numcl\\32\maxcl\\32\\gamedir\\"
continue + gamedir.encode("latin-1")
if addr[1] == i.addr[1]: + b"\\"
s = i )
break sock.sendto(baseReply, addr)
else:
count += 1
if count > MAX_SERVERS_FOR_IP:
return
challenge2 = None
if len(data) == 6:
# little endian challenge
challenge2 = unpack('<I', data[2:])[0]
if not s: def fake_info_for_old_versions(self, gamedir, addr):
challenge = random.randint(0, 2**32-1) & 0xffffffff #hash(addr[0]) + hash(addr[1]) + hash(time()) error_message = [
s = ServerEntry(addr, challenge) "This version is not",
self.serverList.append(s) "supported anymore",
elif s.sentChallengeAt + 5 > time(): "Please update Xash3DFWGS",
return "From GooglePlay or GitHub",
"Эта версия",
"устарела",
"Обновите Xash3DFWGS c",
"GooglePlay или GitHub",
]
packet = MasterProtocol.challengePacketHeader for string in error_message:
packet += pack('I', s.challenge) _send_fake_info(self.sock, string, gamedir, addr)
# send server-to-master challenge back
if challenge2 is not None:
packet += pack('I', challenge2)
self.sock.sendto(packet, addr) def remove_server_from_list(self, addr):
for server in self.serverList:
if server.addr == addr:
logging.debug("Remove Server: from {0}:{1}".format(addr[0], addr[1]))
self.serverList.remove(server)
def addServerToList(self, data, addr):
# Remove the header. Just for better parsing.
info = data.strip(b'\x30\x0a\x5c').decode('latin_1')
# Find a server with same address def send_challenge_to_server(self, addr):
s = None logging.debug("Challenge Request: from {0}:{1}".format(addr[0], addr[1]))
for s in self.serverList: # At first, remove old server- data from list
if s.addr == addr: # self.removeServerFromList(None, addr)
break
if not s: count = 0
log('Server skipped challenge request: %s:%d' % (addr[0], addr[1])) for i in self.serverList:
return if i.addr[0] == addr[0]:
if i.addr[1] == addr[1]:
self.serverList.remove(i)
else:
count += 1
if count > MAX_SERVERS_FOR_IP:
return
challenge = random.randint(0, 2**32 - 1)
# Add server to list
self.serverList.append(ServerEntry(addr, challenge))
# And send him a challenge
packet = MasterProtocol.challengePacketHeader
packet += pack("I", challenge)
self.sock.sendto(packet, addr)
def add_server_to_list(self, data, addr):
logging.debug("Add Server: from {0}:{1}".format(addr[0], addr[1]))
# Remove the header. Just for better parsing.
serverInfo = data.strip("\x30\x0a\x5c")
# Find a server with same address
for serverEntry in self.serverList:
if serverEntry.addr == addr:
break
serverEntry.setInfoString(serverInfo)
if s.setInfoString( info ):
log('Add server: %s:%d, game=%s/%s, protocol=%d, players=%d/%d/%d, version=%s' % (addr[0], addr[1], s.gamemap, s.gamedir, s.protocol, s.players, s.bots, s.maxplayers, s.version))
else:
log('Failed challenge from %s:%d: %d must be %d' % (addr[0], addr[1], s.challenge, s.challenge2))
def spawn_pymaster(verbose, ip, port): def spawn_pymaster(verbose, ip, port):
if verbose: if verbose:
logging.getLogger().addHandler(logging.StreamHandler()) logging.getLogger().addHandler(logging.StreamHandler())
# logging.getLogger().addHandler(logging.FileHandler(LOG_FILENAME)) logging.getLogger().addHandler(logging.FileHandler(LOG_FILENAME))
logging.getLogger().setLevel(logging.DEBUG) logging.getLogger().setLevel(logging.DEBUG)
masterMain = PyMaster(ip, port)
while True:
try:
masterMain.server_loop()
except Exception:
logging.debug(traceback.format_exc())
masterMain = PyMaster(ip, port)
while True:
try:
masterMain.serverLoop()
except Exception:
log(traceback.format_exc())
pass
if __name__ == "__main__": if __name__ == "__main__":
parser = OptionParser()
parser.add_option('-i', '--ip', action='store', dest='ip', default='0.0.0.0',
help='ip to listen [default: %default]')
parser.add_option('-p', '--port', action='store', dest='port', type='int', default=27010,
help='port to listen [default: %default]')
parser.add_option('-d', '--daemonize', action='store_true', dest='daemonize', default=False,
help='run in background, argument is uid [default: %default]')
parser.add_option('-q', '--quiet', action='store_false', dest='verbose', default=True,
help='don\'t print to stdout [default: %default]')
(options, args) = parser.parse_args() parser = OptionParser()
parser.add_option(
"-i",
"--ip",
action="store",
dest="ip",
default="0.0.0.0",
help="ip to listen [default: %default]",
)
parser.add_option(
"-p",
"--port",
action="store",
dest="port",
type="int",
default=27010,
help="port to listen [default: %default]",
)
parser.add_option(
"-d",
"--daemonize",
action="store_true",
dest="daemonize",
default=False,
help="run in background, argument is uid [default: %default]",
)
parser.add_option(
"-q",
"--quiet",
action="store_false",
dest="verbose",
default=True,
help="don't print to stdout [default: %default]",
)
if options.daemonize != 0: (options, args) = parser.parse_args()
from daemon import pidfile, DaemonContext
with DaemonContext(stdout=sys.stdout, stderr=sys.stderr, working_directory=os.getcwd()) as context: if options.daemonize != 0:
spawn_pymaster(options.verbose, options.ip, options.port) from daemon import DaemonContext
else:
sys.exit(spawn_pymaster(options.verbose, options.ip, options.port)) with DaemonContext(
stdout=sys.stdout, stderr=sys.stderr, working_directory=os.getcwd()
) as context:
spawn_pymaster(options.verbose, options.ip, options.port)
else:
sys.exit(spawn_pymaster(options.verbose, options.ip, options.port))