How to grant root permissions to Android applications

Source: Internet
Author: User
Tags dmesg



Write this articleArticleFirst, thank you
Simon_fu

, His two articles about
Root
This article is a supplement to his article.
Simon_fu
See the following two web pages:

AndroidProgramSecurity System


AndroidApplication acquisition

RootPermission


 


generally,
Android
the maximum permission that can be obtained by the application under" direct "is
System
, however, if we need to execute some operations in the program,
root
permission commands, such as
ifconfig
,
root
the permission is granted. Follow
Simon
as mentioned in the article, applications can obtain
root
permission:

1)

implement a
init

implement a
service

to help
Android

application execution
root

permission command.

2)



Implement a virtual device. This device helps
Android

Application Execution
Root

Permission command.

 

Second, I did not try it here. Here we will talk about the process of implementing the first method and some problems I encountered.

 

1.



Write the commands we want to execute as scripts or executable programs.

Below is my script
Ifconfig_test.sh
:

#
!
/System/bin/sh

Ifconfig

Note:
The first line of the script must be
#
!
/System/bin/sh
Otherwise, it cannot be executed.
Dmesg
You can see that the information content is
Cannot execve./ifconfig_test.sh: exec format Error

 

You can also use
C/C ++
Compile the command or program to be executed and compile
Image
Compile the program into an executable program.

 

2.

in
init. RC

moderate registration
service

Android
In
Service
You must
Init. RC
,
Init. RC
Defined in
Service
Will be
Init
Process Creation.
Root
Permission. After you get the corresponding notification (through attribute settings,
Init
The process starts
Service
.

The content registered in this article is as follows:

Service ifconfig_test/system/etc/ifconfig_test.sh

Oneshot

Disabled

Where,
Oneshot
Indicates that the program will not be restarted after exiting,
Disabled
It indicates that it is not started when the system is started.

 

Note:
Here
Service name
Cannot exceed
16
Characters. My previous
Service name
Because the definition is long,
18
Characters, set the property notification
Service
View after startup
Dmesg
You can see the prompt:
Init: no such service
. View
/System/CORE/init/parser. c
OfSource code, In
Parse_service-> valid_name
The function can see the following content:
If (strlen (name)> 16) {return 0 ;}
, Proof
Service
The name cannot exceed
16
Characters.

 

3.


Android

Upgrade the application to
System

permission

Since the application can be started
Service
Obtain
Root
Permissions, so it is not very insecure.
Android
With this in mind, only
System
Only applications with permissions can set properties, notification
Service
Start. About upgrading
System
There are many permission articles on the Internet, so I will not elaborate on them here. I can refer to the following two articles:

Http://blog.csdn.net/liujian885/archive/2010/03/22/5404834.aspx

Http://labs.chinamobile.com/mblog/532767_73183

 

4.



Add property settings to the applicationCode

As mentioned earlier, for
Android
for example, application Notification
init
Start
service
This is done by setting system properties, specifically set
System
system attributes
"CTL. start "

" ifconfig_test "
, in this way
Android
the system will run
ifconfig_test
This
service
now.

There are three methods for setting the system property, which correspond to three different applications:
 

1) Java code

 
Android provides the system. getproperty and system. setproperty methods in the Java library. Java programs can set and obtain properties through them. The Code is as follows:

Systemproperties. Set ("CTL. Start", "ifconfig_test ");

The above code is a notification.
Android
Run
Ifconfig_test Service
To query the current
Service
The execution status. For example, you can query the execution status by using the following code:

Ret = systemproperties. Get ("init. SVC. ifconfig_test ","");

If (Ret! = NULL & ret. Equals ("STOPPED "))

 
{


Return true;

 
}

 
2)


JNI
Code

When writing
Ndk
You can use
Property_get
And
Property_set
The two
API
To obtain and set properties. Use these two
API
The header file must be included.
Cutils/properties. h


And links
Libcutil
Library.

 

3)


Shell
Script

Android
Provides command line
Setprop
And
Getprop
To set and obtain attributes. They can be used in scripts.

 

Because my program is in
JNI
The script is executed again.
Ifconfig
So I put the Property setting part in the script. The Code is as follows:

setprop CTL. Start ifconfig_test

# Wait for the service until it stops

ret = 1

while [$ ret-Ne 0]

DO


getprop | grep "$ enable_mapper_srv" | grep stopped


ret = $?

done


above
4
steps,
Android
the application obtains
root
permission, more specifically, is obtained temporarily when executing the command we need to execute
root
permission.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.