package com.example.checkrw;
import
android.os.Bundle;
import
android.widget.Toast;
import
androidx.appcompat.app.AppCompatActivity;
import
java.io.IOException;
import
java.io.InputStream;
import
java.util.NoSuchElementException;
import
java.util.Scanner;
public
class
MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
this.checkForRWPaths();
}
private String[] pathsThatShouldNotBeWritable
=
{
"/system"
,
"/system/bin"
,
"/system/sbin"
,
"/system/xbin"
,
"/vendor/bin"
,
"/sbin"
,
"/etc"
,
"/sys"
,
"/proc"
,
"/dev"
};
private String[] mountReader() {
try
{
InputStream inputstream
=
Runtime.getRuntime().
exec
(
"mount"
).getInputStream();
if
(inputstream
=
=
null)
return
null;
String propVal
=
new Scanner(inputstream).useDelimiter(
"\\A"
).
next
();
return
propVal.split(
"\n"
);
} catch (IOException | NoSuchElementException e) {
Toast.makeText(getApplicationContext(), e.getMessage(),
Toast.LENGTH_LONG).show();
return
null;
}
}
public void checkForRWPaths() {
/
/
Run the command
"mount"
to retrieve
all
mounted directories
String[] lines
=
mountReader();
if
(lines
=
=
null){
return
;
}
int
sdkVersion
=
android.os.Build.VERSION.SDK_INT;
for
(String line : lines) {
/
/
Split lines into parts
String[] args
=
line.split(
" "
);
if
((sdkVersion <
=
android.os.Build.VERSION_CODES.M && args.length <
4
)
|| (sdkVersion > android.os.Build.VERSION_CODES.M && args.length <
6
)) {
/
/
If we don't have enough options per line, skip this
and
log an error
Toast.makeText(getApplicationContext(),
"Error formatting mount line: "
+
line
+
line,
Toast.LENGTH_LONG).show();
continue
;
}
String mountPoint;
String mountOptions;
/
*
*
*
To check
if
the device
is
running Android version higher than Marshmallow
or
not
*
/
if
(sdkVersion > android.os.Build.VERSION_CODES.M) {
mountPoint
=
args[
2
];
mountOptions
=
args[
5
];
}
else
{
mountPoint
=
args[
1
];
mountOptions
=
args[
3
];
}
for
(String pathToCheck: this.pathsThatShouldNotBeWritable) {
if
(mountPoint.equalsIgnoreCase(pathToCheck)) {
/
*
*
*
If the device
is
running an Android version above Marshmallow,
*
need to remove parentheses
from
options parameter;
*
/
if
(android.os.Build.VERSION.SDK_INT > android.os.Build.VERSION_CODES.M) {
mountOptions
=
mountOptions.replace(
"("
, "");
mountOptions
=
mountOptions.replace(
")"
, "");
}
/
/
Split options out
and
compare against
"rw"
to avoid false positives
for
(String option : mountOptions.split(
","
)){
if
(option.equalsIgnoreCase(
"rw"
)){
Toast.makeText(getApplicationContext(), pathToCheck
+
" 路径以rw权限挂载! "
+
line,
Toast.LENGTH_LONG).show();
}
}
}
}
}
}
}