first off: I know that this worky only on rooted devices and i know that it is not recommended - but I still want and need to do it.
I am writing an app which performs OCR on an other app, parses its on-screen output and gives the user feedback on the apps progress (therefor using getRootView is out of the question).
This can not be done in an other way, I need to have screenshots of the app at least 3 to 4 times per second.
Other ways that I tried:
/system/bin/screencap - too slow, takes >2 sec per shot on a Galaxy S5.
using obscure C Code to access the internal API of the SurfaceComposer (bloated, did not compile)
What I want: Have a way to read bytes from the framebuffer without having to write it to a file each time.
Currently I have the problem that my app does not have the right permissions. I added the READ_FRAME_BUFFER permission, but I still get a ERRNO 13 (Permission denied) when reading /dev/graphics/fb0, as the app itself is not started with root permissions.
I know I can start a shell or something similar with su, but that is not convenient - I would prefer a way to start a Service or my native Code with the right permissions.
I read about System Services but could not find any "easy" introductions. My experience in C/C++/Java is more than enough, but the Android API's jungle is newfound land to me.
The information from TI-Wiki - Writing System Services seems to implie that a rebuild of the Android System is necessary to integrate a System Service. That would be out of the question. Is that correct?
In an ideal world I would have:
Bitmap Service.getCurrentFrame() {
read one frame from /dev/graphics/fb0
create Java Bitmap
return Bitmap
}
This could be either native or Java code.
But how to gain the privileges?
Any ideas?
Additionally, I read that using the framebuffer is not recommended since it is about to be removed in future releases (sorry, lost source link).
What other fast ways are there to get the current screen content?
Use DDMS lib /dev/graphics/fb0 to get bitmap. No special permission needed.
Related
is it possible to hide files in android by transfering/moving them to a location or sector (like in root folders or something) of which other apps don't have access to (via adb or termux or something)?.
I have mentioned adb and termux because i've seen performing actions like uninstalling system apps from device. and if possible, i don't want to root my device.
One humble request: i don't know the ABC's of app building/compiling, maximum i can do is execute commands in adb/termux. so if you paste any code, please also mention what to do with it.
i have tried:
putting dot at starting of the name of the files is too older method and everyone knows about it. encryption and decryption is too much time consuming process. And i don't have that much important data, i just want to hide it from direct access so that most of the people can't find it by normal methods.
Thank you very much
Assume we have a process that may dlopen() some third-party library. This library may perform open("write_only_logfile", O_WRONLY) on some file to which user has only write access. We need to have an ability to be notified if this library attempts to open a file, so later we may dup() returned descriptor and redirect output.
There are few restrictions that make interception harder:
LD_PRELOAD is forbidden - no way to hook open()
inotify(7) doesn't help because user has no read permissions on "write_only_logfile" and it is owned by admin
we have no access to library sources and therefore cannot modify it
"write_only_logfile" is hardcoded inside the library, so we cannot pass another name to perform redirecting
I'm wondering if Linux has an efficient way to help in such situation.
Especially taking in account the fact that process may open() miscellaneous files pretty often.
P.S. To avoid confusion and understand better - it is a regular Android application with loaded JVM. If app hangs (so called ANR) - system sends SIGQUIT to it. Signal is received via dedicated thread that open()s /data/anr/traces.txt and writes JVM state to it. These data extremely useful for debugging. But app cannot read that file directly because of security reasons (All applications write to it, so there may be somewhat sensitive). Anyway I believe that it is absolutely fair to intercept content that my process would write to it.
P.S.S. In the worst case it is possible to find JVM library image (libart.so) and manually patch jump slot for open(). But it doesn't sound well.
Sounds like you are in troublesome situation. Most solutions briefly mentioned below are guaranteed to interfere with SELinux, so don't take my word for any of that.
Debugging your own process with strace to intercept open is one of usual solutions on normal Linux. I am not sure if it would work in Android; it certainly might become off-limit for non-debuggable apps starting in some new versions (if it is has not been banned yet).
seccomp-bpf is another possibility. Might not be available on older Android versions, but since Android O seccomp is going to be a guaranteed part of Android security getup. Intercept open in warn-only mode and give control back to yourself when something interesting happen (via debugging or signals).
If /data/anr/traces.txt is opened on-demand, you should be able to observe that by watching contents of /proc/self/fd/ with inotify or via polling. You might be able to reduce impact of races by setting io niceness of the opening thread…
All of above are only partial solutions, you still might need to decode actual open syscall that happened (strace source code might be helpful there for strace/seccomp solutions, readlink for /proc/self/fd/) and act upon it (dup2, as you already mentioned).
"write_only_logfile" is hardcoded inside the library
Is it possible to modify the memory of data segment of the library/executable? Afaik mprotect and PROTECT_EXEC in particular have been heavily restricted, but at least mmap is certainly permitted (to support JIT compilers etc). It might be possible to cook something up to edit the string constant in place (as long as doing so is possible and allowed, I am not sure myself about that).
If this is just about redirecting writes (and reads) to a single file, you could run the application in a mount namespace with a suitable bind mount for that particular file. Setting things up in this way probably requires a small SUID binary.
A more general solution quickly approaches a union file system, and getting it right is quite hard. Even the in-kernel union file system, overlayfs, does not manage to provide full POSIX semantics.
You need LD_PRELOAD to hook an application. To hook a third-party library, just load your hook normally before the library (or have it in your executable).
Assuming the library calls open from libc and not the corresponding syscall directly, and that it is linked in a normal way, you just have a function named open somewhere in your code. Make it call open from libc (RTLD_NEXT or whatever). The third-party library (and all other libraries of course) will resolve its open symbol to your function.
I'd like to get some numerical data from an app, but they are not stored as files like db. I know there are some memory hack apps for changing in game values although I do not know how they work.
I am looking for similar features but I don't need to change anything.
The app I am trying to write just reads some data from a specific app and do some background calculation based on that. If this is not possible, I would need to get information by reading the screen(for example get pixel color), but this seems to be very cumbersome task for getting many data.
Is there a way of achieving this?
Thanks.
EDIT: I'd assume I would need a root permission for this?
Yes, you would need root permission. Additionally your users must have fully rooted device with e.g. SuperSU or other modern Su app, that can lift most SELinux restrictions. There may also be conflicts with KNOX and other similar systems, but I am not really knowledgeable about those.
You would need to attach your process as debugger to the target application and locate the necessary data by scanning it's memory. This can be done in multiple ways, the best reference implementation to look at can be found in scanmem.
The code, performing the actual deed, which requires root rights, — reading/writing target process memory — would reside in a native executable, being run via su. You'd have to write some code to communicate with that executable (probably via it's stdin/stdout or something like that).
You will also have to write additional code to parse the memory layout of target application yourself.
Alternatively, you may prefer to inject a small module in memory of target application and/or have the app itself load a Dex file of you making (especially handy, if your target data is stored in Java memory). This approach have a benefit of minimizing interaction with memory layout of virtual machine, but you still have to initiate loading of initial Dex file. Once Dex file is loaded, you can do the rest in Java code, using good-old reflection API. If you go with this route, a (decently supported!) code for injecting executable snippets in memory of Linux process can be found in compel library, being developed as part of CRIU project[1].
Two Android processes cannot share memory and communicate with each other directly. So to communicate, objects have to be decomposed into primitives (marshalling) and transfered across process boundaries.
To do this marshalling, one has to write a lot of complicated code, hence Android handles it for us with AIDL (Android Interface Definition Language).
From the OP, as no more details can be found, I would recommend you reading/searching with the keyword "AIDL" and you will be redirected to the concrete solutions.
I am working on an android project which needs to create and write files rapidly. I am using ndk for this purpose and found that fopen() call takes uncertain amount of time, from minimum ~30ms to several seconds whening running from the main thread. After opening the file, I then need to compute some results, store results into the opened file and then close it.
I am trying to put it into another thread but not sure if it helps at all and how to handle scheduling issue if it does. I am also thinking about possibly opening many of those file descriptors at the beginning of the application and maintain a pool of those through the applcation. Anyone helping to point to the right direction?
It sounds like you are trying to go very low level.
Have you considered using the open() write() and close() System calls. The c-library fopen calls do some nice things for you such as buffering, but the system calls are likely to be faster. You will have to profile, but I think you will see lower latency.
int fd = open("myfilepath",O_WRONLY|O_CREAT);
write(fd,myData, myDataSize);
close(fd);
You will find more info here.
http://www.tutorialspoint.com/unix_system_calls/open.htm
I want to implement __android_log_write() functionality using write() or some api available in libc(actually ulibc). The reason being that i cannot use any libraries associated with android as that would increase the memory required. I have very limited amount of memory as my code is running in separate memory region reserved during boot up. Main goal is to attach my debugging logs to logcat.
I am looking something similar to this:
write(1,"sandy",6);
The abovce code i can directly write to stdout. Similarly, i want to use write() or something else and write to logcat. What is the clean way to do it.
Hope i am clear. Thanks.
Got the answer. We need to open /dev/radio and write into them.
Thanks