Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implementation for android (maybe using termux) #78

Closed
diehard88 opened this issue Jan 6, 2020 · 11 comments · Fixed by #384
Closed

Implementation for android (maybe using termux) #78

diehard88 opened this issue Jan 6, 2020 · 11 comments · Fixed by #384
Labels
enhancement New feature or request help wanted Extra attention is needed

Comments

@diehard88
Copy link

Is it possible?

@imsnif imsnif changed the title android termux Implementation for android (maybe using termux) Jan 6, 2020
@imsnif imsnif added good first issue Good for newcomers help wanted Extra attention is needed labels Jan 6, 2020
@imsnif
Copy link
Owner

imsnif commented Jan 6, 2020

Anything's possible. :)

@ivanhercaz
Copy link

Have it in Android using Termux would be really awesome.

@imsnif imsnif added enhancement New feature or request and removed good first issue Good for newcomers labels Jan 8, 2020
@sigmaSd
Copy link
Collaborator

sigmaSd commented Jul 19, 2020

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
Copy link
Collaborator

sigmaSd commented Jul 20, 2020

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)

@imsnif
Copy link
Owner

imsnif commented Jul 21, 2020

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).

@sigmaSd
Copy link
Collaborator

sigmaSd commented Jul 21, 2020

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

@imsnif
Copy link
Owner

imsnif commented Jul 21, 2020

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+ :)

@sigmaSd
Copy link
Collaborator

sigmaSd commented Jul 21, 2020

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 )

@imsnif
Copy link
Owner

imsnif commented Aug 21, 2020

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!!

@sigmaSd
Copy link
Collaborator

sigmaSd commented Aug 30, 2020

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).

@alexanderadam
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants