检测目标程序ELF bit是32还是64

时间:2022-09-01 00:03:04

android操作系统在5.0之后加入了对64位程序的支持,同时兼容运行32位的进程

android的进程绝大部分是zygote父进程fork出来的子进程

zygote进程fork出来的进程是32位进程

zygote64进程fork出来的进程是64位进程

但是有一些在zygote启动之前的进程,那就是init进程fork出来的,都属于64bit进程

zygote进程在fork出子进程之后,更改了进程的名字(setNiceName)

如果想根据进程名找到进程文件,通过读取elf头的方法来判断目标进程的elf bit

不是太理想

通过man proc查阅文档可以知道,解析目标进程的auxv文件可以判断elf bit

内核支持从2.6开始,android的内核版本在这之后,检测auxv文件判断elf bit是可靠的

先上makefile,Application.mk

APP_ABI := arm64-v8a
APP_PLATFORM := android-

只编译64位的版本,如果系统无法跑64位程序,证明整个系统都是32位的

Android.mk

# Copyright (C)  The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS)
LOCAL_MODULE := auxv
LOCAL_SRC_FILES := auxv.c
LOCAL_ARM_MODE := arm
include $(BUILD_EXECUTABLE)

源码auxv.c

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h> /*
* Parsing auxv: if you parse the auxv structure yourself (not relying on the dynamic loader), then there's
* a bit of a conundrum: the auxv structure follows the rule of the process it describes, so sizeof(unsigned
* long) will be 4 for 32-bit processes and 8 for 64-bit processes. We can make this work for us. In
* order for this to work on 32-bit systems, all key codes must be 0xffffffff or less. On a 64-bit system,
* the most significant 32 bits will be zero. Intel machines are little endians, so these 32 bits follow
* the least significant ones in memory. * As such, all you need to do is: * 1. Read 16 bytes from the `auxv` file.
* 2. Is this the end of the file?
* 3. Then it's a 64-bit process.
* 4. Done.
* 5. Is buf[4], buf[5], buf[6] or buf[7] non-zero?
* 6. Then it's a 32-bit process.
* 7. Done.
* 8. Go to 1.
*/ int check_auxv(int pid)
{
if(pid < )
{
printf("invalid process id\n");
return -;
} char auxv[];
snprintf(auxv, , "/proc/%d/auxv", pid);
int fd = open(auxv, O_RDONLY);
if(fd < )
{
printf("%s does not exist\nprocess %d maybe running in 32 bit elf mode", auxv, pid);
return ;
}
const int SIZE = ;
char buf[SIZE];
do {
int nread = read(fd, buf, SIZE);
if(nread < SIZE)
{
printf("process %d running in 64 bit elf mode\n", pid);
break;
} if(buf[] && buf[] && buf[])
{
printf("process %d running in 32 bit elf mode @ line %d\n", pid, __LINE__);
printf("%x, %x, %x, %x\n", buf[], buf[], buf[], buf[]);
break;
}
else if(buf[])
{
printf("process %d running in 32 bit elf mode\n", pid);
break;
}
} while(); close(fd); return ;
} int main(int argc, char* argv[])
{
if(argc <= )
{
printf("usage:auxv pid1 [pid2 pid3 ...]\n");
return ;
}
int i = ;
for(i = ; i < argc; ++i)
{
int pid = atoi(argv[i]);
check_auxv(pid);
} return ;
}

编译命令

call ndk-build NDK_PROJECT_PATH=. NDK_APPLICATION_MK=./Application.mk

目录结构

|---Application.mk
|---build.bat
|---jni
||---Android.mk
||---auxv.c

