1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
| import java.io.*; import java.security.MessageDigest; import java.util.zip.Adler32; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; import java.util.Enumeration; import java.util.zip.ZipOutputStream; import java.util.zip.ZipInputStream;
public class enCode { public static String path = "E:\\testShell\\"; public static void main(String[] args) { try { File srcFile = new File(path + "myapp.apk"); d("需要加密的apk大小",String.valueOf(srcFile.length()));
File shellFile = new File(path + "shell.apk"); d("原始壳apk大小",String.valueOf(shellFile.length()));
File shellDexFile = getZipFile(shellFile, "classes.dex"); d("原始壳dex大小",String.valueOf(shellDexFile.length())); byte[] shellDex = getFilebyte(shellDexFile); byte[] enCodedApk = encrpt(getFilebyte(srcFile));
int enCodedApkLen = enCodedApk.length; d("加密后的apk大小",String.valueOf(enCodedApkLen));
int shellDexLen = shellDex.length; d("原始shellDex大小",String.valueOf(shellDexLen));
int toatalLen = enCodedApkLen + shellDexLen + 4; d("合并后shell-encodedApk.dex大小",String.valueOf(toatalLen));
byte[] newDex = new byte[toatalLen]; System.arraycopy(shellDex, 0, newDex, 0, shellDexLen); System.arraycopy(enCodedApk, 0, newDex, shellDexLen, enCodedApkLen); System.arraycopy(intToByte(enCodedApkLen), 0, newDex, toatalLen - 4, 4);
fixNewdexHeaderSize(newDex); d("修复file_size","完成");
fixSHA1Header(newDex); d("修复signature","完成");
fixNewdexHeqaderChecksum(newDex); d("修复checksum","完成"); File outNewDexFile = new File(path + "shell-encodedApk.dex"); outNewDexFile.createNewFile(); FileOutputStream outStream = new FileOutputStream(outNewDexFile); outStream.write(newDex); outStream.flush(); outStream.close(); d("输出","shell-encodedApk.dex");
File news = new File(path + "shell/classes.dex"); news.createNewFile(); FileOutputStream outSteam = new FileOutputStream(news); outSteam.write(newDex); outSteam.flush(); outSteam.close(); d("输出","shell-encodedApk.dex到shell/classes.dex");
File newApk = new File(path + "myapp-encoded.apk"); toZip(path + "shell", new FileOutputStream(newApk), true); d("输出","myapp-encoded.apk");
apksign(path + "myapp-encoded.apk");
} catch(Exception e) {} }
public static void apksign(String filePath) throws Exception { String cmd = "java -jar " + path +"signApk/signapk.jar " + path + "signApk/testkey.x509.pem " + path + "signApk/testkey.pk8 " + filePath + " " + filePath.replace(".apk", "_signed.apk"); d("签名中", cmd); BufferedReader br = null; Process process = Runtime.getRuntime().exec(cmd); InputStream is = process.getInputStream(); br = new BufferedReader(new InputStreamReader(is,"utf-8")); StringBuffer sb = new StringBuffer(); sb.append(br.readLine()); String content; while ((content = br.readLine()) != null) { content = br.readLine(); sb.append(content); } br.close(); br = null; d("签名信息",content); }
public static byte[] fixNewdexHeaderSize(byte[] data) { byte[] newLenData = intToByte(data.length); d("修复file_size值为",Integer.toHexString(data.length)); byte[] refs = new byte[4]; for (int i = 0; i < 4; i++) { refs[i] = newLenData[newLenData.length - 1 - i]; d("-修复为",Integer.toHexString(newLenData[i])); } System.arraycopy(refs, 0, data, 32, 4); return data; } public static byte[] fixSHA1Header(byte[] data) throws Exception { MessageDigest md = MessageDigest.getInstance("SHA-1"); md.update(data, 32, data.length - 32); byte[] newData = md.digest(); System.arraycopy(newData, 0, data, 12, 20); String hexstr = ""; for (int i = 0; i < newData.length; i++) { hexstr += Integer.toString((newData[i] & 0xff) + 0x100, 16).substring(1); } d("-sha1 signature修改为",hexstr); return data; } public static byte[] fixNewdexHeqaderChecksum(byte[] data) { Adler32 adler = new Adler32(); adler.update(data,12,data.length-12); long value = adler.getValue(); byte[] newData = longToByte(value); d("修复checkSum值为",Long.toHexString(value)); byte[] refs = new byte[4]; for (int i = 0; i < 4; i++) { refs[i] = newData[newData.length - 1 - i]; d("-修复为",Long.toHexString(newData[4 + i])); } System.arraycopy(refs, 0, data, 8, 4); return data; } public static byte[] longToByte(long num) { byte[] b = new byte[8]; for (int i = 7; i > 0; i--) { b[i] = (byte)(num%256); num >>= 8; } return b; } public static byte[] intToByte(int num) { byte[] b = new byte[4]; for (int i = 3; i > 0; i--) { b[i] = (byte)(num%256); num >>= 8; } return b; }
public static byte[] encrpt(byte[] data) { return data; }
public static byte[] getFilebyte(File file) throws IOException { byte[] buffer = new byte[1024]; ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); FileInputStream fis = new FileInputStream(file); while(true) { int readCount = -1; if ((readCount = fis.read(buffer)) != -1) { byteArrayOutputStream.write(buffer, 0, readCount); }else { break; } } fis.close(); return byteArrayOutputStream.toByteArray(); }
public static File getZipFile(File file,String zipIn) throws Exception { ZipFile zip = new ZipFile(file); Enumeration<? extends ZipEntry> files = zip.entries(); ZipEntry entry = null; File outFile = null; while (files.hasMoreElements()) { entry = files.nextElement(); d("-解压中",entry.getName());
String outPath = (path +"shell/"+ entry.getName()).replace("\\*", "/"); File outfile = new File(outPath.substring(0,outPath.lastIndexOf("/"))); if (!outfile.exists()) { outfile.mkdirs(); }
InputStream in = zip.getInputStream(entry); OutputStream out = new FileOutputStream(outPath); byte[] buf1 = new byte[1024]; int len; while((len=in.read(buf1))>0) { out.write(buf1,0,len); } in.close(); out.close();
if (entry.getName().equals(zipIn)) { outFile = new File(outPath); } } zip.close(); return outFile; }
public static void toZip(String srcDir, OutputStream out, boolean KeepDirStructure) throws RuntimeException { long start = System.currentTimeMillis(); ZipOutputStream zos = null; try { zos = new ZipOutputStream(out); File sourceFile = new File(srcDir); compress(sourceFile, zos, sourceFile.getName(), KeepDirStructure); long end = System.currentTimeMillis(); d("压缩用时",String.valueOf(end - start) + "ms"); } catch (Exception e) { throw new RuntimeException("zip error from ZipUtils", e); } finally { if (zos != null) { try { zos.close(); } catch (IOException e) { e.printStackTrace(); } } } }
private static void compress(File sourceFile, ZipOutputStream zos, String name, boolean KeepDirStructure) throws Exception { byte[] buf = new byte[2 * 1024]; d("-压缩中",name); if (sourceFile.isFile()) { zos.putNextEntry(new ZipEntry(name.substring(6))); int len; FileInputStream in = new FileInputStream(sourceFile); while ((len = in.read(buf)) != -1) { zos.write(buf, 0, len); } zos.closeEntry(); in.close(); } else { File[] listFiles = sourceFile.listFiles(); if ((listFiles == null || listFiles.length == 0) && !name.equals("shell")) { if (KeepDirStructure) { zos.putNextEntry(new ZipEntry(name + "/")); zos.closeEntry();
} } else { for (File file : listFiles) { if (KeepDirStructure) { compress(file, zos, name + "/" + file.getName(), KeepDirStructure); } else { compress(file, zos, file.getName(), KeepDirStructure); } } } } }
public static void d(String str,String str1) { long time = System.currentTimeMillis(); System.out.println("["+time+"] "+str+":"+str1); } }
|