我想编写一个程序,以便获得.Smali文件的完整路径(已经在.apk文件上使用APKtool)。
在那之后,您必须输入一个功能或单词。然后,代码应扫描所有.smali文件(如文本文件)并扫描给定的功能或Word。所有包含这些内容的.Smali文件都应保存在新文件中,并打印到控制台。
一般想法是针对给定的功能或Word进行Backslice,并找到.Smali文件使用它们。 (将用于提高安全性或在应用程序中查找安全性问题)
我已经尝试(借助一些找到的代码)扫描路径以查找给定的.smali文件。我现在尝试(使用扫描仪)扫描给定Word的文件。我只希望函数打印并保存.smali文件,其中包含此Word。
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中的注释代码本身可以正常工作。但是使用扫描仪,我现在无法存储被扫描的文件名。)
也许有人知道如何解决问题。
答案 0 :(得分:0)
嗨,要打印所有包含您要查找的单词的文件并将其写入文件,您需要在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());
}
}