+ All Categories
Home > Technology > Porting your favourite cmdline tool to Android

Porting your favourite cmdline tool to Android

Date post: 19-Feb-2017
Category:
Upload: vlatko-kosturjak
View: 689 times
Download: 0 times
Share this document with a friend
43
PORTING TO ANDROID PORTING YOUR FAVOURITE CMDLINE TOOL TO ANDROID Vlatko Kosturjak (@k0st), Droidcon Zagreb, 30th of April
Transcript
Page 1: Porting your favourite cmdline tool to Android

PORTING TO ANDROID

PORTING YOUR FAVOURITE CMDLINE TOOL

TO ANDROID

Vlatko Kosturjak (@k0st), Droidcon Zagreb, 30th of April

Page 2: Porting your favourite cmdline tool to Android

AGENDAIntroductionNative codeToolchainsThings I wish I knew in advanceCalling native executablesIssues and implicationsSummaryQuestions and answers

45 minutes

Page 3: Porting your favourite cmdline tool to Android

ABOUT ME

Security Consultant in DivertoLinux and FLOSS enthusiastOpen source developer

Have code in OpenVAS, Nmap, Metasploit, ...

Android "developer" since 2010started counting from first Market appmostly focused on NDK and ADK

https://github.com/kost

Page 4: Porting your favourite cmdline tool to Android

ABOUT ME IN PICTURES

Page 5: Porting your favourite cmdline tool to Android

ABOUT ME IN PICTURES

Page 6: Porting your favourite cmdline tool to Android

INTRODUCE ELEPHANTTalk will cover

producing standalone binaries

executing standalone binaries

Talk is mostly about Nmap experience

Most Nmap frontends on playstore are using this port

in source or binary form

Talk will NOT cover

producing libraries or JNI

integrating with Android Studio

https://github.com/kost/nmap-android

https://github.com/kost/NetworkMapper

Page 7: Porting your favourite cmdline tool to Android

NATIVE CODE

NOT your Java code :)

It's mostly about

C/C++

Assembler

Not portable across platforms

For each platform, you need different binary

x86

arm

mips

Page 8: Porting your favourite cmdline tool to Android

WHY BOTHER WITH NATIVE CODE?performancelegacy codecode reuseyou just need that tool

Page 9: Porting your favourite cmdline tool to Android

WHAT'S THE PROCESS?compiling

compiling on same machinecross-compiling

compiling on (host) machine for other (target) machine

Page 10: Porting your favourite cmdline tool to Android

TOOLCHAINSAndroid NDK

Commercial

Open Source

Custom

Page 11: Porting your favourite cmdline tool to Android

CUSTOM TOOLCHAINYour own version of compilerYour own version of build scriptsCustom

Page 12: Porting your favourite cmdline tool to Android

COMMERCIAL

EmbarcaderoGood old Borland...

XamarinNative apps in C#

...

Page 13: Porting your favourite cmdline tool to Android

OPEN SOURCE / FREE

Crystax

drop-in replacement for Google's NDK

WCHAR, locales, full C+11 standard library...

Buildroot

Standard embedded cross compilation toolchain

ARM, x86, MIPS

Scratchbox

ARM, x86, MIPS (experimental)

Anyone remembers Maemo? :)

...

Page 14: Porting your favourite cmdline tool to Android

ANDROID NDK

Android official toolchain

Available for free from developer.android.com

Bionic

No full ANSI C support

locale

different threads

Patch as you grow

standalone binary support/bugs

stdout symbol bug

WCHAR support

standard library support

Page 15: Porting your favourite cmdline tool to Android

WHAT'S THE FUZZ?

Download NDKDownload tool you want to port

./configure --host=arm-linux-androideabimakemake install

It works - go home!

Page 16: Porting your favourite cmdline tool to Android

IN CASE IT IS HELLO WORLD.../* Hello World program */

#include <stdio.h>

void main()

{

printf("Hello World");

}

It works pretty well indeed.

Page 17: Porting your favourite cmdline tool to Android

IN REAL WORLD

