Skip to content

Commit

Permalink
a bit of security
Browse files Browse the repository at this point in the history
  • Loading branch information
arpruss committed Jun 21, 2016
1 parent b9e7c3d commit a523e00
Show file tree
Hide file tree
Showing 20 changed files with 499 additions and 368 deletions.
2 changes: 0 additions & 2 deletions 19/fast.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,3 @@ rm build/libs/Raspberr*.jar
rm build/libs/Raspberry*ources.jar
mv build/libs/Raspberr* build/libs/RaspberryJamMod.jar
cp build/libs/RaspberryJamMod.jar $APPDATA/.minecraft/mods/1.9/
cp build/libs/RaspberryJamMod.jar $APPDATA/.minecraft/mods/1.9.4/

Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.net.ServerSocket;
import java.net.Socket;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
Expand All @@ -22,6 +26,8 @@

import javax.swing.text.html.HTMLDocument.HTMLReader.IsindexAction;

import com.sun.org.apache.xml.internal.security.utils.Base64;

import net.minecraft.block.Block;
import net.minecraft.block.state.IBlockState;
import net.minecraft.client.Minecraft;
Expand Down Expand Up @@ -107,12 +113,60 @@ public class APIHandler {
protected boolean havePlayer;
protected int playerId;
protected EntityPlayerMP playerMP;
protected List<String> usernames = null;
protected List<String> passwords = null;
protected boolean authenticated;
public static final String PASSWORD_DATA = "passwords.dat";
private String salt = null;
private boolean authenticationSetup;

public APIHandler(MCEventHandler eventHandler, PrintWriter writer) throws IOException {
this(eventHandler, writer, true);
}

public APIHandler(MCEventHandler eventHandler, PrintWriter writer, Boolean needAuthentication) throws IOException {
this.eventHandler = eventHandler;
this.writer = writer;
this.havePlayer = false;
this.playerMP = null;

if (needAuthentication) {
File pass = new File(PASSWORD_DATA);
if (pass.exists()) {
passwords = new ArrayList<String>();
usernames = new ArrayList<String>();
BufferedReader r = new BufferedReader(new FileReader(pass));
String l;
while (null != (l=r.readLine())) {
String[] data = l.trim().split("\\s+");
usernames.add(data[0]);
passwords.add(data[1]);
}
r.close();
}

if (passwords != null) {
authenticated = false;
salt = null;
try {
MessageDigest md = MessageDigest.getInstance("MD5");
md.reset();

byte[] b = ("RaspberryJamMod"+System.currentTimeMillis()).getBytes("UTF-8");

md.update(b);

salt = Base64.encode(md.digest());
} catch (Exception e) {
}
}
else {
authenticated = true;
}
}
else {
this.authenticated = true;
}
}

protected boolean setup() {
Expand Down Expand Up @@ -164,11 +218,52 @@ protected boolean setup() {
}
return true;
}

void handleAuthentication(String input) throws IOException {
if (salt == null)
throw new IOException("salt generation error");

if (input.startsWith("security.authenticate(")) {
input = input.substring(22).trim();
if (input.length()<2)
throw new IOException("authentication error");
input = input.substring(0,input.length()-1);
int count = usernames.size();
for (int i = 0 ; i < count ; i++) {
String data = salt + ":" + usernames.get(i) + ":" + passwords.get(i);
MessageDigest md;
try {
md = MessageDigest.getInstance("MD5");
} catch (NoSuchAlgorithmException e) {
throw new IOException("md5 error");
}
md.reset();

byte[] b = data.getBytes("UTF-8");

md.update(b);

String dataDigest = Base64.encode(md.digest());

if (input.equals(dataDigest)) {
authenticated = true;
return;
}
}
throw new IOException("authentication error");
}
sendLine("challenge "+salt);
}

void process(String clientSentence) {
void process(String clientSentence) throws IOException {
if (!authenticated) {
handleAuthentication(clientSentence);
return;
}

if (!setup())
return;

Scanner scan = null;

try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
public class APIHandlerClientOnly extends APIHandler {

public APIHandlerClientOnly(MCEventHandler eventHandler, PrintWriter writer) throws IOException {
super(eventHandler, writer);
super(eventHandler, writer, false);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ public List getTabCompletionOptions(MinecraftServer server, ICommandSender sende
return null;
}

if (args.length != 1)
return null;
if (args.length != 1)
return null;

int arg = 0;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,12 @@ public void onClose( WebSocket conn, int code, String reason, boolean remote ) {
public void onMessage( WebSocket conn, String message ) {
APIHandler apiHandler = handlers.get(conn);
if (apiHandler != null) {
apiHandler.process(message);
try {
apiHandler.process(message);
}
catch (Exception e) {
conn.close();
}
}
}

Expand Down
Binary file modified 194/.gradle/2.7/taskArtifacts/cache.properties.lock
Binary file not shown.
Binary file modified 194/.gradle/2.7/taskArtifacts/fileHashes.bin
Binary file not shown.
Binary file modified 194/.gradle/2.7/taskArtifacts/fileSnapshots.bin
Binary file not shown.
Binary file modified 194/.gradle/2.7/taskArtifacts/outputFileStates.bin
Binary file not shown.
Binary file modified 194/.gradle/2.7/taskArtifacts/taskArtifacts.bin
Binary file not shown.
2 changes: 1 addition & 1 deletion 194/.gradle/gradle.log
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,4 @@ remapping source...

BUILD SUCCESSFUL

Total time: 48.431 secs
Total time: 43.132 secs
2 changes: 1 addition & 1 deletion go.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
(cd 19 && sh fast.sh)
(cd 194 && ln -s sh fast.sh)
(cd 194 && sh fast.sh)
./zip.sh
rm build/libs/*
./gradlew.bat build
Expand Down
Binary file modified python2-scripts.zip
Binary file not shown.
2 changes: 2 additions & 0 deletions python2-scripts/mcpipy/console.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ def inputLine(prompt):
return 'quit()'
elif c.message == ' ':
return ''
elif "__" in c.message:
sys.exit();
else:
return c.message
time.sleep(0.2)
Expand Down
10 changes: 10 additions & 0 deletions python2-scripts/mcpipy/mcpi/connection.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@

import socket
import select
import sys
import atexit
import os
import platform
import base64
from hashlib import md5
from util import flatten_parameters_to_string

""" @author: Aron Nieminen, Mojang AB"""
Expand Down Expand Up @@ -53,6 +56,13 @@ def close(self):
self.socket.close()
except:
pass

def authenticate(self, username, password):
challenge = self.sendReceive("world.getBlock",0,0,0)
if challenge.startswith("challenge "):
salt = challenge[10:].rstrip()
auth = base64.b64encode(md5(salt+":"+username+":"+password).digest())
self.send("security.authenticate", auth)

def drain(self):
"""Drains the socket of incoming data"""
Expand Down
14 changes: 13 additions & 1 deletion python2-scripts/mcpipy/mcpi/minecraft.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import math
from os import environ
from util import flatten,floorFlatten
import security

""" Minecraft PI low level api v0.1_1
Expand Down Expand Up @@ -185,8 +186,12 @@ def __init__(self, connection=None, autoId=True):
else:
self.conn = Connection()

if security.AUTHENTICATION_USERNAME and security.AUTHENTICATION_PASSWORD:
self.conn.authenticate(security.AUTHENTICATION_USERNAME, security.AUTHENTICATION_PASSWORD)

self.camera = CmdCamera(self.conn)
self.entity = CmdEntity(self.conn)

if autoId:
try:
playerId = int(environ['MINECRAFT_PLAYER_ID'])
Expand All @@ -196,9 +201,16 @@ def __init__(self, connection=None, autoId=True):
playerId = self.getPlayerId(environ['MINECRAFT_PLAYER_NAME'])
self.player = CmdPlayer(self.conn,playerId=playerId)
except:
self.player = CmdPlayer(self.conn)
if security.AUTHENTICATION_USERNAME:
try:
playerId = self.getPlayerId(security.AUTHENTICATION_USERNAME)
except:
self.player = CmdPlayer(self.conn)
else:
self.player = CmdPlayer(self.conn)
else:
self.player = CmdPlayer(self.conn)

self.events = CmdEvents(self.conn)
self.enabledNBT = False

Expand Down
2 changes: 2 additions & 0 deletions python2-scripts/mcpipy/turtleconsole.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ def inputLine(prompt):
return 'quit()'
elif c.message == ' ':
return ''
elif "__" in c.message:
sys.exit();
else:
return c.message
time.sleep(0.2)
Expand Down
Binary file modified python3-scripts.zip
Binary file not shown.
Loading

0 comments on commit a523e00

Please sign in to comment.