Monday, March 16, 2015

Zip password unlocker vb

Zip password unlocker vb

To unpack a zip file protected by a password, there is the brutal force attack. Of course, if you ask for the password, it would be easier.
First, I will show basically how the brutal force attacks program work. Later on, I will try to improve a little bit the basic program.

What is the brutal force attack?

The brutal force attack consist or testing every possibilities to unlock a security. For our example, our zip file is protected by a password. That password could be anything: iloveyou, monkey1234, Sup3rMan, Sp0ck….

The password protected zip file.

For education purpose, I will place a protected zip file at the end of this article. Of course, you could make your own protected zip file. My file will be What_is_the_password.zip

The basic algorithm

If I want to test every possibility, I will sweep all possible combinations start from:
1, 2, 3, 4, 5, 6, 7, 8, 9, 0, a, b, c, d….
1a, 1b, 1c, 1d, …
1aa, 1ab, 1ac, 1ad, ….
9aa, 9ab, 9ac, 9ad, …
Iloveyou1, iloveyou2, iloveyou3, iloveyou4, …

To the end of the world. The longer is the password, the harder is. Also, the higher number of possible character there is, the harder your password will be to be break.

The basic vb code

The tokens

In this example, I will create a generic collection of numbers and letter. I will name JETON.

        Dim Jeton As Collection
    Private Sub CreateTokenArray()
        Jeton = New Collection
        For index1 = 0 To 9 'les chiffres (numbers)
            Jeton.Add(index1.ToString)
        Next

        For index1 = 97 To 123 ' lettres minuscules (lower case letters)
            Jeton.Add(index1)
        Next
    End Sub

The tester

I will use dotnetzip, an opensource librairie to test the password:
      Public Sub Test(sPassword As String, Optional ByRef aFilneme As String = "c:\temp\What_is_the_password.zip")
        Using zip As ZipFile = ZipFile.Read(aFilneme)
            'just try to open the first file
            zip.Item(0).ExtractWithPassword("c:\temp\", Ionic.Zip.ExtractExistingFileAction.OverwriteSilently, sPassword)
        End Using
    End Sub

The main program

You need to loop thought over all possibilities. Here is the simple case for a password length of 1 character.
    
Private Sub LoopPassword1()
        Dim index1 As Integer
        Dim sPassword As String = String.Empty
        Dim quitter As Boolean = False
        Dim bfound As Boolean = False

        CreateTokenArray()

        quitter = False
        While quitter = False ' to quit
            For index1 = 1 To Jeton.Count
                Try
                    sPassword = Jeton(index1)
                    Test(sPassword, TextBox1.Text) 'send exception if password fails
                    index1 = Jeton.Count + 1
                    quitter = True 'ready to quit
                    bfound = True
                Catch ex As Exception

                End Try
            Next
            quitter = True
        End While
       
    End Sub

Now if you want to test everything from 1 character to 4 characters, you could do 4 sequels of 4 loops.
    

Private Sub LoopPassword1()
        Dim index1, index2, index3, index4 As Integer
        Dim sPassword As String
        Dim quitter As Boolean = False
        Dim bfound As Boolean = False

        CreateTokenArray()

        quitter = False
        While quitter = False ' to quit
            For index1 = 1 To Jeton.Count
                Try
                    sPassword = Jeton(index1)
                    Test(sPassword, TextBox1.Text) 'send exception if password fails
                    index1 = Jeton.Count + 1
                    quitter = True 'ready to quit
                    bfound = True
                Catch ex As Exception

                End Try
            Next
            quitter = True
        End While

        If bfound = False Then
            quitter = False
            While quitter = False ' to quit
                For index2 = 1 To Jeton.Count
                    For index1 = 1 To Jeton.Count
                        Try
                            sPassword = Jeton(index2) + Jeton(index1)
                            Test(sPassword, TextBox1.Text) 'send exception if password fails
                            index1 = Jeton.Count + 1
                            index2 = Jeton.Count + 1
                            quitter = True 'ready to quit
                            bfound = True
                        Catch ex As Exception

                        End Try
                    Next
                Next
                quitter = True
            End While
        End If
       

       If bfound = False Then
            quitter = False
            While quitter = False ' to quit
                For index3 = 1 To Jeton.Count

                    For index2 = 1 To Jeton.Count
                        For index1 = 1 To Jeton.Count
                            Try
                                sPassword = Jeton(index3) + Jeton(index2) + Jeton(index1)
                                Test(sPassword, TextBox1.Text) 'send exception if password fails
                                index1 = Jeton.Count + 1
                                index2 = Jeton.Count + 1
                                index3 = Jeton.Count + 1

                                quitter = True 'ready to quit
                                bfound = True
                            Catch ex As Exception

                            End Try
                        Next
                    Next
                Next
                quitter = True
            End While
        End If

      If bfound = False Then
            quitter = False
            While quitter = False ' to quit
                For index4 = 1 To Jeton.Count
                    For index3 = 1 To Jeton.Count
                        For index2 = 1 To Jeton.Count
                            For index1 = 1 To Jeton.Count
                                Try
                                    sPassword = Jeton(index4) + Jeton(index3) + Jeton(index2) + Jeton(index1)
                                    Test(sPassword, TextBox1.Text) 'send exception if password fails
                                    index1 = Jeton.Count + 1
                                    index2 = Jeton.Count + 1
                                    index3 = Jeton.Count + 1
                                    index4 = Jeton.Count + 1

                                    quitter = True 'ready to quit
                                    bfound = True
                                Catch ex As Exception

                                End Try
                            Next
                        Next
                    Next
                Next
                quitter = True
            End While
        End If

       If bfound Then
            Label1.Text = "the password is " + sPassword
        Else
            Label1.Text = "the password is not found (password length =1)"
        End If

    End Sub

References: