Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use AMSI for archive malware scanning #4910

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/actions/spelling/expect.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ alreadyinstalled
AMap
Amd
amrutha
amsi
ansistring
APARTMENTTHREADED
apfn
Expand Down Expand Up @@ -209,6 +210,8 @@ gwgso
gwgus
gwgv
Hackathon
HAMSICONTEXT
HAMSISESSION
hashtables
helplib
helplibrary
Expand Down
3 changes: 3 additions & 0 deletions src/AppInstallerCLITests/AppInstallerCLITests.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,9 @@
<CopyFileToFolders Include="TestData\TestZip.zip">
<DeploymentContent>true</DeploymentContent>
</CopyFileToFolders>
<CopyFileToFolders Include="TestData\TestTarGz.tar.gz">
<DeploymentContent>true</DeploymentContent>
</CopyFileToFolders>
<None Include="Run-TestsInPackage.ps1" />
<CopyFileToFolders Include="TestData\Manifest-Bad-ArchInvalid.yaml">
<DeploymentContent>true</DeploymentContent>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -633,6 +633,9 @@
<CopyFileToFolders Include="TestData\TestZip.zip">
<Filter>TestData</Filter>
</CopyFileToFolders>
<CopyFileToFolders Include="TestData\TestTarGz.tar.gz">
<Filter>TestData</Filter>
</CopyFileToFolders>
<CopyFileToFolders Include="TestData\DownloadFlowTest_DownloadCommandProhibited.yaml">
<Filter>TestData</Filter>
</CopyFileToFolders>
Expand Down
116 changes: 82 additions & 34 deletions src/AppInstallerCLITests/Archive.cpp
Original file line number Diff line number Diff line change
@@ -1,34 +1,82 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
#include "pch.h"
#include "TestCommon.h"
#include <winget/Archive.h>

using namespace AppInstaller::Archive;
using namespace TestCommon;

constexpr std::string_view s_ZipFile = "TestZip.zip";

TEST_CASE("Extract_ZipArchive", "[archive]")
{
TestCommon::TempDirectory tempDirectory("TempDirectory");
TestDataFile testZip(s_ZipFile);

const auto& testZipPath = testZip.GetPath();
const auto& tempDirectoryPath = tempDirectory.GetPath();

HRESULT hr = TryExtractArchive(testZipPath, tempDirectoryPath);

std::filesystem::path expectedPath = tempDirectoryPath / "test.txt";
REQUIRE(SUCCEEDED(hr));
REQUIRE(std::filesystem::exists(expectedPath));
}

TEST_CASE("Scan_ZipArchive", "[archive]")
{
TestDataFile testZip(s_ZipFile);

const auto& testZipPath = testZip.GetPath();
bool result = ScanZipFile(testZipPath);
REQUIRE(result);
}
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
#include "pch.h"
#include "TestCommon.h"
#include "WorkflowCommon.h"
#include <winget/Archive.h>
#include <Workflows/ShellExecuteInstallerHandler.h>

using namespace AppInstaller::Archive;
using namespace AppInstaller::CLI::Workflow;
using namespace AppInstaller::Settings;
using namespace TestCommon;

constexpr std::string_view s_ZipFile = "TestZip.zip";
constexpr std::string_view s_TarGzFile = "TestTarGz.tar.gz";
constexpr std::string_view s_Large7zFile = "TestLarge7z.7z";

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is missing

TEST_CASE("Extract_ZipArchive", "[archive]")
{
TestCommon::TempDirectory tempDirectory("TempDirectory");
TestDataFile testZip(s_ZipFile);

const auto& testZipPath = testZip.GetPath();
const auto& tempDirectoryPath = tempDirectory.GetPath();

HRESULT hr = TryExtractArchive(testZipPath, tempDirectoryPath);

std::filesystem::path expectedPath = tempDirectoryPath / "test.txt";
REQUIRE(SUCCEEDED(hr));
REQUIRE(std::filesystem::exists(expectedPath));
}

TEST_CASE("Scan_ZipArchive", "[archive]")
{
TestDataFile testZip(s_ZipFile);

const auto& testZipPath = testZip.GetPath();
bool result = ScanZipFile(testZipPath);
REQUIRE(result);
}

TEST_CASE("Extract_TarGzArchive", "[archive]")
{
TestCommon::TempDirectory tempDirectory("TempDirectory");
TestDataFile testTarGz(s_TarGzFile);

TestCommon::TestUserSettings testSettings;
testSettings.Set<Setting::ArchiveExtractionMethod>(AppInstaller::Archive::ExtractionMethod::Tar);

const auto& testTarGzPath = testTarGz.GetPath();
const auto& tempDirectoryPath = tempDirectory.GetPath();

ShellExecuteExtractArchive(testTarGzPath, tempDirectoryPath);

std::ostringstream extractOutput;
TestContext context{ extractOutput, std::cin };
context << ShellExecuteExtractArchive(testTarGzPath, tempDirectoryPath);

std::filesystem::path expectedPath = tempDirectoryPath / "test.txt";
REQUIRE(SUCCEEDED(context.GetTerminationHR()));
REQUIRE(std::filesystem::exists(expectedPath));
INFO(extractOutput.str());
}

