How to open encrypted . zip files?

Asked

Viewed 71 times

1

I am trying to open . zip files with password, using existing passwords in other file. txt.

The code function that opens the file:

package main

import (
    "bufio"
    "fmt"
    "os"
    "time"

    "github.com/yeka/zip"
)

//criando constante
const (
    zipPath  = "./assets/batlorem.zip"
    passPath = "./assets/rockyou2.txt"
)

func main() {
    abrirArquivoZip()
}

func abrirArquivoZip() {

    arquivos, err := zip.OpenReader(zipPath)

    if err != nil {
        panic(fmt.Sprintf("Erro: %v, %v\n", err, arquivos))
    }

    defer arquivos.Close()

    arquivo := arquivos.File[0]

    if arquivo.IsEncrypted() {

        listaSenhas := obterListaDeSenhas(passPath)

        timeStart := time.Now()

        for i := 0; i < len(listaSenhas); i++ {

            arquivo.SetPassword(listaSenhas[i])

            _, err := arquivo.Open()

            if err == nil {

                fmt.Printf("\nSenha encontrada: %v\n", listaSenhas[i])
                fmt.Printf("localizada na linha: %v\n", i+1)
                break
            }
        }

        fmt.Printf("\nTempo: %v\n", time.Since(timeStart))
    }
}

The function that creates reads passwords and returns a string array with them:

func obterListaDeSenhas(caminhoArquivo string) (senhas []string) {

    arquivo, err := os.Open(caminhoArquivo)

    if err != nil {
        panic(fmt.Sprintf("Erro: %v\n", err))
    }

    scanner := bufio.NewScanner(arquivo)
    scanner.Split(bufio.ScanLines)

    for scanner.Scan() {
        senhas = append(senhas, scanner.Text())
    }

    defer arquivo.Close()

    arquivoStatus, err := os.Stat(caminhoArquivo)

    if err != nil {
        panic(fmt.Sprintf("Erro: %v\n", err))
    }

    fmt.Printf("Nome Arquivo: %v\n", arquivoStatus.Name())
    fmt.Printf("Tamanho Arquivo: %v\n", arquivoStatus.Size()/1024)
    fmt.Printf("Quantidade de senhas: %v\n", len(senhas))

    return
}

My.txt file with passwords has numerous passwords each in a row, as in the example:

123456
12345
123456789
password
iloveyou
princess

And the.zip file contains any text, the text is not relevant is testing whether the file opens with some of the passwords in . txt.

What I have observed:

The code is not returning error while trying to open the file with incorrect passwords.

I checked that no error is being returned for any of the attempts with incorrect password. What is the reason for this ?

The above code is complete but can be tested more easily here.

  • The question was closed by "not being objective" I would like it to be clarified what is not this objective in the question. 'Cause I think my problem is clear.

  • 1

    Are you sure that IF is going to if arquivo.IsEncrypted() { ? Please, to be able to help just [Dit] create a [mcve] that we can test simply. Do not delete and do not repeat the question, just edit and wait for the reopening process. To take better advantage of the site, understand and avoid closures is worth reading the Stack Overflow Survival Guide in English. Thank you for understanding.

  • From what I understand was missing check: if err != nil the moment it makes the arquivo.Open() ... because that’s probably what’s going on, but I haven’t checked all your code. Just debugging.

  • @Danizavtz In this case I’m hoping the code returns error for all passwords that are different from the correct password so in case it would be if err == nil even as the code must inform with which password he was able to open the file.

  • 1

    Can you reopen the question? I would like to post a solution.

  • @Danizavtz You can vote to reopen the question, missing 3 votes to reopen.

  • I have no reputation. : -/ @water

  • 1

    @water, solution

  • 1

    I ended up putting the condition if err != nil in that snippet of code to make the next loop iteration. This is the moment I call the continue. @water

  • 1

    Thank you. When the question is reopened could you post your explanation please? I don’t understand why arquivo.Open() nay is returning error when trying to open the file with the wrong password. @Danizavtz

  • 1

    What detects the incorrect password is the section that has the ioutil.ReadAll() because that is where there is the error when reading the data. This chunk was missing from your code. I took the code from the lib repository "github.com/yeka/zip"

Show 6 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.