[转帖]WebLogic密码恢复(二)_ 2_Tomcat, WebLogic及J2EE讨论区_Weblogic技术|Tuxedo技术|中间件技术|Oracle论坛|JAVA论坛|Linux/Unix技术|hadoop论坛_联动北方技术论坛  
网站首页 | 关于我们 | 服务中心 | 经验交流 | 公司荣誉 | 成功案例 | 合作伙伴 | 联系我们 |
联动北方-国内领先的云技术服务提供商
»  游客             当前位置:  论坛首页 »  自由讨论区 »  Tomcat, WebLogic及J2EE讨论区 »
总帖数
1
每页帖数
101/1页1
返回列表
0
发起投票  发起投票 发新帖子
查看: 2907 | 回复: 0   主题: [转帖]WebLogic密码恢复(二)_ 2        下一篇 
songjian
注册用户
等级:上尉
经验:711
发帖:60
精华:0
注册:2012-11-8
状态:离线
发送短消息息给songjian 加好友    发送短消息息给songjian 发消息
发表于: IP:您无权察看 2012-11-12 10:10:02 | [全部帖] [楼主帖] 楼主

另外,原来Apache上也有一个类似的东西,原链接为 http://geronimo.apache.org/apidocs/2.0.1/src-html/org/apache/geronimo/converter/bea/Weblogic81Utils.html。内容如下(以下代码把decryptString()改为public了):

北京联动北方科技有限公司北京联动北方科技有限公司代码

package org.apache.geronimo.converter.bea;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
import java.util.Properties;
import java.util.Iterator;
import java.lang.reflect.Method;
import java.lang.reflect.InvocationTargetException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.StringWriter;
import java.io.PrintWriter;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLClassLoader;
import java.net.URL;
public class Weblogic81Utils {
private final static Pattern ENCRYPTED_STRING= Pattern.compile("\"\{\S+\}\S+?\"");
      private Object decoder;
      private Method decode;
      private Object decrypter;
      private Method decrypt;
      private File domainDir;
      public Weblogic81Utils(String libDirPath, String domainDirPath) {
            File libDir= new File(libDirPath);
            if(!libDir.exists() !libDir.canRead() !libDir.isDirectory())throw new IllegalArgumentException("Bad weblogic lib dir");
            File weblogicJar= new File(libDir,"weblogic.jar");
            File securityJar= new File(libDir,"jsafeFIPS.jar");
            if(!weblogicJar.canRead())throw new IllegalArgumentException("Cannot find JARs in provided lib dir");
            domainDir= new File(domainDirPath);
            if(!domainDir.exists() !domainDir.canRead() !domainDir.isDirectory())throw new IllegalArgumentException("Bad domain directory");
            File state= new File(domainDir,"SerializedSystemIni.dat");
            if(!state.canRead())throw new IllegalArgumentException("Cannot find serialized state in domain directory");
            try {
      ClassLoader loader= new URLClassLoader(securityJar.exists()? new URL[]{weblogicJar.toURL(), securityJar.toURL()} :new URL[]{weblogicJar.toURL()}, Weblogic81Utils.class.getClassLoader());
            initialize(loader, state);
      }catch (Exception e) {
      throw (RuntimeException)new IllegalArgumentException("Unable to initialize encryption routines from provided arguments").initCause(e);
}
}
public Properties getBootProperties() {
      File boot= new File(domainDir,"boot.properties");
      FileInputStream bootIn= null;
      try {
            bootIn= new FileInputStream(boot);
      }catch (FileNotFoundException e) {
            return null;
      }
      try {
            Properties props= new Properties();
            props.load(bootIn);
            bootIn.close();
            for (Iterator it= props.keySet().iterator(); it.hasNext();) {
                  String key= (String) it.next();
                  String value= props.getProperty(key);
                  if(value!= null && value.startsWith("{")) props.setProperty(key, decryptString(value));
                  }
                  return props;
            }catch (Exception e) {
            return null;
      }
}
public String getConfigXML()throws FileNotFoundException {
      File config= new File(domainDir,"config.xml");
      BufferedReader in= new BufferedReader(new FileReader(config));
      StringWriter string= new StringWriter();
      PrintWriter out= new PrintWriter(string);
      String line;
      Matcher m= ENCRYPTED_STRING.matcher("");
      try {
            while((line= in.readLine())!= null) {
                  m.reset(line);
                  int last= -1;
                  while(m.find()) {
                        out.print(line.substring(last+1, m.start()));
                        String s= line.substring(m.start(), m.end());
                        out.print(""");
                        out.print(decryptString(s.substring(1, s.length()-1)));
                        out.print(""");
                        last= m.end()-1;
                  }
                  if(last== -1) {
                        out.println(line);
                  }else {
                  if(line.length()> last+1) {
                        out.print(line.substring(last+1));
                  }
                  out.println();
            }
            out.flush();
      }
      in.close();
      out.close();
}catch (Exception e) {
return null;
}
return string.getBuffer().toString();
}
private void initialize(ClassLoader loader, File state)throws IOException, IllegalAccessException, NoSuchMethodException, InvocationTargetException, ClassNotFoundException, InstantiationException {
      byte[] salt= null, key= null;
      FileInputStream in= new FileInputStream(state);
      salt= readBytes(in);
      int i= in.read();
      if(i!= -1) {
            if(i!= 1)throw new IllegalStateException();
            key= readBytes(in);
      }
      in.close();
      decrypter= getEncryptionService(loader, salt, key);
      decoder= loader.loadClass("weblogic.utils.encoders.BASE64Decoder").newInstance();
decode= decoder.getClass().getMethod("decodeBuffer",new Class[]{String.class});
decrypt= decrypter.getClass().getMethod("decryptString",new Class[]{byte[].class});
}
private static byte[] readBytes(InputStream in)throws IOException {
      int len= in.read();
      if(len< 0)
      throw new IOException("stream is empty");
      byte result[]= new byte[len];
      int index= 0;
      while(true) {
            if(index>= len) {
                  break;
            }
            int count= in.read(result, index, len- index);
            if(count== -1)
            break;
            index+= count;
      }
      return result;
}
public String decryptString(String string)throws IllegalAccessException, InvocationTargetException {
if(string.indexOf('}')> -1) {
string= string.substring(string.indexOf("}")+1);
}
return (String) decrypt.invoke(decrypter,new Object[]{decode.invoke(decoder,new Object[]{string})});
}
static Object getEncryptionService(ClassLoader loader,byte salt[],byte key[])throws NoSuchMethodException, ClassNotFoundException, IllegalAccessException, InvocationTargetException {
      String magic= "0xccb97558940b82637c8bec3c770f86fa3a391a56";
      Object factory= loader.loadClass("weblogic.security.internal.encryption.JSafeEncryptionServiceImpl").getMethod("getFactory",new Class[0]).invoke(null,null);
Method getter= factory.getClass().getMethod("getEncryptionService",new Class[]{byte[].class, String.class,byte[].class});
return getter.invoke(factory,new Object[]{salt, magic, key});
}
}




赞(0)    操作        顶端 
总帖数
1
每页帖数
101/1页1
返回列表
发新帖子
请输入验证码: 点击刷新验证码
您需要登录后才可以回帖 登录 | 注册
技术讨论