redis 获取字节码 反序列化

来源:互联网 发布:有什么软件推广 编辑:程序博客网 时间:2024/04/27 22:20

我们有时候调式服务器(redis)的字节码,但是不知道具体对象,内容。


出现的都是这样一个东西,比如:




上面的\x表示十六进制开头的值,\r \n 表示回车换行,还有其他的字符数字等(这些字节能直接转成字符)。


下面通过java代码反序列化处理,code如下:


package com.hongbao.user;


import com.hongbao.dal.model.User;
import com.sun.xml.internal.messaging.saaj.util.ByteInputStream;
import org.apache.commons.io.FileUtils;


import java.io.*;
import java.util.List;
import java.util.Queue;


/**
 * Created by shengshan.tang on 8/1/2015 at 3:51 PM
 */
public class RedisUser {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        String content = FileUtils.readFileToString(new File("d:/abc.txt"),"utf-8");
        int index = 0;
        List<Byte> byteList = new java.util.ArrayList<Byte>();
        while(index < content.length()){


            String s = content.substring(index, index + 1);
            if(s.equals("\\")){    //需要转换的
                s = content.substring(index, index + 2);
                if(s.equals("\\x")){  //十六进制
                    index+=2;   //一般占两个字符
                    String str = content.substring(index,index+2);
                    byte devBin = (byte) Integer.parseInt(str, 16);   //转成字节
                    byteList.add(devBin);
                    index+=2;
                }else if(s.startsWith("\\r") || s.startsWith("\\n") || s.startsWith("\\t") || s.startsWith("\\b")){  //特殊转义字符
                    String str = content.substring(index,index+2);
                    if(s.startsWith("\\r")){
                        str = "0D";
                    }
                    if(s.startsWith("\\n")){
                        str = "0A";
                    }
                    if(s.startsWith("\\t")){
                        str = "09";
                    }
                    if(s.startsWith("\\b")){
                        str = "08";
                    }
                    byte devBin = (byte) Integer.parseInt(str, 16);
                    byteList.add(devBin);
                    index+=2;
                }


            }else{   //直接转成字节
                s = content.substring(index, index + 1);
                byte bytes []= s.getBytes("UTF-8");
                for(byte b : bytes){
                    byteList.add(b);
                }
                index++;
            }
        }
        byte [] bytes = new byte[byteList.size()];
        int n = 0;
        for(byte b : byteList){
            bytes[n] = b;
            n++;
        }
        ByteInputStream bis = new ByteInputStream(bytes,byteList.size());
        ObjectInputStream os = new ObjectInputStream(bis);
        User user = (User) os.readObject();
        System.out.println(user.getId()+" "+user.getUserName());


    }



}


运行下,看结果


0 0
原创粉丝点击