Exploit database

GH-a3d0db9bad9aad6eca3a3c960ab2ffa3

alexandreheroux/fix-cve-2021-44228

GitHubNot listedPoC
Download repository ZIP

Sources for CVE-2021-44228

465 sources

Preferred order: Exploit-DB, Metasploit, then GitHub repositories by stars. Open preferred source

Repository files2

Files from this repository share one source choice. Supporting files may cover other vulnerabilities.

src/main/java/com/infinisolution/fix/BaseFix.java

Suggested starting fileJava · 6,792 bytes
package com.infinisolution.fix;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Collection;
import java.util.HashSet;
import java.util.jar.JarEntry;
import java.util.jar.JarInputStream;
import java.util.jar.JarOutputStream;
import java.util.zip.ZipEntry;

/**
 * General method to scan
 * 
 * @author Alex
 *
 */
public abstract class BaseFix {
	public static final String[] EXTENSIONS = { ".zip", ".ear", ".war", ".jar" };

	public void run(File root) throws FileNotFoundException, IOException {
		Collection<BaseFix.ArchiveFix> fix;
		long time = System.currentTimeMillis();
		System.out.println("Starting scan of " + (root));
		fix = scan(root);
		System.out.println("Number of fixed files " + fix.size());
		for (BaseFix.ArchiveFix f : fix)
			System.out.println("\t" + f.getFixedFile());
		System.out.println("Time " + (System.currentTimeMillis() - time) + " milliseconds");
	}

	public Collection<ArchiveFix> scan(File current) throws FileNotFoundException, IOException {
		Collection<ArchiveFix> fix = new HashSet<BaseFix.ArchiveFix>();
		if (current.isDirectory()) {
			for (File c : current.listFiles()) {
				fix.addAll(scan(c));
			}
		} else if (current.isFile() && isArchive(current.getName())) {
			fix.addAll(scanArchive(current));
		}
		return fix;
	}

	public Collection<ArchiveFix> scanArchive(File file) throws FileNotFoundException, IOException {
		InputStream in = new BufferedInputStream(new FileInputStream(file));
		Collection<ArchiveFix> childs;
		File fixed;
		Collection<ArchiveFix> fix = new HashSet<BaseFix.ArchiveFix>();
		try {
			childs = scanArchive(in);
		} finally {
			in.close();
		}
		for (ArchiveFix f : childs) {
			// replace file on fileSystem
			in = new BufferedInputStream(new FileInputStream(file));
			try {
				fixed = fixArchive(in, file.getName(), f);
			} finally {
				in.close();
			}
			if (!file.delete() || !fixed.renameTo(file)) {
				throw new IOException("cannot fix " + file);
			}
			fix.add(new ArchiveFix(file, file.getName()));
		}
		return fix;
	}

	public Collection<ArchiveFix> scanArchive(InputStream in) throws FileNotFoundException, IOException {
		JarInputStream jarIn = new JarInputStream(in);
		ZipEntry entry;
		Collection<ArchiveFix> fix = new HashSet<BaseFix.ArchiveFix>();
		File tempFile;
		do {
			entry = jarIn.getNextEntry();
			if (entry != null) {
				// Check if archive contains child
				if (isArchive(entry.getName())) {
					tempFile = streamOut(jarIn, entry.getName());
					for (ArchiveFix e : scanArchive(tempFile))
						fix.add(new ArchiveFix(e.fixedFile, entry.getName()));
				} else if (isMaliciousFile(entry)) {
					fix.add(new ArchiveFix(null, entry.getName()));
				}
			}
		} while (entry != null);
		return fix;
	}

	public File streamOut(InputStream in, String currentFileName) throws IOException {
		byte[] buffer = new byte[255];
		File target = File.createTempFile(currentFileName, ".tmp");
		OutputStream out = new BufferedOutputStream(new FileOutputStream(target));
		int len;
		try {
			target.deleteOnExit();
			do {
				len = in.read(buffer);
				if (len >= 0) {
					out.write(buffer, 0, len);
				}
			} while (len >= 0);
		} finally {
			out.close();
		}
		return target;
	}

	public File fixArchive(InputStream in, String currentFileName, ArchiveFix child) throws IOException {
		File target = File.createTempFile(currentFileName, ".tmp");
		JarInputStream jarIn = new JarInputStream(in);
		JarOutputStream jarOut = new JarOutputStream(new BufferedOutputStream(new FileOutputStream(target)),
				jarIn.getManifest());
		ZipEntry entry;
		// Copy stream
		try {
			target.deleteOnExit();
			do {
				entry = jarIn.getNextEntry();
				if (entry != null) {
					// Check if archive contains child
					if (child.internalFile.equals(entry.getName())) {
						if (child.fixedFile != null) {
							jarOut.putNextEntry(new JarEntry(entry.getName()));
							streamIn(child.fixedFile, jarOut);
							jarOut.closeEntry();

						}
					} else {
						jarOut.putNextEntry(entry);
						jarOut.write(jarIn.readAllBytes());
						jarOut.closeEntry();
					}
				}
			} while (entry != null);
		} finally {
			jarOut.close();
		}
		return target;
	}

	public void streamIn(File source, OutputStream out) throws IOException {
		byte[] buffer = new byte[255];
		InputStream in = new BufferedInputStream(new FileInputStream(source));
		int len;
		try {
			do {
				len = in.read(buffer);
				if (len >= 0) {
					out.write(buffer, 0, len);
				}
			} while (len >= 0);
		} finally {
			in.close();
		}

	}

	public boolean isArchive(String name) {
		boolean match = false;
		if (name != null)
			for (String ext : EXTENSIONS) {
				if (match = name.endsWith(ext))
					break;
			}
		return match;
	}

	public abstract String[] getMaliciousFile();

	public boolean isMaliciousFile(ZipEntry entry) {
		boolean match = false;
		String fullName;
		if (entry != null && entry.getName() != null) {
			fullName = entry.getName();
			for (String file : getMaliciousFile()) {
				if (match = file.equalsIgnoreCase(fullName))
					break;
			}
		}
		return match;
	}

	/**
	 * Archive detail
	 * 
	 * @author Alex
	 *
	 */
	public static class ArchiveFix {
		private File fixedFile;
		private String internalFile;

		public ArchiveFix(File fixedFile, String internalFile) {
			super();
			this.fixedFile = fixedFile;
			this.internalFile = internalFile;
		}

		public File getFixedFile() {
			return fixedFile;
		}

		public String getInternalFile() {
			return internalFile;
		}

		@Override
		public int hashCode() {
			final int prime = 31;
			int result = 1;
			result = prime * result + ((fixedFile == null) ? 0 : fixedFile.hashCode());
			result = prime * result + ((internalFile == null) ? 0 : internalFile.hashCode());
			return result;
		}

		@Override
		public boolean equals(Object obj) {
			if (this == obj)
				return true;
			if (obj == null)
				return false;
			if (getClass() != obj.getClass())
				return false;
			ArchiveFix other = (ArchiveFix) obj;
			if (fixedFile == null) {
				if (other.fixedFile != null)
					return false;
			} else if (!fixedFile.equals(other.fixedFile))
				return false;
			if (internalFile == null) {
				if (other.internalFile != null)
					return false;
			} else if (!internalFile.equals(other.internalFile))
				return false;
			return true;
		}
	}
}