Commit 7954bfb2 authored by Tatyana's avatar Tatyana 👽

FilePreparer added

some fixes
parent 15a5d95a
......@@ -53,6 +53,18 @@
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.dorkbox</groupId>
<artifactId>PeParser</artifactId>
<version>2.14</version>
</dependency>
<dependency>
<groupId>com.dorkbox</groupId>
<artifactId>PeParser-Dorkbox-Util</artifactId>
<version>2.7</version>
</dependency>
</dependencies>
</project>
\ No newline at end of file
......@@ -2,21 +2,26 @@ package application.controller;
import application.dto.RecordMap;
import application.entity.Record;
import application.util.FilePreparer;
import application.util.FileSearcher;
import application.util.NotificationType;
import application.util.RecordLoader;
import com.jfoenix.controls.JFXButton;
import com.jfoenix.controls.JFXTextArea;
import com.jfoenix.controls.JFXTextField;
import com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader;
import javafx.application.Platform;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Control;
import javafx.scene.control.Hyperlink;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import javafx.scene.paint.Color;
import javafx.stage.DirectoryChooser;
......@@ -36,6 +41,8 @@ public class FxAdminController extends FxController {
private Scene scene;
private Stage window;
private boolean isPE = false;
private final DirectoryChooser directoryChooser = new DirectoryChooser();
public void loadAddSignature(ActionEvent event) throws IOException {
......@@ -172,6 +179,10 @@ public class FxAdminController extends FxController {
}
}
public void filterPe(ActionEvent event) {
isPE = !isPE;
}
public void prepareFiles(ActionEvent event) {
JFXButton button = (JFXButton) event.getSource();
scene = button.getScene();
......@@ -189,13 +200,29 @@ public class FxAdminController extends FxController {
FileSearcher fileSearcher = FileSearcher.getInstance();
Thread fileSearcherThread = new Thread(() -> {
List<File> files = fileSearcher.prepareFiles(root);
List<File> peFiles;
if (isPE) {
peFiles = new ArrayList<>();
for (File file : files) {
if (FilePreparer.isPE(file))
peFiles.add(file);
}
files.clear();
files.addAll(peFiles);
}
List<String> filesItems = files.stream().map(file -> file.toString()).collect(Collectors.toList());
ObservableList<String> items = FXCollections.observableArrayList(filesItems);
preparedFilesList.setItems(items);
Platform.runLater(() -> {
ObservableList<String> items = FXCollections.observableArrayList(filesItems);
preparedFilesList.setItems(items);
});
});
fileSearcherThread.setName("File Searcher");
fileSearcherThread.start();
} else {
notificate("Invalid dirpath!", NotificationType.ERROR);
}
}
}
}
\ No newline at end of file
package application.util;
import java.io.*;
public class FilePreparer {
public static boolean isPE(File file) {
try (InputStream is = new FileInputStream(file)) {
// take e_lfanew value on 3C offset that contains PE address
// extra 4 bytes stand for DWORD data type
int e_lfanew = Integer.parseInt("3C", 16) + 4;
byte[] pe = new byte[e_lfanew];
if (is.read(pe, 0, e_lfanew) < 64)
return false;
// take DWORD (4bytes) of e_lfanew value
StringBuilder sb = new StringBuilder();
sb.append((char) pe[0]);
sb.append((char) pe[1]);
if (!sb.toString().equals("MZ"))
return false;
sb.setLength(0);
// TODO neg arr size ex
for (int i = e_lfanew - 1; i != e_lfanew - 1 - 4; i--) {
sb.append(String.format("%02X", pe[i]));
}
// convert from HEX to dec offset of PE header
int peOffset = Integer.parseInt(sb.toString(), 16);
// bytes left to read in order to get to PE header
int offsetToPe = peOffset - e_lfanew;
// read 'em
byte[] signature = new byte[offsetToPe];
is.read(signature, 0, offsetToPe);
// and finally get PE signature
byte[] pe_header = new byte[2];
is.read(pe_header, 0, 2);
sb.setLength(0);
for (byte p : pe_header) {
sb.append((char) p);
}
if (sb.toString().equals("PE"))
return true;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (NumberFormatException e) {
return false;
}
return false;
}
}
......@@ -7,10 +7,11 @@ import java.util.List;
public class FileSearcher {
private static FileSearcher instance;
private FileMonitor monitor = new FileMonitor();
private FileMonitor monitor;
private List<File> monitoredFiles = new ArrayList<>();
public List<File> prepareFiles(File root) {
monitor = new FileMonitor();
monitor.start();
monitor.registerDir(root);
List<File> foundedFiles = scanDirectory(root);
......
......@@ -3,6 +3,7 @@
<?import com.jfoenix.controls.JFXButton?>
<?import com.jfoenix.controls.JFXListView?>
<?import com.jfoenix.controls.JFXTextField?>
<?import com.jfoenix.controls.JFXToggleButton?>
<?import de.jensd.fx.glyphs.fontawesome.FontAwesomeIcon?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Hyperlink?>
......@@ -163,7 +164,7 @@
<Font name="Calibri Light" size="20.0" />
</font>
</Label>
<Label layoutX="659.0" layoutY="494.0" text="0">
<Label fx:id="filesFound" layoutX="659.0" layoutY="494.0" text="0">
<font>
<Font name="Calibri Light" size="20.0" />
</font>
......@@ -184,6 +185,7 @@
<Font name="Calibri Light" size="22.0" />
</font>
</Label>
<JFXToggleButton layoutX="598.0" layoutY="387.0" mnemonicParsing="false" onAction="#filterPe" text="PE" />
</children>
</Pane>
</VBox>
......
......@@ -58,6 +58,24 @@
<Font name="Calibri Light Italic" size="19.0" />
</font>
</Label>
<VBox prefHeight="150.0" prefWidth="724.0">
<children>
<Pane prefHeight="200.0" prefWidth="200.0" style="-fx-background-color: #f9f9f9;">
<children>
<Label layoutX="50.0" layoutY="38.0" text="Your device is being protected">
<font>
<Font name="Calibri Light" size="32.0" />
</font>
</Label>
<Label layoutX="50.0" layoutY="98.0" text="Last health scan: 04/04/2019">
<font>
<Font name="Calibri Light" size="19.0" />
</font>
</Label>
</children>
</Pane>
</children>
</VBox>
</children>
</Pane>
</children>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment