一、问题情况
因为业务上传服务器有apk包,需要收集整理apk包的包名,获取权限,版本号,md5等数据。找了个aapt.exe,liunx系统下没法运行,需要先装wine内核才能执行exe程序。
二、解决方法
2.1 安装wine教程
wine32和wine64共存编译过程如下:
安装依赖
1 2 3 4 5 6 7 |
yum groupinstall 'Development Tools' yum install libX11-devel freetype-devel zlib-devel libxcb-devel yum install alsa-lib-devel.i686 libsndfile-devel.i686 readline-devel.i686 glib2.i686 glibc-devel.i686 libgcc.i686 libstdc++-devel.i686 pulseaudio-libs-devel.i686 cmake audiofile-devel.i686 freeglut-devel.i686 lcms-devel.i686 libieee1284-devel.i686 openldap-devel.i686 unixODBC-devel.i686 sane-backends-devel.i686 fontforge libgphoto2-devel.i686 isdn4k-utils-devel.i686 mesa-libGL-devel.i686 mesa-libGLU-devel.i686 libXxf86dga-devel.i686 libXxf86vm-devel.i686 giflib-devel.i686 cups-devel.i686 gsm-devel.i686 libv4l-devel.i686 fontpackages-devel ImageMagick-devel.i686 libX11-devel.i686 docbook-utils-pdf libtextcat tex-cm-lgc yum install alsa-lib-devel audiofile-devel.i686 audiofile-devel cups-devel.i686 cups-devel dbus-devel.i686 dbus-devel fontconfig-devel.i686 fontconfig-devel freetype.i686 freetype-devel.i686 freetype-devel giflib-devel.i686 giflib-devel lcms-devel.i686 lcms-devel libICE-devel.i686 libICE-devel libjpeg-turbo-devel.i686 libjpeg-turbo-devel libpng-devel.i686 libpng-devel libSM-devel.i686 libSM-devel libusb-devel.i686 libusb-devel libX11-devel.i686 libX11-devel libXau-devel.i686 libXau-devel libXcomposite-devel.i686 libXcomposite-devel libXcursor-devel.i686 libXcursor-devel libXext-devel.i686 libXext-devel libXi-devel.i686 libXi-devel libXinerama-devel.i686 libXinerama-devel libxml2-devel.i686 libxml2-devel libXrandr-devel.i686 libXrandr-devel libXrender-devel.i686 libXrender-devel libxslt-devel.i686 libxslt-devel libXt-devel.i686 libXt-devel libXv-devel.i686 libXv-devel libXxf86vm-devel.i686 libXxf86vm-devel mesa-libGL-devel.i686 mesa-libGL-devel mesa-libGLU-devel.i686 mesa-libGLU-devel ncurses-devel.i686 ncurses-devel openldap-devel.i686 openldap-devel openssl-devel.i686 openssl-devel zlib-devel.i686 pkgconfig sane-backends-devel.i686 sane-backends-devel xorg-x11-proto-devel glibc-devel.i686 prelink fontforge flex bison libstdc++-devel.i686 pulseaudio-libs-devel.i686 gnutls-devel.i686 libgphoto2-devel.i686 isdn4k-utils-devel.i686 gsm-devel.i686 samba-winbind libv4l-devel.i686 cups-devel.i686 libtiff-devel.i686 gstreamer-devel.i686 gstreamer-plugins-base-devel.i686 gettext-devel.i686 |
下载wine
1 2 3 4 |
mkdir wine cd wine wget https://master.dl.sourceforge.net/project/wine/Source/wine-1.8.4.tar.bz2 tar -xf wine-1.8.4.tar.bz2 |
这里将wine下载到了/root/wine文件夹内
安装wine
这里使用源码编译,根据电脑配置不同,编译时间也不同
1 2 3 4 5 6 7 8 9 10 11 12 |
mkdir -p /root/wine/wine64 (我这里故意写了个绝对路径) cd /root/wine/wine64 ../wine-1.8.4/configure --enable-win64 make -j 4 (-j 4根据cpu数量填写,要是不懂就直接make) cd .. mkdir wine32 cd wine32 ../wine-1.8.4/configure --enable-win32 make -j 4 make install cd ../wine64 make install |
至此wine安装完成,可以直接使用wine,winecfg,winefile命令
如果需要运行Windows软件的话,需要完善软件所需的环境。
附:查看CPU方法:
直接使用命令lscpu显示CPU简要信息
也可以用cat /proc/cpuinfo
以上安装方法转载:https://blog.whsir.com/post-592.html
2.2 使用aapt.exe读取apk包信息
服务器环境是lnmp
首先写一个用于读取包信息的php文件getapkinfo.php(传参用apkName写apk文件名,platForm写上传目录里的相对目录,上传目录代码里写死了/data/www/down,有需要的可以自己加参指定上传目录.)
例子:curl https://www.1987619.com/getapkinfo.php?apkName=abc.apk&platForm=az(请求的就是/data/www/down/az/abc.apk这个包的apkinfo信息)
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 |
vim getapkinfo.php <?php $apkName=escapeshellcmd($_GET['apkName']);//需要获取的apk文件名 $platForm=escapeshellcmd($_GET['platForm']);//需要获取的apk文件所在上传目录下的相对目录 $dir = "/data/www/down";//请写apk包所在上传目录 $nowdir = dirname(__FILE__)."/"; $fileurls = $dir . "/".$platForm."/".$apkName ; $filesize = filesize ( $fileurls ); $sh =$nowdir.$apkName.".sh"; //print_r ($nowdir.$apkName.".sh"); $infourls =$nowdir.$apkName .".txt"; //print_r ($nowdir.$apkName.".txt"); $message = "#!/bin/bash" . "\n" . "cd $nowdir" . "\n" . "/usr/local/bin/wine aapt.exe dump badging \"$fileurls\" Manifest.xml >$infourls"; $handle = fopen ( $sh, 'w' ); fwrite ( $handle, $message ); fclose ( $handle ); chmod ( $sh, 0777 ); exec($sh); // 获取apk信息 $apkinfo = get_apkinfo ( $infourls ); $apkinfo ['size'] = !$filesize?'0':$filesize; $apkinfo ['md5'] = !md5_file($fileurls)?'':md5_file($fileurls); print_r(json_encode($apkinfo)); //删除apk临时信息文件 @unlink ( $sh ); @unlink ( $infourls ); function get_apkinfo($infourls) { $package_case = 'package: '; $name = ''; $versionCode = ''; $versionName = ''; $application_case = 'application:'; $label = '程序名'; $sdkVersion_case = 'sdkVersion:'; $sdkVersion = '3'; $sdkVersion_value = array ( '0' => '1.0', '1' => '1.0', '2' => '1.1', '3' => '1.5', '4' => '1.6', '5' => '2.0', '6' => '2.0', '7' => '2.1', '8' => '2.2', '9' => '2.3', '10' => '2.3', '11' => '3.0', '12' => '3.1', '13' => '3.2', '14' => '4.0', '15' => '4.0', '16' => '4.1', '17' => '4.2', '18' => '4.3', '19' => '4.4', '20' => '4.4', '21' => '5.0', '22' => '5.1', '23' => '6.0' ); $uses_permission_case = 'uses-permission:'; $uses_permission = array (); $uses_permission_item = 0; $screen_case = 'supports-screens:'; $densities_case = 'densities:'; if (file_exists ( $infourls )) { $data = file ( $infourls ); // 读取全部内容; foreach ( $data as $i => $line ) { $line = ' ' . $line; if (strpos ( $line, $package_case )) { $name = cut ( $line, "name='", "'" ); $versionCode = cut ( $line, "versionCode='", "'" ); $versionName = cut ( $line, "versionName='", "'" ); } else if (strpos ( $line, $application_case )) { $label = cut ( $line, "label='", "'" ); $icon = cut ( $line, "icon='", "'" ); } else if (strpos ( $line, $sdkVersion_case )) { $sdkVersion = cut ( $line, "sdkVersion:'", "'" ); } else if (strpos ( $line, $uses_permission_case )) { $uses_permission [$uses_permission_item ++] = cut ( $line, "uses-permission:'", "'" ); } else if (strpos ( $line, $screen_case )) { $screen = cut ( $line, "supports-screens: '", "'\r" ); $tmp = explode ( "' '", $screen ); $screens = join ( "|", $tmp ); } else if (strpos ( $line, $densities_case )) { $densities = cut ( $line, "densities: '", "'\r" ); $tmp = explode ( "' '", $densities ); $densities = join ( "|", $tmp ); } } $apkinfo = array ( // 'label' => iconv ( 'UTF-8', 'GBK', $label ), // 软件名称 // 'icon' => $icon,//软件图标 'packname' => $name, // 包名 'versioncode' => $versionCode, // 版本号 'version' => $versionName, // 版本名称 // 'sdkcode' => $sdkVersion, // 软件系统编号 // 'sdkversion' => $sdkVersion_value [$sdkVersion], // 软件系统 'permissionno' => $uses_permission_item, // 权限数 'permission' => join ( '|', $uses_permission ), // 权限列表 // 'screens' => $screens, // 分辨率 // 'densities' => $densities // 屏幕密度 ) ; } return $apkinfo; } function cut($file, $from, $end) { $message = explode ( $from, $file ); $message = explode ( $end, $message [1] ); return $message [0]; } ?> |
getapkinfo.php写好以后,执行时会在getapkinfo.php所在目录临时生成apk包信息文件,所以目录要有可写权限才行.
php还需要开放exec命令
1 2 3 4 5 6 7 |
vim /usr/local/php/etc/php.ini 找到disable_functions 删除后面的exec然后保存 重启php-fpm service php-fpm restart |
至此,就可以通过网页接口获取服务器apk包信息,如果有些apk加密过,可能无法读取出apk信息.