在实施IAmsiStream以使用 Windows Defender 执行扫描时,对于大于 ~20MB 的文件,它会失败并显示Value does not fall within the expected range.
.
这个实现缺少什么?
public class AmsiStream : IAmsiStream
{
private readonly Stream _input;
private readonly string _name;
private static readonly byte[] _nullPtr = new byte[Marshal.SizeOf(IntPtr.Zero)];
public AmsiStream(Stream input, string name)
{
_input = input ?? throw new ArgumentNullException(nameof(input));
_name = name ?? throw new ArgumentNullException(nameof(name));
}
public int GetAttribute(AMSI_ATTRIBUTE attribute, int dataSize, byte[] data, out int retData)
{
const int E_NOTIMPL = unchecked((int)0x80004001);
const int E_NOT_SUFFICIENT_BUFFER = unchecked((int)0x8007007A);
byte[] bytes = { };
int retValue = 0;
switch (attribute)
{
case AMSI_ATTRIBUTE.AMSI_ATTRIBUTE_APP_NAME:
bytes = Encoding.Unicode.GetBytes("TestAmsi" + "\0");
break;
case AMSI_ATTRIBUTE.AMSI_ATTRIBUTE_CONTENT_NAME:
bytes = Encoding.Unicode.GetBytes(_name + "\0");
break;
case AMSI_ATTRIBUTE.AMSI_ATTRIBUTE_CONTENT_SIZE:
bytes = BitConverter.GetBytes((ulong)_input.Length);
break;
case AMSI_ATTRIBUTE.AMSI_ATTRIBUTE_SESSION:
bytes = _nullPtr;
break;
case AMSI_ATTRIBUTE.AMSI_ATTRIBUTE_CONTENT_ADDRESS:
retValue = E_NOTIMPL;
break;
default:
retValue = E_NOTIMPL;
break;
}
retData = 0;
if (retValue == 0)
{
retData = bytes.Length;
if (dataSize < bytes.Length)
return E_NOT_SUFFICIENT_BUFFER;
Array.Copy(bytes, data, bytes.Length);
}
return retValue;
}
public int Read(long position, int size, byte[] buffer, out int readSize)
{
_input.Seek(position, SeekOrigin.Begin);
readSize = _input.Read(buffer, 0, size);
return 0;
}
}
一个测试用例是:
[Fact]
public void TestWithLargeFile()
{
const long k = 1024;
const long m = 1024 * k;
const long fileSize = 21 * m;
var c = new Random(42);
var fileBuffer = new byte[fileSize];
c.NextBytes(fileBuffer);
Equal(AMSI_RESULT.AMSI_RESULT_CLEAN, ScanInternal(new MemoryStream(fileBuffer), "test.file.txt"));
}
private AMSI_RESULT ScanInternal(Stream streamToCheck, string fileName)
{
var scanner = (IAntiMalware)new CAntiMalware();
var stream = new AmsiStream(streamToCheck, fileName);
var result = scanner.Scan(stream, out AMSI_RESULT scanResult, out IAntiMalwareProvider _);
if (result != (ulong)HResult.S_OK)
{
throw new InvalidOperationException($"Malware scan returned not OK: {result}");
}
return scanResult;
}
此 github 存储库中此测试的完整源代码