문제

I have an object which is serialised and written to a file.

Before de serialising the file back into an object instance, I want to maliciously edit the txt in the file.

    //FILE TAMPER
    //Lexical block: Tamper
    {
        String output = null;
        //Lexical block make output
        {
            LinkedList<String> lls = new LinkedList<String>();
            //Lexical block: Reader
            {
                BufferedReader br = new BufferedReader(new FileReader(fileString));
                while (br.ready()) {
                    String readLine = br.readLine();
                    lls.add(readLine);
                }
                br.close();
            }
            //Lexical block: manipulate
            {
                //Henry Crapper
                final String[] llsToArray = lls.toArray(new String[lls.size()]);
                for (int i = 0; i < llsToArray.length; i++) {
                    String line = llsToArray[i];
                    if (line.contains("Henry")) {
                        line = line.replace("Henry",
                                            "Fsekc");
                        llsToArray[i] = line;
                    }
                    if (line.contains("Crapper")) {
                        line = line.replace("Crapper",
                                            "Dhdhfie");
                        llsToArray[i] = line;
                    }

                    lls = new LinkedList<String>(Arrays.asList(llsToArray));
                }
            }

            //Lexical block: write output
            {
                StringBuilder sb = new StringBuilder();
                for (String string : lls) {
                    sb.append(string).append('\n');
                }
                output = sb.toString();
            }
        }
        //Lexical block: Writer
        {
            BufferedWriter bw = new BufferedWriter(new FileWriter(fileString));
            bw.write(output);
            bw.close();
        }
    }

However the edited file isn't correct and has some unusual characters.

//Before
¨Ìsr&Snippets.Parsed.EmployeeSerialization0I
bankBalanceLnametLjava/lang/String;xp•Åt
Henry Crappe

//After
ÔøΩÔøΩsr&Snippets.Parsed.EmployeeSerialization0I
bankBalanceLnametLjava/lang/String;xpÔøΩÔøΩt
Fsekc Dhdhfie

I'm guessing there is some sort of non readable character issue or something?


Answer continued in a new question is here

도움이 되었습니까?

해결책

A file which contains a serialized object instance is a binary file: you should not edit it with a BufferedWriter. Edit it with a RandomAccessFile, for example.

If you are wondering of why, the charset used in a Writer could not map one-to-one with a byte. Saving all the file would change also unexpected positions.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top