2018-04-12 22:28:47 -04:00
|
|
|
/*
|
2020-11-17 10:31:08 -05:00
|
|
|
Hypatia: A realtime malware scanner for Android
|
2019-06-18 13:30:41 -04:00
|
|
|
Copyright (c) 2017-2018 Divested Computing Group
|
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;
|
2021-02-22 11:04:05 -05:00
|
|
|
import android.widget.Toast;
|
|
|
|
|
2019-09-22 21:17:21 -04:00
|
|
|
import androidx.core.app.NotificationCompat;
|
2017-12-16 07:11:02 -05:00
|
|
|
|
|
|
|
import java.io.File;
|
2021-02-22 23:28:07 -05:00
|
|
|
import java.text.NumberFormat;
|
2018-01-20 01:50:37 -05:00
|
|
|
import java.util.HashSet;
|
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) {
|
2021-02-02 09:47:22 -05:00
|
|
|
Utils.setContext(getApplicationContext());
|
2017-12-27 16:02:37 -05:00
|
|
|
malwareMonitors.clear();
|
|
|
|
addMalwareMonitor(Environment.getExternalStorageDirectory().toString());
|
|
|
|
|
2021-09-20 12:59:47 -04:00
|
|
|
int threadCount = Utils.getMaxThreads() + malwareMonitors.size();
|
|
|
|
threadPoolExecutor = Utils.getNewThreadPoolExecutor(threadCount);
|
|
|
|
threadPoolExecutor.execute(() -> Database.loadDatabase(getApplicationContext(), false, Database.signatureDatabases));
|
2017-12-26 17:56:39 -05:00
|
|
|
|
2017-12-27 16:02:37 -05:00
|
|
|
for (final RecursiveFileObserver malwareMonitor : malwareMonitors) {
|
2021-09-20 12:59:47 -04:00
|
|
|
threadPoolExecutor.execute(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();
|
2021-03-02 10:18:44 -05:00
|
|
|
Toast.makeText(this, getString(R.string.lblNotificationRealtimeStopped), 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() {
|
2021-02-22 23:28:07 -05:00
|
|
|
foregroundNotification
|
|
|
|
.setSubText(NumberFormat.getInstance().format(Database.getSignatureCount()) + " sigs" + " • " + getString(R.string.main_files_scanned_count, NumberFormat.getInstance().format(Utils.FILES_SCANNED) + ""));
|
2017-12-26 17:28:44 -05:00
|
|
|
notificationManager.notify(-1, foregroundNotification.build());
|
2017-12-16 07:11:02 -05:00
|
|
|
}
|
|
|
|
|
2018-07-25 21:08:35 -04:00
|
|
|
}
|