I am trying to read in the data written to a socket from the open source project BlueHydra --> https://github.com/pwnieexpress/blue_hydra
Here is the code that writes the json data to the socket, found here:
# write json data to result socket
TCPSocket.open('127.0.0.1', 8244) do |sock|
sock.write(json)
sock.write("\n")
sock.flush
end
Now, I have a java program that attempts to read that json data and write it to a local mongodb. Here is the meat of the code that does that:
public class ReadHydraOutput {
//static ServerSocket variable
private static ServerSocket server;
//socket server port on which it will listen
private static int port = 8244;
public static void main(String args[]) {
try{
//create the socket server object
server = new ServerSocket(port);
//keep listening indefinitely until receives 'exit' call or program terminates
while(true){
System.out.println("Waiting for blue hydra client output in json");
//creating socket and waiting for client connection
Socket socket = server.accept();
//read from socket to ObjectInputStream object
ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
//convert ObjectInputStream object to String
String message = (String) ois.readObject();
System.out.println("Parsing the Json message received from blue hydra server port 8244: " + message);
try {
String jsonValue = "";
HashMap<String, String> hawksMap = new HashMap<String, String>();
JSONObject jsonObj = new JSONObject(message);
//parse for values
String[] jsonNames = JSONObject.getNames(jsonObj);
for (int i=0; i < jsonNames.length; i++) {
if (jsonNames[i].equalsIgnoreCase("data")) {
JSONObject jsonObjData = (JSONObject)jsonObj.get(jsonNames[i]);
//parse again
String[] jsonDataNames = JSONObject.getNames(jsonObjData);
for (int j=0; j < jsonDataNames.length; j++) {
if (jsonDataNames[j].equalsIgnoreCase("le_rssi")) {
//nested within data field name
JSONObject jsonObjRssi = (JSONObject)jsonObjData.get(jsonDataNames[j]);
String[] jsonrssiNames = JSONObject.getNames(jsonObjRssi);
for (int k=0; k < jsonrssiNames.length; k++) {
jsonValue = (String) jsonObjRssi.get(jsonrssiNames[k]);
hawksMap.put(jsonrssiNames[k], jsonValue);
}
} else {
jsonValue = (String) jsonObjData.get(jsonDataNames[j]);
hawksMap.put(jsonDataNames[j], jsonValue);
}
}
} else {
jsonValue = (String) jsonObj.get(jsonNames[i]);
hawksMap.put(jsonNames[i], jsonValue);
}
}
When I run the java program and start BlueHydra, I get the following error message:
Waiting for blue hydra client output in json
Error in ReadHydraOutput: invalid stream header: 47455420
java.io.StreamCorruptedException: invalid stream header: 47455420
at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:857)
at java.io.ObjectInputStream.<init>(ObjectInputStream.java:349)
at ReadHydraOutput.main(ReadHydraOutput.java:43)
I have a feeling that something is off with the way I used ObjectInputStream, but I can't pinpoint the error.