Code isn't perfectNot portableEndianessPath SeparatorsDependenciesExtensions3rd party libraries

Page 18: Porting your favourite cmdline tool to Android

TWO WAYS TO INVOKE COMPILER

Calling with sysrootexport CC="$NDK/toolchains/arm-linux-androideabi-4.6/prebuilt/linux-x86/bin/arm-linux-androideabi-gcc"

export CFLAGS="--sysroot=$SYSROOT"

$CC $CFLAGS -o hello hello.c

Producing directory for target

$NDK/build/tools/make-standalone-toolchain.sh --platform=android-3 --install-dir=/

/opt/ndk3/bin/arm-linux-androideabi-gcc -o hello hello.c

Page 19: Porting your favourite cmdline tool to Android

NDK PLATFORMS

NDK platform Platforms 32/64 bit

3 ARM 32

9 ARM/MIPS/Intel 32

21 ARM/MIPS/Intel 64

Page 20: Porting your favourite cmdline tool to Android

PROCESS OF CROSS COMPILING

Compile and fix as you go :)sorry, no single recipe

Standard problemsstdout bugold autoconf/automake support filesarm-linux-androideabi missing

In shortnothing that google/stackoverflow can't help :)

Page 21: Porting your favourite cmdline tool to Android

STATIC VS DYNAMIC LINKINGDynamic

small sizerun-time dependency

Staticlarge sizeno dependencies

Page 22: Porting your favourite cmdline tool to Android

LIFE IS PERFECTStatic binaries working like a charm

“until resolv.conf disappeared :) ”

Page 23: Porting your favourite cmdline tool to Android

DNS PROBLEMS

int main(int argc,char *argv[]) {

int i;

struct hostent *hp;

for ( i=1; i<argc; ++i ) {

hp = gethostbyname(argv[i]);

if ( !hp ) {

fprintf(stderr, "%s: host '%s'\n", hstrerror(h_errno),

argv[i]);

continue;

}

printf("Host:\t%s\n" ,argv[i]);

printf("\tResolves to:\t%s\n", hp->h_name);

}

}

Original at gist

Page 24: Porting your favourite cmdline tool to Android

DNS AND RESOLV.CONF

#ifdef ANDROID_CHANGES /* READ FROM SYSTEM PROPERTIES */ dns_last_change_counter = _get_dns_change_count(); [..]#else /* !ANDROID_CHANGES - IGNORE resolv.conf in Android */#define MATCH(line, name) \ [..]

Original at https://code.google.com/p/android-source-

browsing

Page 25: Porting your favourite cmdline tool to Android

DYNAMIC VS STATIC

Type Size Dependency DNS OOTB

Dynamic smaller yes yes

Static bigger no no

Mixed medium yes (basic) yes

Page 26: Porting your favourite cmdline tool to Android

HERE COMES LOLIPOPerror: only position independent executables (PIE) are supported.

Position Independent Executable (PIE)PIE support appeared in API level 16Finally they implemented it :)

Too bad binaries does not work

Page 27: Porting your favourite cmdline tool to Android

WHAT'S PIE?

Position Independent Executable (PIE)

Security protection

better Address Space Layout Randomization (ASLR)

Exploitation mitigation technique

Harder return-to-libc exploitation

Requirements

PIE required for dynamic executables

PIE not required for static executables

Page 28: Porting your favourite cmdline tool to Android

PIE EXAMPLE#include <stdio.h>

int global;

int checkadr (int *bla){ int local; printf("bla adr = %p\n", &bla); printf("global adr = %p\n", &global); printf("local adr = %p\n", &global);}

int main (void) { int c; printf("c adr = %p\n", &c); printf("checkadr adr = %p\n", &checkadr);

Page 29: Porting your favourite cmdline tool to Android

PIE SUPPORT

Android version Supported Required

1,2,3 no no

4 yes no

5 yes yes

Page 30: Porting your favourite cmdline tool to Android

PIE WORKAROUND

Way to run PIE executables on non supported systemsif system suppports PIE

just run executableif system does not suppport PIE

use run_pie.crun_pie your_proggy args

CFLAGS +=-fvisibility=default -fPIELDFLAGS += -rdynamic -pie

https://gist.github.com/kost/5fd4628f45a4995bec28

Page 31: Porting your favourite cmdline tool to Android

CALLING NATIVE EXECUTABLESp = Runtime.getRuntime().exec(command);p.waitFor();BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));String line;while ((line = reader.readLine()) != null) { output.append(line).append("\n");}

Page 32: Porting your favourite cmdline tool to Android

BETTER WAY - USING

PROCESSBUILDER

ProcessBuilder processBuilder = new ProcessBuilder(shellToRun);

processBuilder.redirectErrorStream(true);

scanProcess = processBuilder.start();

outputStream = new DataOutputStream(scanProcess.getOutputStream());

inputStream = new BufferedReader(new InputStreamReader(scanProcess.getInputStream()));

while (((pstdout = inputStream.readLine()) != null)) {

output.append(pstdout).append("\n");

}

Page 33: Porting your favourite cmdline tool to Android

RUNNING BINARIES AS ROOT

Not needed to set any new android permission

Historic references to SUPERUSER permissions

Not much different than executing as normal user

Have to Runtime.getRuntime().exec("su")

Write commands to stdin of process

Loop the output

Page 34: Porting your favourite cmdline tool to Android

ROOT IMPLICATIONS

Killing run away root processes

Hard as it can be due to blocking nature

UI does not have root access

Killing spawned root processes

parse ps output

spawn su shell

kill process

Page 35: Porting your favourite cmdline tool to Android

SECURITY IMPLICATIONS

Native binary problemsMemory corruption attacks (Buffer overflows, ...)Format string problems......

PermissionsCommand injections

Page 36: Porting your favourite cmdline tool to Android

SECURITY IMPLICATIONS -

PERMISSIONS

Setting insecure permissions to executables/libraries

Very common when something does not work

Dangerous and heroic

Other apps can write to your bin or library

Exploitation

Find insecure .so library, inject your code

Find insecure binary, replace it with your version!

echo "#!/bin/sh" > /data/data/com.heroic.app/bin/mybinaryecho "echo '0wned!'" >> /data/data/com.heroic.app/bin/mybinary

Page 37: Porting your favourite cmdline tool to Android

SECURITY IMPLICATIONS -

UNTRUSTED INPUT

Passing untrusted/unvalidated input to shellRunning native executables can lead to commandinjections

Extremely dangerous if running as userExtremely heroic and dangerous if running as root

Pay special attention to exported activitiesother apps can call that intentwhich means they can execute commands as yourapp!!

Page 38: Porting your favourite cmdline tool to Android

UNTRUSTED INPUT EXAMPLEBundle b = getIntent().getExtras();

configFilePath = b.getString("path");

[..]

ShellExecuter exe = new ShellExecuter();

return exe.Executer("cat " + configFilePath);

<activity

android:name=".MyHeroicActivity"

....

android:exported="true" />

Page 39: Porting your favourite cmdline tool to Android

UNTRUSTED INPUT EXPLOITATION

public void onBtnClick(View view) { Intent intent = new Intent(); intent.setClassName("com.heroic.app", "com.heroic.app.MyHeroicActivity" intent.putExtra("path", "/system/etc/hosts; echo 'Owned' > /data/data/com.heroic.app/bin/binary" startActivity(intent);}

Page 40: Porting your favourite cmdline tool to Android

ON THE END..You get bad comments :)

Don't use ratings for bug reports ;)

Please submit VERBOSE bug reports to author directly

Page 41: Porting your favourite cmdline tool to Android

FORTUNATELY

Fortunately, there are good comments ;)

Thanks on these

Page 42: Porting your favourite cmdline tool to Android

SUMMARYPorting is quite possible

Not as easy as marketing says

You can't configure; make; make install in most cases

Expect you'll have to patch if project is bigger

Not that hard

If you know requirements upfront

Have listened to this lecture carefully

Be aware of security implications!

Page 43: Porting your favourite cmdline tool to Android

THANKS ON LISTENING

?ANY QUESTIONS?


Recommended