对于练习,我必须在字节数组中寻找特定的字节模式,这很容易,但我想知道代码是否可以简化甚至优化:
package anti_virus;
import java.nio.file.Files;
import java.nio.file.Paths;
public class Main {
public static void main(String[] args) throws Exception {
byte[] virus = Files.readAllBytes(Paths.get("C:/Users/Nick/Desktop/Uni/infected.com"));
byte[] payload = new byte[]{0x56, 0x69, 0x72, 0x75, 0x73, (byte)0xB4, 0x40, (byte) 0xBB, 0x01,
0x00, (byte) 0xB9, 0x05, 0x00, (byte) 0xBA, 0x0, 0x0, (byte) 0xCD, 0x21};
// payload[14] and payload[14] have varying values
for (int i = 0; i < virus.length; i++) {
if ((virus[i] == payload[0]) && (virus[i+1] == payload[1]) && (virus[i+2] == payload[2]) &&
(virus[i+3] == payload[3]) && (virus[i+4] == payload[4]) && (virus[i+5] == payload[5]) &&
(virus[i+6] == payload[6]) && (virus[i+7] == payload[7]) && (virus[i+8] == payload[8]) &&
(virus[i+9] == payload[9]) && (virus[i+10] == payload[10]) && (virus[i+11] == payload[11]) &&
(virus[i+12] == payload[12]) && (virus[i+13] == payload[13]) && (virus[i+16] == payload[16]) &&
(virus[i+17] == payload[17])) {
System.out.println("This file is probably a Virus!");
return;
}
}
System.out.println("This file is no Virus.");
}
}
是的,它可以被简化/优化:
- 您可以使用KMP算法(前14字节)。该算法对任意
payload
而不是O(payload.length * virus.length)
在O(payload.length + virus.length)
中运行。(你的代码比O(payload.length * virus.length)
工作更有效,只有一个原因:0x56
只作为你的数组的第一个元素发生) - 即使你选择保留你的算法,我也会使用循环来使代码更短&更具可读性。我还会在你的循环中修复
ArrayIndexOutOfBoundsException
的来源(你可以访问virus
数组的索引i, ..., i+13, i+16, i+17
,你的循环条件允许i
和virus.length-1
一样大)。
你的代码非常好,它在一个没有病毒的6 MB文件上给出了合理的21 ms。但我发现最好是为前14个字节做一些预循环。此外,您必须注意末尾的字节。
begin = System.currentTimeMillis();
for (i = 0; i < virus.length-payload.length; i++) {
for (j = 0; j < 14; j++) {
// payload[14] and payload[15] have varying values
if (virus[i+j] != payload[j]) {
bFound = false;
break;
}
}
if ((bFound) && (virus[i+16] == payload[16]) && (virus[i+17] == payload[17])) {
end = System.currentTimeMillis();
System.out.println("time : "+(end-begin)+" ms");
System.out.println("This file is probably a Virus!");
return;
}
}
end = System.currentTimeMillis();
System.out.println("time : "+(end-begin)+" ms");
System.out.println("This file is not a Virus.");
第一个优化给出了一个合理的14 ms(-33%的CPU)。
另一个优化,如果你能负担得起读取你的文件作为整数,是进行宽比较(4字节)一次。您还应该将有效载荷填充为4的倍数。
begin = System.currentTimeMillis();
for (i = 0; i < virusInt.length-payloadInt.length; i++) {
if ((virusInt[i] == payloadInt[0]) &&
(virusInt[i+1] == payloadInt[1]) &&
(virusInt[i+2] == payloadInt[2]) &&
((virusInt[i+3]&0xFFFF0000) == payloadInt[3]) &&
((virusInt[i+4]&0xFFFF0000) == payloadInt[4])) {
end = System.currentTimeMillis();
System.out.println("time : "+(end-begin)+" ms");
System.out.println("This file is probably a Virus!");
return;
}
}
end = System.currentTimeMillis();
System.out.println("time : "+(end-begin)+" ms");
System.out.println("This file is not a Virus.");
这给了我一个更合理的2毫秒(-90%的CPU)。当然,我没有计算转换成int型数组的时间因为我假设你加载的是int型数组而你的负载也是int型数组。我没有尝试过长(在JAVA中是64位),但它可能会快一点。
(这里我假设病毒是病毒签名,并且负载任何数据。我可能看错了你的代码)
必须遍历[0,payload]中paylöadIndex的负载数组。长度-病毒。长度](!),并在for循环中每一步再次检查病毒数组,使用virusIndex。
问题解决策略。想想你在纸上是怎么写的。您将在有效负载数组上移动病毒数组。
类似这样的代码将检查数组中任何地方的签名,它还没有经过彻底的测试,尽管
public static void main(String[] args) throws Exception {
byte[] virus = FileUtil.readBytes(new File("c:/x.txt"));
byte[] payload = "def".getBytes();
for (int i = 0; i < virus.length; i++) {
if ((i + payload.length) <= virus.length) {
boolean found = true;
for (int j = 0; j < payload.length; j++) {
if (virus[i + j] != payload[j]) {
found = false;
break;
}
}
if (found) {
System.out.println("This file is probably a Virus!");
return;
}
} else {
break;
}
}
System.out.println("This file is no Virus.");
}