TEST_CASE("Scan_TarGzArchive", "[archive]")
{
TestDataFile testTarGz(s_TarGzFile);

const auto& testTarGzPath = testTarGz.GetPath();
bool result = ScanZipFile(testTarGzPath);
REQUIRE(result);
}

//TEST_CASE("Scan_Large7zArchive", "[archive]")
//{
// TestDataFile testLarge7z(s_Large7zFile);
//
// const auto& testTarGzPath = testLarge7z.GetPath();
// bool result = ScanZipFile(testTarGzPath);
// REQUIRE(result);
//}

Binary file added src/AppInstallerCLITests/TestData/TestTarGz.tar.gz
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This may be me being paranoid but I would be slightly concerned about having a binary checked without knowing what it is. Would be great if you could share how it gas generated so that we can validate that it's that (assuming it's a deterministic process)

Binary file not shown.
48 changes: 48 additions & 0 deletions src/AppInstallerCommonCore/AppInstallerCommonCore.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,21 @@
<SubSystem Condition="'$(Configuration)|$(Platform)'=='Debug|ARM64'">Windows</SubSystem>
<SubSystem Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">Windows</SubSystem>
</Link>
<Lib>
<AdditionalDependencies Condition="'$(Configuration)|$(Platform)'=='Debug|ARM'">amsi.lib;%(AdditionalDependencies)</AdditionalDependencies>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Huh, I wonder why we don't have the other dependencies here. I think they are listed in the projects that produce the final binaries, like WindowsPackageManager.vcxproj. Maybe we could avoid some duplication by having the dependencies at the point they are introduced...
(Not a comment for this PR)

</Lib>
<Lib>
<AdditionalDependencies Condition="'$(Configuration)|$(Platform)'=='Debug|ARM'">amsi.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Lib>
<Lib>
<AdditionalDependencies Condition="'$(Configuration)|$(Platform)'=='Debug|ARM64'">amsi.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Lib>
<Lib>
<AdditionalDependencies Condition="'$(Configuration)|$(Platform)'=='Debug|ARM64'">amsi.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Lib>
<Lib>
<AdditionalDependencies Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">amsi.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Lib>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Platform)'=='Win32'">
<ClCompile>
Expand All @@ -287,6 +302,9 @@
<Link>
<SubSystem Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">Windows</SubSystem>
</Link>
<Lib>
<AdditionalDependencies Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">amsi.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Lib>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)'=='Release'">
<ClCompile>
Expand Down Expand Up @@ -324,6 +342,18 @@
<SubSystem Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">Windows</SubSystem>
<SubSystem Condition="'$(Configuration)|$(Platform)'=='Release|x64'">Windows</SubSystem>
</Link>
<Lib>
<AdditionalDependencies Condition="'$(Configuration)|$(Platform)'=='Release|ARM'">amsi.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Lib>
<Lib>
<AdditionalDependencies Condition="'$(Configuration)|$(Platform)'=='Release|ARM64'">amsi.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Lib>
<Lib>
<AdditionalDependencies Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">amsi.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Lib>
<Lib>
<AdditionalDependencies Condition="'$(Configuration)|$(Platform)'=='Release|x64'">amsi.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Lib>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)'=='ReleaseStatic'">
<ClCompile>
Expand Down Expand Up @@ -365,6 +395,18 @@
<SubSystem Condition="'$(Configuration)|$(Platform)'=='ReleaseStatic|Win32'">Windows</SubSystem>
<SubSystem Condition="'$(Configuration)|$(Platform)'=='ReleaseStatic|x64'">Windows</SubSystem>
</Link>
<Lib>
<AdditionalDependencies Condition="'$(Configuration)|$(Platform)'=='ReleaseStatic|ARM'">amsi.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Lib>
<Lib>
<AdditionalDependencies Condition="'$(Configuration)|$(Platform)'=='ReleaseStatic|ARM64'">amsi.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Lib>
<Lib>
<AdditionalDependencies Condition="'$(Configuration)|$(Platform)'=='ReleaseStatic|Win32'">amsi.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Lib>
<Lib>
<AdditionalDependencies Condition="'$(Configuration)|$(Platform)'=='ReleaseStatic|x64'">amsi.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Lib>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)'=='Fuzzing'">
<ClCompile>
Expand All @@ -385,6 +427,12 @@
<GenerateWindowsMetadata>false</GenerateWindowsMetadata>
<SubSystem>Windows</SubSystem>
</Link>
<Lib>
<AdditionalDependencies Condition="'$(Configuration)|$(Platform)'=='Fuzzing|Win32'">amsi.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Lib>
<Lib>
<AdditionalDependencies Condition="'$(Configuration)|$(Platform)'=='Fuzzing|x64'">amsi.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Lib>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(WingetDisableTestHooks)'=='true'">
<ClCompile>
Expand Down
Loading
Loading