Skip to content

Instantly share code, notes, and snippets.

@ayoubzulfiqar
Last active July 11, 2023 11:32
Show Gist options
  • Save ayoubzulfiqar/15264d380ce09bd788fa3a5362b7c6de to your computer and use it in GitHub Desktop.
Save ayoubzulfiqar/15264d380ce09bd788fa3a5362b7c6de to your computer and use it in GitHub Desktop.
Simple Scripts to Crack winrar Password
package main
import (
"bufio"
"fmt"
"os"
"os/exec"
"strings"
)
func crackRar(rarFilename string, pwdGenerator <-chan string) {
for pwd := range pwdGenerator {
cmd := exec.Command("unrar", "x", "-p"+pwd, rarFilename)
err := cmd.Run()
if err == nil {
fmt.Println("File extracted")
fmt.Printf("The password is %s\n", pwd)
return
}
}
fmt.Println("Sorry, cannot find the password")
}
func fromDictionary(dicName string) <-chan string {
pwdGenerator := make(chan string)
go func() {
file, err := os.Open(dicName)
if err != nil {
fmt.Println("Error opening dictionary file:", err)
close(pwdGenerator)
return
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
pwdGenerator <- scanner.Text()
}
close(pwdGenerator)
}()
return pwdGenerator
}
func main() {
fmt.Println("----------------------------------------------------")
fmt.Println(" RAR Cracker")
fmt.Println(" by @AYOUB")
fmt.Println("----------------------------------------------------")
reader := bufio.NewReader(os.Stdin)
fmt.Print("Please enter the filename: ")
filename, _ := reader.ReadString('\n')
filename = strings.TrimSpace(filename)
for !fileExists(filename) {
fmt.Print("No such file, please enter a valid filename: ")
filename, _ = reader.ReadString('\n')
filename = strings.TrimSpace(filename)
}
var mode string
for mode != "dictionary" && mode != "brute" {
fmt.Print("Please select a working mode [dictionary/brute]: ")
mode, _ = reader.ReadString('\n')
mode = strings.TrimSpace(mode)
}
var pwdGen <-chan string
if mode == "dictionary" {
fmt.Print("Please enter the filename of the dictionary: ")
dicName, _ := reader.ReadString('\n')
dicName = strings.TrimSpace(dicName)
for !fileExists(dicName) {
fmt.Print("No such file, please enter a valid filename: ")
dicName, _ = reader.ReadString('\n')
dicName = strings.TrimSpace(dicName)
}
pwdGen = fromDictionary(dicName)
}
if mode == "brute" {
var letters, symbols, numbers, spaces bool
fmt.Print("Include letters? [yes/no] (default yes) ")
includeLetters, _ := reader.ReadString('\n')
letters = strings.TrimSpace(includeLetters) != "no"
fmt.Print("Include symbols? [yes/no] (default yes) ")
includeSymbols, _ := reader.ReadString('\n')
symbols = strings.TrimSpace(includeSymbols) != "no"
fmt.Print("Include numbers? [yes/no] (default yes) ")
includeNumbers, _ := reader.ReadString('\n')
numbers = strings.TrimSpace(includeNumbers) != "no"
fmt.Print("Include spaces? [yes/no] (default no) ")
includeSpaces, _ := reader.ReadString('\n')
spaces = strings.TrimSpace(includeSpaces) == "yes"
fmt.Print("Min length: ")
var startLength int
fmt.Scanf("%d\n", &startLength)
fmt.Print("Max length: ")
var length int
fmt.Scanf("%d\n", &length)
pwdGen = brute(startLength, length, letters, numbers, symbols, spaces)
}
fmt.Println("Start cracking")
fmt.Println("This may take some time, please wait...")
crackRar(filename, pwdGen)
}
func fileExists(filename string) bool {
_, err := os.Stat(filename)
return err == nil
}
func brute(startLength, length int, letters, numbers, symbols, spaces bool) <-chan string {
pwdGenerator := make(chan string)
go func() {
charset := ""
if letters {
charset += "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
}
if numbers {
charset += "0123456789"
}
if symbols {
charset += "!@#$%^&*()-_=+[]{}|;:',.<>/?`~"
}
if spaces {
charset += " "
}
base := len(charset)
maxCombinations := pow(base, length)
for i := 0; i < maxCombinations; i++ {
password := ""
n := i
for j := 0; j < length; j++ {
password = string(charset[n%base]) + password
n /= base
}
pwdGenerator <- password
if len(password) < length {
break
}
}
close(pwdGenerator)
}()
return pwdGenerator
}
func pow(a, b int) int {
result := 1
for i := 0; i < b; i++ {
result *= a
}
return result
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment