Skip to content

Instantly share code, notes, and snippets.

@maca134
Created December 6, 2014 02:10
Show Gist options
  • Save maca134/8c7b389fe54ba5fc5dfa to your computer and use it in GitHub Desktop.
Save maca134/8c7b389fe54ba5fc5dfa to your computer and use it in GitHub Desktop.
import socket #for sockets
import sys #for exit
import binascii
import time
import threading
def rcon_hash(msg):
hashdec = (binascii.crc32(msg) & 0xFFFFFFFF)
hashdec = "%08X" % hashdec
hx = '0x';
ha = hx + hashdec[0:2];
hb = hx + hashdec[2:4];
hc = hx + hashdec[4:6];
hd = hx + hashdec[6:8];
return chr(int(hd, 16)) + chr(int(hc, 16)) + chr(int(hb, 16)) + chr(int(ha, 16));
def rcon_login(host, port, password):
head = chr(0x42) + chr(0x45);
passmsg = chr(0xFF) + chr(0x00) + password;
loginmsg = chr(0x42) + chr(0x45) + rcon_hash(passmsg) + passmsg;
s.sendto(loginmsg, (host, port))
d = s.recvfrom(1024)
reply = d[0]
addr = d[1]
return True if (str(ord(reply[-1:])) == "1") else False;
host = '127.0.0.1';
port = 2302;
password = 'password';
try:
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
s.bind(('', port))
except socket.error:
print 'Failed to create socket'
sys.exit()
if rcon_login(host, port, password) == False:
print 'Login failed';
sys.exit();
print 'Login success';
#print ' '.join(str(hex(ord(c))) for c in loginmsg)
def keepalive(s, host, port):
i = 0
while (1):
msg = chr(0xFF) + chr(0x01) + chr(i)
msg = chr(0x42) + chr(0x45) + rcon_hash(msg) + msg;
s.sendto(msg, (host, port))
time.sleep(10)
t = threading.Thread(target=keepalive, args = (s, host, port))
t.daemon = True
t.start()
def parse_chat(chatResponse):
checksum = chatResponse[2:6]
cmd = str(ord(chatResponse[7:8]))
seq = chatResponse[8:9]
chat = chatResponse[9:]
if cmd == "2":
msg = chr(0xFF) + chr(0x02) + seq;
msg = chr(0x42) + chr(0x45) + rcon_hash(msg) + msg;
s.sendto(msg, (host, port))
print chat.strip()
while(1) :
d = s.recv(1024)
reply = d[0]
addr = d[1]
parse_chat(d)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment