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

CTI snapshot unzip tool creation #319

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
## [Unreleased 5.0.x]

### Added
- New tools directory with unzip tool [(#319)](https://github.com/wazuh/wazuh-indexer-plugins/pull/319)

### Dependencies

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.wazuh.contentmanager.util;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.io.*;
import java.util.zip.*;

public class Unzip {

private static final Logger log = LogManager.getLogger(Unzip.class);
private static final byte[] BUFFER = new byte[1024];

/*
* Unzips a ZIP file's content in a specified folder
* @param zipFilePath Origin ZIP file path following the format: "path/file.zip"
* @param destDirectory Unzipped files destiny path following the format: "path/"
*/
public void unzip(String zipFilePath, String destDirectory) throws FileNotFoundException {
File zipFile = new File(zipFilePath);
if (!zipFile.exists() || !zipFile.isFile()) {
log.error("Error, ZIP file does not exist or is invalid: {}", zipFilePath);
throw new FileNotFoundException("Error, ZIP file does not exist or is invalid: " + zipFilePath);
}

try (ZipInputStream zipIn = new ZipInputStream(new FileInputStream(zipFilePath))) {
ZipEntry entry;

while ((entry = zipIn.getNextEntry()) != null) {
File filePath = new File(destDirectory, entry.getName());

Check failure

Code scanning / CodeQL

Arbitrary file access during archive extraction ("Zip Slip") High

Unsanitized archive entry, which may contain '..', is used in a
file system operation
.

Copilot Autofix AI about 6 hours ago

To fix the problem, we need to ensure that the output paths constructed from zip archive entries are validated to prevent writing files to unexpected locations. This can be achieved by normalizing the file path and checking that it starts with the destination directory.

  1. Normalize the file path using toPath().normalize().
  2. Check if the normalized path starts with the destination directory path using startsWith().
  3. If the check fails, throw an exception to prevent writing the file.
Suggested changeset 1
plugins/content-manager/src/main/java/com/wazuh/contentmanager/util/Unzip.java

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/plugins/content-manager/src/main/java/com/wazuh/contentmanager/util/Unzip.java b/plugins/content-manager/src/main/java/com/wazuh/contentmanager/util/Unzip.java
--- a/plugins/content-manager/src/main/java/com/wazuh/contentmanager/util/Unzip.java
+++ b/plugins/content-manager/src/main/java/com/wazuh/contentmanager/util/Unzip.java
@@ -28,3 +28,6 @@
             while ((entry = zipIn.getNextEntry()) != null) {
-                File filePath = new File(destDirectory, entry.getName());
+                File filePath = new File(destDirectory, entry.getName()).toPath().normalize().toFile();
+                if (!filePath.toPath().startsWith(new File(destDirectory).toPath().normalize())) {
+                    throw new IOException("Bad zip entry: " + entry.getName());
+                }
 
EOF
@@ -28,3 +28,6 @@
while ((entry = zipIn.getNextEntry()) != null) {
File filePath = new File(destDirectory, entry.getName());
File filePath = new File(destDirectory, entry.getName()).toPath().normalize().toFile();
if (!filePath.toPath().startsWith(new File(destDirectory).toPath().normalize())) {
throw new IOException("Bad zip entry: " + entry.getName());
}

Copilot is powered by AI and may make mistakes. Always verify output.
Positive Feedback
Negative Feedback

Provide additional feedback

Please help us improve GitHub Copilot by sharing more details about this comment.

Please select one or more of the options

try (BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filePath))) {
int len;
while ((len = zipIn.read(BUFFER)) > 0) {
bos.write(BUFFER, 0, len);
}
} catch (IOException e) {
log.error("Error, {} could not be extracted", filePath);
}
zipIn.closeEntry();
}
} catch (IOException e) {
log.error("Error unzipping the file due to {}", e.getMessage());
}
}
}