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

carves: use full pathnames while creating an archive #7681

Merged
merged 6 commits into from
Aug 4, 2022
Merged
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
27 changes: 23 additions & 4 deletions osquery/carver/carver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,21 @@

#include <osquery/carver/carver.h>
#include <osquery/carver/carver_utils.h>
#include <osquery/core/flags.h>
#include <osquery/core/system.h>
#include <osquery/database/database.h>
#include <osquery/filesystem/fileops.h>
#include <osquery/core/flags.h>
#include <osquery/hashing/hashing.h>
#include <osquery/logger/logger.h>
#include <osquery/remote/serializers/json.h>
#include <osquery/utils/conversions/split.h>
#include <osquery/core/system.h>
#include <osquery/utils/base64.h>
#include <osquery/utils/conversions/split.h>
#include <osquery/utils/json/json.h>
#include <osquery/utils/system/system.h>
#include <osquery/utils/system/time.h>

#include <boost/algorithm/string.hpp>

namespace fs = boost::filesystem;

namespace osquery {
Expand Down Expand Up @@ -232,7 +234,24 @@ std::set<fs::path> Carver::carveAll() {
VLOG(1) << "File does not exist on disk or is subdirectory: " << srcPath;
continue;
}
const auto dstPath = carveDir_ / srcPath.leaf();

fs::path dstPath;
if (srcPath.has_root_name()) {
auto temp = srcPath.string();
boost::erase_first(temp, ":");
dstPath = carveDir_ / fs::path(temp);
} else {
dstPath = carveDir_ / srcPath;
}
if (!fs::exists(dstPath.parent_path())) {
auto ret = fs::create_directories(dstPath.parent_path());
if (!ret) {
VLOG(1) << "Failed to create directories for: "
<< dstPath.parent_path();
continue;
}
}

PlatformFile dst(dstPath, PF_CREATE_NEW | PF_WRITE);
if (!dst.isValid()) {
VLOG(1) << "Destination temporary file is invalid: " << dstPath;
Expand Down
5 changes: 3 additions & 2 deletions osquery/filesystem/file_compression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,8 @@ Status decompress(const boost::filesystem::path& in,
}

Status archive(const std::set<boost::filesystem::path>& paths,
const boost::filesystem::path& out, std::size_t block_size) {
const boost::filesystem::path& out,
std::size_t block_size) {
auto arch = archive_write_new();
if (arch == nullptr) {
return Status(1, "Failed to create tar archive");
Expand All @@ -177,7 +178,7 @@ Status archive(const std::set<boost::filesystem::path>& paths,
PlatformFile pFile(f, PF_OPEN_EXISTING | PF_READ);

auto entry = archive_entry_new();
archive_entry_set_pathname(entry, f.leaf().string().c_str());
archive_entry_set_pathname(entry, f.string().c_str());
archive_entry_set_size(entry, pFile.size());
archive_entry_set_filetype(entry, AE_IFREG);
archive_entry_set_perm(entry, 0644);
Expand Down