0

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.

  • 1
    you are trying to read the JSON with an ObjectInputStream but an ObjectInputStream is used for serialized java objects created with ObjectOutputStream - they are different things. See: https://stackoverflow.com/questions/22461663/convert-inputstream-to-jsonobject – slipperyseal Jan 29 '18 at 00:07
  • How would I read the data off the socket w/o using ObjectInputStream? – Christian Cameron Jan 29 '18 at 00:13
  • you use socket.getInputStream() - and pass that to something which can parse JSON from it. The link I put in the comment above shows you how to do that. – slipperyseal Jan 29 '18 at 00:42
  • You haven't stated which java JSON library you're using. There's probably something in it that takes an `InputStream` argument and returns json elements. – President James K. Polk Jan 29 '18 at 00:43
  • Thanks guys, seems to have fixed the problem. Any suggestions on tools I can use to simulate pushing data to a socket? At the moment I can't run BlueHydra to get real data. – Christian Cameron Jan 29 '18 at 00:58

1 Answers1

0

You can use BufferedReader to read from server socket. Just replace objectInput stream with bufferedReader. Here is a sample code.

socketReader = new  BufferedReader(new InputStreamReader(socket.getInputStream()));
String str = socketReader.readLine();

Then parse that str using a json library.

Code to change:

        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
        BufferedReader socketReader = 
        new BufferedReader(new InputStreamReader(socket.getInputStream()));
        String message = socketReader.readLine();

            //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);
MD Ruhul Amin
  • 4,386
  • 1
  • 22
  • 37