java - 使用 Java 扫描文件并根据其内容过滤它们(反向切片)

标签 java java.util.scanner slice

我想编写一个程序,它将获得 .Smali 文件的完整路径(已经在 .apk 文件上使用 APKtool)。

之后,您应该输入一个函数或单词。然后代码应该扫描所有 .smali 文件(如文本文件)并扫描给定的函数或 Word。包含这些内容的所有 .Smali 应保存在新文件中并打印到控制台。

总体思路是对给定的函数或 Word 进行 Backslice,并查找哪些 .Smali 文件使用这些文件。 (将用于提高安全性或发现应用程序中的安全问题)

我已经尝试(在一些找到的代码的帮助下)扫描给定 .smali 文件的路径。我现在尝试扫描文件中的给定单词(使用扫描仪)。我只想要打印并保存包含此 Word 的 .smali 文件的功能。


import java.io.File;
import java.io.FileFilter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.List;


 
public class Final
{
    public void getFiles(String dir) throws IOException {
        File directory = new File(dir);
        //Verify if it is a valid file name
        if (!directory.exists()) {
            System.out.println(String.format("Directory %s does not exist", dir));
            return;
        }
        //Verify if it is a directory and not a file path
        if (!directory.isDirectory()) {
            System.out.println(String.format("Provided value %s is not a directory", dir));
            return;
        }
      //create a FileFilter and override its accept-method
        FileFilter logFilefilter = new FileFilter() {
                                       //Override accept method
                                       public boolean accept(File file) {
                                          //if the file extension is .smali return true, else false
                                          if (file.getName().endsWith(".smali")) {
                                             return true;
                                          }
                                          return false;
                                       }
                                    };
        
        File[] files = directory.listFiles(logFilefilter);

        // create an additional scanner to store the userInput (the word you want to find).
        Scanner userInput = new Scanner(System.in);
        System.out.println("Please enter the word that you want to find in the .smali files: ");
        String seekWord = userInput.nextLine(); //this line will store the line you are looking for
        userInput.close();

        // create an arrayList to store all the files that contain the word.
        List<File> filesWithWord = new ArrayList<>();

        //Let's list out the filtered files
        for (File f : files) {
            Scanner sc = new Scanner(f);
            while (sc.hasNext()) {
                // look in each line and if the line contains the word store the file.
                String word = sc.nextLine();
                if (word.contentEquals(seekWord)) {
                    System.out.println(f.getName());
                    filesWithWord.add(f);
                    continue; // no need to go through the rest of the lines.
                }

            }
            sc.close();
        }
        // create another file to store the results
        File fileWithFoundFiles = new File("C:\\examplePath\\results.txt");
        //make sure the parents of the file exist.
        fileWithFoundFiles.getParentFile().mkdirs();
        if (!fileWithFoundFiles.exists()) {
            fileWithFoundFiles.createNewFile();
        }
        try (FileWriter writer = new FileWriter(fileWithFoundFiles)) { //try-with resources so your resource gets closed automatically
            for (File f : filesWithWord) {
                //write the fileNames to the file
                writer.write(f.getName() + "\n");
            }
        } catch (Exception e) {
          System.out.println(e.getMessage()); 
       }
        
        
        
    }
    
     public static void main(String[] args) throws IOException
       {
         Final Final = new Final();
         Final.getFiles("C:\\examplePath");
       }
}

到目前为止,代码仅显示给定路径中的所有 Smali 文件。我不擅长编码,不知道如何实现扫描所有 .smali 文件的过滤器。 ( getFiles.class 中的注释代码本身可以正常工作。但是使用扫描仪,我无法存储现在扫描的文件名)。

也许有人知道如何解决这个问题。

最佳答案

您好,为了打印包含您正在查找的单词的所有文件并将它们写入文件,您需要在 getFiles 方法中进行一些更改:

 public void getFiles(String dir) throws IOException {
        File directory = new File(dir);
        //Verify if it is a valid file name

        System.out.println("Looking for files in directory: " + dir);
        if (!directory.exists()) {
            System.out.println(String.format("Directory %s does not exist", dir));
            return;
        }
        //Verify if it is a directory and not a file path
        if (!directory.isDirectory()) {
            System.out.println(String.format("Provided value %s is not a directory", dir));
            return;
        }
        System.out.printf("files found in dir: %s%n", Arrays.asList(directory.listFiles()));
        //create a FileFilter and override its accept-method
        FileFilter logFilefilter = new FileFilter() {
            //Override accept method
            public boolean accept(File file) {
                //if the file extension is .smali return true, else false
                if (file.getName().endsWith(".smali")) {
                    return true;
                }
                return false;
            }
        };

        File[] files = directory.listFiles(logFilefilter);

        // create an additional scanner to store the userInput (the word you want to find).
        Scanner userInput = new Scanner(System.in);
        System.out.println("Please enter the word that you want to find in the .smali files: ");
        String seekWord = userInput.nextLine(); //this line will store the line you are looking for
        userInput.close();

        // create an arrayList to store all the files that contain the word.
        List<File> filesWithWord = new ArrayList<>();



        //Let's list out the filtered files
        for (File f : files) {
            Scanner sc = new Scanner(f);
            while (sc.hasNext()) {
                // look in each line and if the line contains the word store the file.
                String line = sc.nextLine();
                if (line.contains(seekWord)) {
                    System.out.println("found word " + seekWord + " in file: " + f.getName());
                    filesWithWord.add(f);
                    break; // no need to go through the rest of the lines.
                }

            }
            sc.close();
        }

        System.out.println("Smali files with the word: " + filesWithWord);
        // create another file to store the results
        File fileWithFoundFiles = new File("path/to/file/files.txt");
        //make sure the parents of the file exist.
        fileWithFoundFiles.getParentFile().mkdirs();
        if (!fileWithFoundFiles.exists()) {
            fileWithFoundFiles.createNewFile();
        }
        try (FileWriter writer = new FileWriter(fileWithFoundFiles)) { //try-with resources so your resource gets closed automatically
            for (File f : filesWithWord) {
                //write the fileNames to the file
                System.out.println("writing file name " + f.getName() + " to " + f);
                writer.write(f.getName() + "\n");
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }


    }

关于java - 使用 Java 扫描文件并根据其内容过滤它们(反向切片),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56609304/

相关文章:

java - 如何从InputStream中查看nextInt?

java - 使用文件的凯撒密码

json - Elasticsearch 使用 Golang Beats 返回原始 JSON

python - 迭代列表的不同方法

java - java HashMap 的统计信息

java - Spring Boot 设置包含通过环境变量的配置文件

Java 扫描器令人头疼

python - 将切片变为范围

java - 如何将 expandablelistview 的默认扩展限制设置为 4?

java - 返回一个通用的空列表