2018-04-12 22:28:47 -04:00
|
|
|
/*
|
|
|
|
Hypatia: An realtime malware scanner for Android
|
2018-06-03 13:49:37 -04:00
|
|
|
Copyright (c) 2017-2018 Divested Computing, Inc.
|
2018-04-12 22:28:47 -04:00
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2017-12-16 07:11:02 -05:00
|
|
|
package us.spotco.malwarescanner;
|
|
|
|
|
|
|
|
import android.app.Notification;
|
2018-04-05 21:08:52 -04:00
|
|
|
import android.app.NotificationChannel;
|
2017-12-26 17:28:44 -05:00
|
|
|
import android.app.NotificationManager;
|
2017-12-16 07:11:02 -05:00
|
|
|
import android.app.Service;
|
2017-12-26 17:28:44 -05:00
|
|
|
import android.content.Context;
|
2017-12-16 07:11:02 -05:00
|
|
|
import android.content.Intent;
|
2018-04-05 21:08:52 -04:00
|
|
|
import android.os.Build;
|
2017-12-16 07:11:02 -05:00
|
|
|
import android.os.Environment;
|
|
|
|
import android.os.FileObserver;
|
|
|
|
import android.os.IBinder;
|
|
|
|
import android.support.v4.app.NotificationCompat;
|
|
|
|
|
|
|
|
import java.io.File;
|
2018-01-20 01:50:37 -05:00
|
|
|
import java.util.HashSet;
|
2017-12-27 16:02:37 -05:00
|
|
|
import java.util.concurrent.Executors;
|
2017-12-26 17:56:39 -05:00
|
|
|
import java.util.concurrent.ThreadPoolExecutor;
|
2017-12-17 20:02:26 -05:00
|
|
|
|
2017-12-16 07:11:02 -05:00
|
|
|
public class MalwareScannerService extends Service {
|
|
|
|
|
2018-01-20 01:50:37 -05:00
|
|
|
private final HashSet<RecursiveFileObserver> malwareMonitors = new HashSet<>();
|
2017-12-26 17:56:39 -05:00
|
|
|
private ThreadPoolExecutor threadPoolExecutor = null;
|
2017-12-26 17:28:44 -05:00
|
|
|
private NotificationCompat.Builder foregroundNotification = null;
|
|
|
|
private NotificationManager notificationManager = null;
|
2017-12-16 07:11:02 -05:00
|
|
|
|
|
|
|
@Override
|
2017-12-16 15:02:10 -05:00
|
|
|
public final IBinder onBind(Intent intent) {
|
2017-12-16 07:11:02 -05:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2017-12-16 15:02:10 -05:00
|
|
|
public final int onStartCommand(Intent intent, int flags, int startId) {
|
2017-12-27 16:02:37 -05:00
|
|
|
malwareMonitors.clear();
|
|
|
|
addMalwareMonitor(Environment.getExternalStorageDirectory().toString());
|
|
|
|
|
|
|
|
threadPoolExecutor = (ThreadPoolExecutor) Executors.newScheduledThreadPool(Utils.getMaxThreads() + malwareMonitors.size());
|
2017-12-26 17:56:39 -05:00
|
|
|
threadPoolExecutor.execute(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
Database.loadDatabase(getApplicationContext(), true, Database.signatureDatabases);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2017-12-27 16:02:37 -05:00
|
|
|
for (final RecursiveFileObserver malwareMonitor : malwareMonitors) {
|
|
|
|
threadPoolExecutor.execute(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
malwareMonitor.startWatching();
|
|
|
|
}
|
|
|
|
});
|
2017-12-16 07:11:02 -05:00
|
|
|
}
|
2017-12-26 17:28:44 -05:00
|
|
|
|
|
|
|
notificationManager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
|
2018-04-16 01:06:57 -04:00
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
2018-04-05 21:46:24 -04:00
|
|
|
NotificationChannel foregroundChannel = new NotificationChannel("FOREGROUND", getString(R.string.lblNotificationRealtimeTitle), NotificationManager.IMPORTANCE_LOW);
|
|
|
|
foregroundChannel.setDescription(getString(R.string.lblNotificationRealtimeDescription));
|
|
|
|
foregroundChannel.setShowBadge(false);
|
|
|
|
notificationManager.createNotificationChannel(foregroundChannel);
|
|
|
|
}
|
2017-12-16 09:31:42 -05:00
|
|
|
setForeground();
|
2017-12-16 07:11:02 -05:00
|
|
|
return START_STICKY;
|
|
|
|
}
|
|
|
|
|
2018-01-20 01:50:37 -05:00
|
|
|
private void addMalwareMonitor(String monitorPath) {
|
2017-12-16 16:58:43 -05:00
|
|
|
malwareMonitors.add(new RecursiveFileObserver(monitorPath) {
|
|
|
|
@Override
|
|
|
|
public void onEvent(int event, String path) {
|
|
|
|
switch (event) {
|
|
|
|
case FileObserver.MOVED_TO:
|
|
|
|
case FileObserver.CLOSE_WRITE:
|
|
|
|
File file = new File(path);
|
2017-12-17 10:36:44 -05:00
|
|
|
if (file.exists() && /*file.length() > 0 &&*/ file.length() <= Utils.MAX_SCAN_SIZE_REALTIME) {
|
2018-01-20 01:50:37 -05:00
|
|
|
HashSet<File> filesToScan = new HashSet<>();
|
2017-12-16 16:58:43 -05:00
|
|
|
filesToScan.add(file);
|
2017-12-26 17:56:39 -05:00
|
|
|
new MalwareScanner(null, getApplicationContext(), false).executeOnExecutor(threadPoolExecutor, filesToScan);
|
2017-12-16 16:58:43 -05:00
|
|
|
}
|
2017-12-26 17:28:44 -05:00
|
|
|
updateForegroundNotification();
|
2017-12-16 16:58:43 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-12-16 07:11:02 -05:00
|
|
|
@Override
|
2017-12-16 15:02:10 -05:00
|
|
|
public final void onDestroy() {
|
2017-12-16 16:58:43 -05:00
|
|
|
for (RecursiveFileObserver malwareMonitor : malwareMonitors) {
|
2017-12-16 07:11:02 -05:00
|
|
|
malwareMonitor.stopWatching();
|
|
|
|
}
|
2017-12-16 16:58:43 -05:00
|
|
|
malwareMonitors.clear();
|
|
|
|
System.gc();
|
2018-07-25 21:08:35 -04:00
|
|
|
//Toast.makeText(this, "Hypatia: Realtime Scanning Stopped", Toast.LENGTH_SHORT).show();
|
2017-12-16 07:11:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
private void setForeground() {
|
2017-12-26 17:28:44 -05:00
|
|
|
foregroundNotification =
|
2017-12-16 07:11:02 -05:00
|
|
|
new NotificationCompat.Builder(this)
|
|
|
|
.setSmallIcon(R.drawable.ic_notification)
|
|
|
|
.setContentTitle(getText(R.string.lblNotificationRealtimeTitle))
|
|
|
|
.setContentText(getText(R.string.lblNotificationRealtimeText))
|
|
|
|
.setPriority(Notification.PRIORITY_MIN)
|
2017-12-26 17:28:44 -05:00
|
|
|
.setShowWhen(false);
|
|
|
|
|
2018-04-16 01:06:57 -04:00
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
2018-04-05 21:08:52 -04:00
|
|
|
foregroundNotification.setChannelId("FOREGROUND");
|
|
|
|
}
|
|
|
|
|
2017-12-26 17:28:44 -05:00
|
|
|
startForeground(-1, foregroundNotification.build());
|
|
|
|
}
|
2017-12-16 07:11:02 -05:00
|
|
|
|
2017-12-26 17:28:44 -05:00
|
|
|
private void updateForegroundNotification() {
|
|
|
|
foregroundNotification.setSubText(Utils.FILES_SCANNED + " files scanned");
|
|
|
|
notificationManager.notify(-1, foregroundNotification.build());
|
2017-12-16 07:11:02 -05:00
|
|
|
}
|
|
|
|
|
2018-07-25 21:08:35 -04:00
|
|
|
}
|