bandwhich icon indicating copy to clipboard operation
bandwhich copied to clipboard

Implementation for android (maybe using termux)

Open diehard88 opened this issue 5 years ago • 11 comments

Is it possible?

diehard88 avatar Jan 06 '20 00:01 diehard88

Anything's possible. :)

imsnif avatar Jan 06 '20 07:01 imsnif

Have it in Android using Termux would be really awesome.

ivanhercaz avatar Jan 06 '20 19:01 ivanhercaz

For the compile step, these diff makes it almost work bandwhich_diff

diff --git a/src/os/mod.rs b/src/os/mod.rs
index 056d44e..374fdbc 100644
--- a/src/os/mod.rs
+++ b/src/os/mod.rs
@@ -1,4 +1,4 @@
-#[cfg(target_os = "linux")]
+#[cfg(any(target_os = "linux", target_os = "android"))]
 pub(self) mod linux;
 
 #[cfg(any(target_os = "macos", target_os = "freebsd"))]
diff --git a/src/os/shared.rs b/src/os/shared.rs
index 462f269..bcef6ea 100644
--- a/src/os/shared.rs
+++ b/src/os/shared.rs
@@ -11,7 +11,7 @@ use ::std::time;
 use crate::os::errors::GetInterfaceErrorKind;
 use signal_hook::iterator::Signals;
 
-#[cfg(target_os = "linux")]
+#[cfg(any(target_os = "linux", target_os = "android"))]
 use crate::os::linux::get_open_sockets;
 #[cfg(any(target_os = "macos", target_os = "freebsd"))]
 use crate::os::lsof::get_open_sockets;
@@ -245,7 +245,7 @@ fn eperm_message() -> &'static str {
 }
 
 #[inline]
-#[cfg(target_os = "linux")]
+#[cfg(any(target_os = "linux", target_os = "android"))]
 fn eperm_message() -> &'static str {
     r#"
     Insufficient permissions to listen on network interface(s). You can work around

procfs_diff

diff --git a/src/process/mod.rs b/src/process/mod.rs
index 0c30c22..6915316 100644
--- a/src/process/mod.rs
+++ b/src/process/mod.rs
@@ -54,12 +54,12 @@
 
 use super::*;
 use crate::from_iter;
-
+use std::convert::TryFrom;
 use std::ffi::OsString;
 use std::fs;
 use std::io::{self, Read};
-#[cfg(unix)]
-use std::os::linux::fs::MetadataExt;
+#[cfg(linux)]
+use std::os::unix::fs::MetadataExt;
 use std::path::PathBuf;
 use std::str::FromStr;
 
@@ -90,6 +90,9 @@ impl FakeMedatadataExt for std::fs::Metadata {
     }
 }
 
+#[cfg(target_os = "android")]
+use std::os::android::fs::MetadataExt;
+
 bitflags! {
     /// Kernel flags for a process
     ///
@@ -179,9 +182,9 @@ bitflags! {
 bitflags! {
     /// The mode (read/write permissions) for an open file descriptor
     pub struct FDPermissions: u32 {
-        const READ = libc::S_IRUSR;
-        const WRITE = libc::S_IWUSR;
-        const EXECUTE = libc::S_IXUSR;
+        const READ = libc::S_IRUSR as u32;
+        const WRITE = libc::S_IWUSR as u32;
+        const EXECUTE = libc::S_IXUSR as u32;
     }
 }
 
@@ -760,7 +763,7 @@ impl Process {
                 let link_os: &OsStr = link.as_ref();
                 vec.push(FDInfo {
                     fd,
-                    mode: md.st_mode() & libc::S_IRWXU,
+                    mode: md.st_mode() & u32::try_from(libc::S_IRWXU).unwrap(),
                     target: expect!(FDTarget::from_str(expect!(link_os.to_str()))),
                 });
             }

with these changes it fails in linking:

  = note: pnet_datalink-0.26.0/src/unix_interfaces.rs:39: error: undefined reference to 'getifaddrs'
             pnet_datalink-0.26.0/src/unix_interfaces.rs:74: error: undefined reference to 'freeifaddrs'

hacking those lines out makes it compile and have a runtime error as expected, so this should be the last issue regarding compiling. I'm hoping someone with more knowledge about the subject can figure whats the next step is, as far as I can see those functions exists in arm-linux-androideabi https://rust-lang.github.io/libc/arm-linux-androideabi/libc/fn.getifaddrs.html

Note: compile command: cross build --target arm-linux-androideabi cargo-cross

sigmaSd avatar Jul 19 '20 19:07 sigmaSd

Turns out that the last compile issue was a cross-compilation problem, compiling from termux with the above diffs actually work. Next I need a rooted phone to test. (I'll update with the test when I can)

sigmaSd avatar Jul 20 '20 20:07 sigmaSd

Wow @sigmaSd - that's wild! If this works, we can probably make a PR to procfs to make the changes there. If for some reason there's an issue with that, we could also use the mac way of getting the proc info with lsof (I'm guessing that would work on android too, but I don't know).

imsnif avatar Jul 21 '20 12:07 imsnif

Currently I don't have a rooted phone to test, and android avd emulator's arm vm is just a slideshow

If someone want to test it and have a rooted andorid phone or arm android vm with good pc (you still need root acess https://stackoverflow.com/questions/5095234/how-to-get-root-access-on-android-emulator ) these are the steps:

1- install termux 2 - pkg install rust 3 - cargo install --git https://github.com/sigmaSd/bandwhich --branch android

sigmaSd avatar Jul 21 '20 18:07 sigmaSd

Hey @sigmaSd, I tried on my rooted android phone, but unfortunately it only has android 6.0.0. This means that I'm getting an outdated version of termux, which includes rust 1.38, which is not high enough to run bandwhich. :( I tried cross-compiling but got the same error you mentioned above. I also tried installing a newer version of rust with rustup, but got a error: target 'aarch64-linux-android' not found in channel. error even when trying for the minimal install. So I guess we'll have to wait for someone with a rooted android phone with android 7.0.0+ :)

imsnif avatar Jul 21 '20 20:07 imsnif

Hi @imsnif , Honestly I don't think this is a priority anyway only because of the fact that it requires root. Hopefully someone interested in it will try it. I'm just curious how well will it work ^^

Note: If someone have a pc with good specs they can just test with an android emulator https://developer.android.com/studio/run/managing-avds (steps: 1) any hardware will do, 2) for the image select an arm based image with Google APIs flag (it can be rooted) 3: get root access https://stackoverflow.com/questions/5095234/how-to-get-root-access-on-android-emulator )

sigmaSd avatar Jul 21 '20 22:07 sigmaSd

Hey @sigmaSd, my old phone died recently and I got a new one, and well...

drawing

Two things that still aren't working:

  1. DNS, I'm only able to run it with the -n flag (no resolve), so we see IP addresses and not hostnames
  2. All processes are <UNKNOWN> - might be a specific android thing

I'm hoping to get a chance to look into this further soon, but great work so far!!

imsnif avatar Aug 21 '20 16:08 imsnif

Hi @imsnif , That's nice to see! As you can see from the diff above bandwihch already works on android, I just changed some compile flags (In a hacky way btw).

sigmaSd avatar Aug 30 '20 15:08 sigmaSd

Just to be sure: bandwhich will use root access, right?

alexanderadam avatar Jan 15 '22 15:01 alexanderadam