android如何防止注入呢?

Home / Android MrLee 2015-7-14 8763

本博客前面写有注入方法,学会注入的朋友可能就想保护好自己的程序不被其它程序注入、修改。在此,说说android如何防止注入。 要防注入,肯定是要先防ptrace,注入是通过它完成的,如果把它干掉的话那么就不存在注入啦。那么总结有以下三种方法 1.ptrace附加失败。
2.修改linker中的dlopen函数,防止第三方so加载。
3.定时检测应用加载的第三方so库,如果发现是被注入的so,卸载加载的so。 这里主要分析第三种情况。
java关键函数如下:
//该函数搜索进程PID被注入的第三方so。
public static HashSet getSoList(int pid, String pkg) 
{
	HashSet temp = new HashSet();
	File file = new File("/proc/" + pid + "/maps");
	if (!file.exists()) 
	{
	    return temp;
	}
	try 
	{
		BufferedReader bufferedReader = new BufferedReader(
		new InputStreamReader(new FileInputStream(file)));
		String lineString = null;
		while ((lineString = bufferedReader.readLine()) != null) 
		{
			String tempString = lineString.trim();
			if (tempString.contains("/data/data")
			&& !tempString.contains("/data/data/" + pkg)) 
			{
				int index = tempString.indexOf("/data/data");
				temp.add(tempString.substring(index));
			}
		}
		bufferedReader.close();
	} 
	catch (FileNotFoundException e) 
	{
		//TODO Auto-generated catch block
		e.printStackTrace();
	} 
	catch (IOException e) 
	{
		//TODO Auto-generated catch block
		e.printStackTrace();
	}
	return temp;
}
//卸载加载的so库
public static native void uninstall(String soPath);
jni实现关键代码如下:
//该方法主要根据so路径,调用dlopen卸载加载的so,需要调用多次,分析linker的dlclose可知,只有so的引用计数为0时,才会完全卸载。
static void com_hook_util_UninstallSo_uninstall(JNIEnv *env, jclass,
		jstring path)
{
	const char* chars = env->GetStringUTFChars(env, path);
	void* handle = dlopen(chars, RTLD_NOW);
	int count = 4;
	int i = 0;
	for (i = 0; i < count; i++)
	{
		if (NULL != handle)
		{
			dlclose(handle);
		}
	}
}
本文参考神乎博客

本文链接:https://www.it72.com/3709.htm

推荐阅读
最新回复 (0)
返回