Skip to content

Commit

Permalink
Fix zip support. Fixes #15
Browse files Browse the repository at this point in the history
To get zip unencrypting to work we have to avoid two false positives.
The first is one that returns a successful exit sats one out of every
255 attempts.  The second one is one that dumps empty files out of the
correctly named content.

To get by this I had to implement it so that empty files will not be
kept.  This may not always be desired behavior and perhaps an
additional fix for the situation where some empty files are wanted
should be implemented.
  • Loading branch information
danielpclark committed Oct 21, 2017
1 parent 0beed0e commit 70cffcf
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 23 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "abrute"
version = "0.1.1"
version = "0.1.2"
authors = ["Daniel P. Clark <[email protected]>"]
description = "AESCrypt Brute force attempter."
documentation = "http:https://danielpclark.github.io/abrute/index.html"
Expand All @@ -12,6 +12,7 @@ keywords = ["crypto","text","cryptography","security"]
categories = ["cryptography"]

[dependencies]
tempdir = "0.3"
clap = "^2.26"
digits = "^0.3.6"
num_cpus = "^1.0"
Expand Down
81 changes: 60 additions & 21 deletions src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@

use std::sync::Mutex;
use digits::Digits;
use std::io::{self, Write};
use std::io::{self, Write, Read};
use std::process::{Command, Output};
use rayon::prelude::*;
use super::result::Error;
extern crate num_cpus;
extern crate tempdir;
use self::tempdir::TempDir;
use std::{fs,path,env};

fn chunk_sequence(d: &mut Digits, adj: Option<&str>) -> Vec<String> {
let qty: usize = num_cpus::get() * 32;
Expand Down Expand Up @@ -45,7 +48,10 @@ fn aes_command(value: &str, target: &str) -> Output {
}

fn unzip_command(value: &str, target: &str) -> Output {
let mut dir = path::PathBuf::from(&target);
dir.pop();
Command::new("unzip").
current_dir(dir).
arg("-u").
arg("-P").
arg(value).
Expand Down Expand Up @@ -117,28 +123,61 @@ pub fn unzip_core_loop<'a>(
adj: Option<&str>
) -> Result<(), Error> {

loop {
has_reached_end(&sequencer, max)?;
progress_report(&sequencer);

let chunk = chunk_sequence(&mut sequencer, adj);
let code: Mutex<Vec<Result<(), Error>>> = Mutex::new(vec![]);

chunk.par_iter().for_each(|ref value|
{
let output = unzip_command(&value, &target);

if output.status.success() {
let mut code_mutex = code.lock().unwrap();
code_mutex.push(Ok(()));
println!("Success!\nPassword is: {}", value);
if let Ok(dir) = TempDir::new("abrute") {
let cwd = env::current_dir().unwrap();
let working = path::Path::new(&dir.path().as_os_str()).join(&target);
fs::copy(&target, &working).unwrap();
assert!(working.is_file());
let target = working.to_str().unwrap();

loop {
has_reached_end(&sequencer, max)?;
progress_report(&sequencer);

let chunk = chunk_sequence(&mut sequencer, adj);
let code: Mutex<Vec<Result<(), Error>>> = Mutex::new(vec![]);

chunk.par_iter().for_each(|ref value|
{
let output = unzip_command(&value, &target);

if output.status.success() {
let mut conspiracy = true;
if fs::read_dir(&dir).unwrap().count() > 1 {
for entry in fs::read_dir(&dir).
expect("Failure reading tempdir's contents.") {
let entry = entry.
expect("Failure reading specific file in tempdir.");

if path::Path::new(&entry.path()) != path::Path::new(&target) {
if fs::File::open(&entry.path()).
expect("Could not open file for validity check in tempdir.").
bytes().count() > 1 {
conspiracy = false;
let file_name = entry.file_name();
let dest_file = path::Path::new(&cwd).join(file_name);

fs::copy(entry.path(), dest_file).
expect("Failure copying file from tempdir.");
}
}
}
if !conspiracy {
let mut code_mutex = code.lock().unwrap();
code_mutex.push(Ok(()));
println!("Success!\nPassword is: {}", value);
}
}
}
}
}
);
);

let mut code = code.lock().unwrap();
if !code.is_empty() {
return code.pop().unwrap();
let mut code = code.lock().unwrap();
if !code.is_empty() {
return code.pop().unwrap();
}
}
} else {
return Err(Error::FailedTempDir);
}
}
8 changes: 8 additions & 0 deletions src/result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use std::error::Error as StdError;
#[derive(Debug)]
pub enum Error {
AescryptMissing,
FailedTempDir,
FileMissing,
InvalidAdjacentNumber,
InvalidCharacterSet,
Expand All @@ -24,6 +25,7 @@ impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
Error::AescryptMissing => f.write_str("AescryptMissing" ),
Error::FailedTempDir => f.write_str("FailedTempDir" ),
Error::FileMissing => f.write_str("FileMissing" ),
Error::InvalidAdjacentNumber => f.write_str("InvalidAdjacentNumber"),
Error::InvalidCharacterSet => f.write_str("InvalidCharacterSet" ),
Expand All @@ -40,6 +42,11 @@ fn aescrypt_missing() -> &'static str {
"aescrypt does not appear to be installed."
}

#[inline]
fn failed_temp_dir() -> &'static str {
"Failed in creating a temp directory to work in."
}

#[inline]
fn file_missing() -> &'static str {
"The target file seems to be missing."
Expand Down Expand Up @@ -79,6 +86,7 @@ impl StdError for Error {
fn description(&self) -> &str {
match *self {
Error::AescryptMissing => aescrypt_missing(),
Error::FailedTempDir => failed_temp_dir(),
Error::FileMissing => file_missing(),
Error::InvalidAdjacentNumber => invalid_adjacent_number(),
Error::InvalidCharacterSet => invalid_character_set(),
Expand Down
Empty file removed test/data/zipped.file
Empty file.
2 changes: 1 addition & 1 deletion test/invalid.test
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ describe "* abrute with failed results"
it "fails with unzip"
folder="$(pwd)"
cd test/data/
../../target/debug/abrute 4 asdfg -z -a 0 -- example.zip >/dev/null
../../target/debug/abrute 4 asdfg -z -a 0 -- example.zip 2>/dev/null
assert unequal $? 0
cd $folder
assert file_absent test/data/zipped.file
Expand Down

0 comments on commit 70cffcf

Please sign in to comment.