检测目标程序ELF bit是32还是64的更多相关文章

  1. dll文件32位64位检测工具以及Windows文件夹SysWow64的坑

    自从操作系统升级到64位以后,就要不断的需要面对32位.64位的问题.相信有很多人并不是很清楚32位程序与64位程序的区别,以及Program Files (x86),Program Files的区别 ...

  2. dll文件32位64位检测工具以及Windows文件夹SysWow64的坑(很详细,还有自动动手编程探测dll)

    阅读目录 dll文件不匹配导致数据库无法启动 究竟是System32还是SysWow64 区分dll文件32位64位的程序让我倍感迷惑 再次判断究竟是System32还是SysWow64——意想不到的 ...

  3. dll文件32位64位检测工具以及Windows文件夹SysWow64的坑【转发】

    原文地址:http://www.cnblogs.com/hbccdf/archive/2014/03/09/3590916.html 自从操作系统升级到64位以后,就要不断的需要面对32位.64位的问 ...

  4. &lbrack;转&rsqb;oracle odp&period;net 32位&sol;64位版本的问题

    本文转自:http://www.cnblogs.com/yjmyzz/archive/2011/04/19/2020793.html 如果你的机器上安装了odp.net,且确信machine.conf ...

  5. 错误&colon; 未能完成程序集的安装&lpar;hr &equals; 0x8007000b&rpar;&comma;&period;net程序关于使用Oracle&period;DataAccess&period;dll不同版本x86和x64问题,即oracle odp&period;net 32位&sol;64位版本的问题

    如果你的机器上安装了odp.net,且确信machine.config也有类似以下结节:(64位+.net 4.0环境下,machine.config可能会有4份,分别对应于.net2.0/4.0的3 ...

  6. &lbrack;转载&rsqb;使用32位64位交叉编码混淆来打败静态和动态分析工具 - wildsator

    0×00 摘要 混淆是一种能增加二进制分析和逆向工程难度与成本的常用技术.主流的混淆技术都是着眼于使用与目标CPU相同的机器代码,在相同的处理器模式下,隐藏代码并进行控制.本文中引入了一种新的混淆方法 ...

  7. 最新Internet Download Manager &lpar;IDMan&rpar; 6&period;25 Build 20 32位 64位注册破解补丁

    0x00 IDMan介绍 Internet Download Manager提升你的下载速度最多达5倍,安排下载时程,或续传一半的软件.Internet Download Manager的续传功能可以 ...

  8. 笔记:C语言数据类型在32位与64位机器上的字节数

    读<深入理解计算机系统> 第二章 信息的表示与处理 32位与64位的典型值,单位字节 声明 32位机器 64位机器 char 1 1 short int int 4 4 long int ...

  9. Win7 下用 VS2015 编译最新 openssl(1&period;0&period;2j)包含32、64位debug和release版本的dll、lib(8个版本)

    Win7 64位系统下通过VS2015编译好的最新的OpenSSL(1.0.2j)所有八个版本的链接库, 包含以下八个版本: 1.32位.debug版LIB: 2.32位.release版LIB: 3 ...

随机推荐

  1. ASP&period;NET MVC5--添加验证

    1.在Model类里面添加验证,代码如下: public class Movie { public int ID { get; set; } [StringLength(,MinimumLength= ...

  2. hdu 2036

    Ps:  - -感觉这道题完全就是数学题...就是求知道每个顶点的坐标,然后求这个多边形的面积... 代码:#include "stdio.h"#include "std ...

  3. 大数据导入Excel

    在平时的项目中,将数据导出到Excel的需求是很常见的,在此对一些常见的方法做以总结,并提供一种大数据量导出的实现. OLEDB   使用OLEDB可以很方便导出Excel,思路很简单,处理时将Exc ...

  4. sqlserver2012 评估期已过问题处理

    由于之前安装sqlserver2012忘记输入序列号,现在出现评估期已过的问题,网上忙活半天,才解决,发现网上叙述都很凌乱,而且只有大意,新手很难操作,所以把我操作的过程分享给大家 1 开始菜单找到s ...

  5. Linux socket 类封装 (面向对象方法)

    /* * socketfactory.h * * Created on: 2014-7-19 * Author: root */ #ifndef SOCKETFACTORY_H_ #define SO ...

  6. Solr 16 - 增删改Solr中索引数据的几种方式 &lpar;在URL上或Web页面中操作&rpar;

    目录 1 添加/更新索引数据 1.1 JSON格式的操作 1.2 XML格式的操作 2 删除索引数据 2.1 删除符合特定条件的数据 2.2 删除指定ID的数据 2.3 删除全部索引数据 3 在doc ...

  7. Swift3 使用系统UIAlertView方法做吐司效果

    /** *显示弹出信息 */ class func showAlertMessage(_ str:String,showtime Num:Double){ let alert = UIAlertVie ...

  8. HDU 5489 二分 LIS

    Removed Interval Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...

  9. CentOS 文件特殊权限SUID&comma;SGID&comma;SBIT

    1.SUID ,是一种对二进制程序进行设置的特殊权限,可以让二进制程序的执行者临时拥有所有者的权限(仅对拥有执行权限的二进制程序有效). (1)SUID权限仅对二进制程序有效: (2)本权限仅在执行该 ...

  10. Git 更改远程地址

    查看远程地址 git remote -v 更换远程地址 git remote set-url origin 新的地址 更换之后可以查看一下 这个非常实用. 通常我们把gitlab服务器更换的时候,对应 ...