Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save DosAmp/e2fa8a32754ab38812be to your computer and use it in GitHub Desktop.
Save DosAmp/e2fa8a32754ab38812be to your computer and use it in GitHub Desktop.
minefake.py
#!/usr/bin/python3
import socket, json
server_address = ('', 25565)
disconnect_message = "Server currently unavailable"
# encode an integer into a protobuf-compatible VarInt
def varint(i):
if i < 0:
# dynamic length two's complement
i = (1 << (i.bit_length() + 1)) + i
repres = bytearray()
while True:
rem = i % 128
i //= 128
if i:
repres.append(rem + 128)
else:
repres.append(rem)
break
return repres
# socket.AF_INET6 for an IPv6 or dual stack socket
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as ss:
ss.bind(server_address)
ss.listen(1)
while True:
(cs, caddr) = ss.accept()
handshake_data = cs.recv(1024)
# crude 7/14 bit VarInt decoder
if handshake_data[0] > 127:
handshake_type_offset = 128 * handshake_data[1] + handshake_data[0] - 128
else:
handshake_type_offset = handshake_data[0]
handshake_type = handshake_data[handshake_type_offset]
if handshake_type == 2:
# login connection
disconnect_reason = json.dumps({"text": disconnect_message}).encode("utf-8")
response = b'\x00' + varint(len(disconnect_reason)) + disconnect_reason
else:
# status connection (0x01)
# protocol version 1 is always older than the client
# (Minecraft 1.8 uses 47, 15w39a/15w39b currently uses 74)
status_message = json.dumps({"version": {"name": "offline", "protocol": 1},
"description": {"text": disconnect_message}}).encode("utf-8")
response = b'\x00' + varint(len(status_message)) + status_message
cs.sendall(varint(len(response)) + response)
cs.close()
@DosAmp
Copy link
Author

DosAmp commented Oct 1, 2015

Demonstration

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment