Browse Source

Signed-off-by: Caner <40012261+Canees@users.noreply.github.com>

Caner 3 years ago
parent
commit
a9df923d92
6 changed files with 426 additions and 30 deletions
  1. 22 0
      .gitattributes
  2. 209 20
      .gitignore
  3. 0 8
      LICENSE
  4. 17 2
      README.md
  5. 178 0
      main.go
  6. BIN
      winscpwd.exe

+ 22 - 0
.gitattributes

@@ -0,0 +1,22 @@
+# Auto detect text files and perform LF normalization
+* text=auto
+
+# Custom for Visual Studio
+*.cs     diff=csharp
+*.sln    merge=union
+*.csproj merge=union
+*.vbproj merge=union
+*.fsproj merge=union
+*.dbproj merge=union
+
+# Standard to msysgit
+*.doc	 diff=astextplain
+*.DOC	 diff=astextplain
+*.docx diff=astextplain
+*.DOCX diff=astextplain
+*.dot  diff=astextplain
+*.DOT  diff=astextplain
+*.pdf  diff=astextplain
+*.PDF	 diff=astextplain
+*.rtf	 diff=astextplain
+*.RTF	 diff=astextplain

+ 209 - 20
.gitignore

@@ -1,26 +1,215 @@
-# ---> Go
-# Compiled Object files, Static and Dynamic libs (Shared Objects)
-*.o
-*.a
-*.so
+#################
+## Eclipse
+#################
 
 
-# Folders
-_obj
-_test
+*.pydevproject
+.project
+.metadata
+bin/
+tmp/
+*.tmp
+*.bak
+*.swp
+*~.nib
+local.properties
+.classpath
+.settings/
+.loadpath
 
 
-# Architecture specific extensions/prefixes
-*.[568vq]
-[568vq].out
+# External tool builders
+.externalToolBuilders/
 
 
-*.cgo1.go
-*.cgo2.c
-_cgo_defun.c
-_cgo_gotypes.go
-_cgo_export.*
+# Locally stored "Eclipse launch configurations"
+*.launch
 
 
-_testmain.go
+# CDT-specific
+.cproject
 
 
-*.exe
-*.test
-*.prof
+# PDT-specific
+.buildpath
 
 
+
+#################
+## Visual Studio
+#################
+
+## Ignore Visual Studio temporary files, build results, and
+## files generated by popular Visual Studio add-ons.
+
+# User-specific files
+*.suo
+*.user
+*.sln.docstates
+
+# Build results
+
+[Dd]ebug/
+[Rr]elease/
+x64/
+build/
+[Bb]in/
+[Oo]bj/
+
+# MSTest test Results
+[Tt]est[Rr]esult*/
+[Bb]uild[Ll]og.*
+
+*_i.c
+*_p.c
+*.ilk
+*.meta
+*.obj
+*.pch
+*.pdb
+*.pgc
+*.pgd
+*.rsp
+*.sbr
+*.tlb
+*.tli
+*.tlh
+*.tmp
+*.tmp_proj
+*.log
+*.vspscc
+*.vssscc
+.builds
+*.pidb
+*.log
+*.scc
+
+# Visual C++ cache files
+ipch/
+*.aps
+*.ncb
+*.opensdf
+*.sdf
+*.cachefile
+
+# Visual Studio profiler
+*.psess
+*.vsp
+*.vspx
+
+# Guidance Automation Toolkit
+*.gpState
+
+# ReSharper is a .NET coding add-in
+_ReSharper*/
+*.[Rr]e[Ss]harper
+
+# TeamCity is a build add-in
+_TeamCity*
+
+# DotCover is a Code Coverage Tool
+*.dotCover
+
+# NCrunch
+*.ncrunch*
+.*crunch*.local.xml
+
+# Installshield output folder
+[Ee]xpress/
+
+# DocProject is a documentation generator add-in
+DocProject/buildhelp/
+DocProject/Help/*.HxT
+DocProject/Help/*.HxC
+DocProject/Help/*.hhc
+DocProject/Help/*.hhk
+DocProject/Help/*.hhp
+DocProject/Help/Html2
+DocProject/Help/html
+
+# Click-Once directory
+publish/
+
+# Publish Web Output
+*.Publish.xml
+*.pubxml
+
+# NuGet Packages Directory
+## TODO: If you have NuGet Package Restore enabled, uncomment the next line
+#packages/
+
+# Windows Azure Build Output
+csx
+*.build.csdef
+
+# Windows Store app package directory
+AppPackages/
+
+# Others
+sql/
+*.Cache
+ClientBin/
+[Ss]tyle[Cc]op.*
+~$*
+*~
+*.dbmdl
+*.[Pp]ublish.xml
+*.pfx
+*.publishsettings
+
+# RIA/Silverlight projects
+Generated_Code/
+
+# Backup & report files from converting an old project file to a newer
+# Visual Studio version. Backup files are not needed, because we have git ;-)
+_UpgradeReport_Files/
+Backup*/
+UpgradeLog*.XML
+UpgradeLog*.htm
+
+# SQL Server files
+App_Data/*.mdf
+App_Data/*.ldf
+
+#############
+## Windows detritus
+#############
+
+# Windows image file caches
+Thumbs.db
+ehthumbs.db
+
+# Folder config file
+Desktop.ini
+
+# Recycle Bin used on file shares
+$RECYCLE.BIN/
+
+# Mac crap
+.DS_Store
+
+
+#############
+## Python
+#############
+
+*.py[co]
+
+# Packages
+*.egg
+*.egg-info
+dist/
+build/
+eggs/
+parts/
+var/
+sdist/
+develop-eggs/
+.installed.cfg
+
+# Installer logs
+pip-log.txt
+
+# Unit test / coverage reports
+.coverage
+.tox
+
+#Translations
+*.mo
+
+#Mr Developer
+.mr.developer.cfg

+ 0 - 8
LICENSE

@@ -1,8 +0,0 @@
-MIT License
-Copyright (c) <year> <copyright holders>
-
-Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
-
-The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
-
-THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

+ 17 - 2
README.md

@@ -1,3 +1,18 @@
-# wscp-pwd
+winscp 解密公交
+=============================================
+
+
+Steps
+-----
+1. 导出配置
+2. 找到[Sessions\***]所对应的host,username,password
+3. cmd|powershell 执行以下
+
+```sh
+winscpwd.exe <host> <username> <password>
+```
+
+About
+------
+https://github.com/Canees/WinSCP-password-decrypter
 
 
-winscp 解密工具

+ 178 - 0
main.go

@@ -0,0 +1,178 @@
+package main
+
+import (
+	"fmt"
+	"log"
+	"net/url"
+	"os"
+	"os/user"
+	"runtime"
+	"strconv"
+	"strings"
+
+	"gopkg.in/ini.v1"
+)
+
+// WinSCP password encryption/decryption salts.
+const (
+	PasswordMagic = 0xA3
+	PasswordFlag  = 0xFF
+)
+
+func main() {
+	args := os.Args[1:]
+	if len(args) != 3 && len(args) != 2 {
+		// In case provided arguments doesn't match the
+		// application usage, print application usage message.
+		PrintHelp()
+		return
+	}
+
+	if args[0] == "ini" {
+		// In case 'ini' argument was provided,
+		// start the decryption of ini file.
+		var iniPath string
+		if len(args) == 2 {
+			iniPath = args[1]
+		} else {
+			iniPath = GetDefaultWinSCPIniFilePath()
+		}
+		DecryptIni(iniPath)
+		return
+	}
+
+	// In case any argument matches a different operation,
+	// perform the default decryption operation.
+	fmt.Println(Decrypt(args[0], args[1], args[2]))
+}
+
+// PrintHelp prints a help message with instructions about the application usage.
+func PrintHelp() {
+	fmt.Println("WinSCP stored password finder")
+
+	// WinSCP's password manual decryption mode.
+	if runtime.GOOS == "windows" {
+		fmt.Println("Registry:")
+		fmt.Println("  Open regedit and navigate to [HKEY_CURRENT_USER\\Software\\Martin Prikryl\\WinSCP 2\\Sessions] to get the hostname, username and encrypted password")
+		fmt.Println("  Usage winscppasswd.exe <host> <username> <encrypted_password>")
+	} else {
+		fmt.Println("  Usage ./winscppasswd <host> <username> <encrypted_password>")
+	}
+
+	// WinSCP's ini file mode.
+	fmt.Println("\nWinSCP.ini:")
+	if runtime.GOOS == "windows" {
+		fmt.Println("  Usage winscppasswd.exe ini [<filepath>]")
+		fmt.Printf("  Default value <filepath>: %s\n", GetDefaultWinSCPIniFilePath())
+	} else {
+		fmt.Println("  Usage ./winscppasswd ini [<filepath>]")
+	}
+}
+
+// GetDefaultWinSCPIniFilePath obtains default WinSCP configuration file.
+func GetDefaultWinSCPIniFilePath() string {
+	usr, err := user.Current()
+	if err != nil {
+		log.Fatal(err)
+	}
+	return usr.HomeDir + "\\AppData\\Roaming\\winSCP.ini"
+}
+
+// DecryptIni decrypts all entries from a WinSCP's ini file.
+func DecryptIni(filepath string) {
+	cfg, err := ini.InsensitiveLoad(filepath)
+	if err != nil {
+		panic(err)
+	}
+
+	// Print every entry of the configuration that has password field.
+	for _, c := range cfg.Sections() {
+		if c.HasKey("Password") {
+			name, _ := url.PathUnescape(strings.TrimPrefix(c.Name(), "sessions\\"))
+			fmt.Printf("%s\n", name)
+			fmt.Printf("  Hostname: %s\n", c.Key("HostName").Value())
+			fmt.Printf("  Username: %s\n", c.Key("UserName").Value())
+			fmt.Printf("  Password: %s\n", Decrypt(c.Key("HostName").Value(), c.Key("UserName").Value(), c.Key("Password").Value()))
+			fmt.Println("========================")
+		}
+	}
+
+}
+
+// Decrypt decripts a specific server password.
+func Decrypt(host, username, password string) string {
+	// Build 'encryptedPasswordBytes' variable.
+	encryptedPasswordBytes := GetCryptedPasswordBytes(password)
+
+	// Extract 'flag' and 'cryptedPasswordlength' variables
+	flag, encryptedPasswordBytes := DecryptNextCharacter(encryptedPasswordBytes) // decryptNextCharacter alters the encryptedPasswordBytes variable to remove already parsed characters.
+	cryptedPasswordlength, encryptedPasswordBytes := GetCryptedPasswordLength(flag, encryptedPasswordBytes)
+
+	// Build 'clearpass' variable
+	clearpass := GetPassword(cryptedPasswordlength, encryptedPasswordBytes)
+
+	// Apply correction to the 'clearpass' variable.
+	if flag == PasswordFlag {
+		// The clearpass will contians the username, host and password.
+		// Substring username and host from the result password.
+		key := username + host
+		clearpass = clearpass[len(key):]
+	}
+	return clearpass
+}
+
+// GetCryptedPasswordBytes obtains the crypted password byte array.
+func GetCryptedPasswordBytes(password string) []byte {
+	encryptedPasswordBytes := []byte{}
+	for i := 0; i < len(password); i++ {
+		val, _ := strconv.ParseInt(string(password[i]), 16, 8)
+		encryptedPasswordBytes = append(encryptedPasswordBytes, byte(val))
+	}
+	return encryptedPasswordBytes
+}
+
+// GetCryptedPasswordLength obtains crypted password length from crypted password byte array.
+func GetCryptedPasswordLength(flag byte, encryptedPasswordBytes []byte) (byte, []byte) {
+	var cryptedPasswordlength byte = 0
+	if flag == PasswordFlag {
+		_, encryptedPasswordBytes = DecryptNextCharacter(encryptedPasswordBytes)                     // Ignore two characters of the encryptedPasswordBytes.
+		cryptedPasswordlength, encryptedPasswordBytes = DecryptNextCharacter(encryptedPasswordBytes) // decryptNextCharacter alters the encryptedPasswordBytes variable to remove already parsed characters.
+	} else {
+		cryptedPasswordlength = flag
+	}
+	toBeDeleted, encryptedPasswordBytes := DecryptNextCharacter(encryptedPasswordBytes) // decryptNextCharacter alters the encryptedPasswordBytes variable to remove already parsed characters.
+	encryptedPasswordBytes = encryptedPasswordBytes[toBeDeleted*2:]
+	return cryptedPasswordlength, encryptedPasswordBytes
+}
+
+// GetPassword obtains clear password from crypted password byte array
+func GetPassword(cryptedPasswordlength byte, encryptedPasswordBytes []byte) string {
+	var i, character byte
+	var decryptedPassword string
+
+	for i = 0; i < cryptedPasswordlength; i++ {
+		character, encryptedPasswordBytes = DecryptNextCharacter(encryptedPasswordBytes) // decryptNextCharacter alters the encryptedPasswordBytes variable to remove already parsed characters.
+		decryptedPassword += string(character)                                           // Add decrypted character to the result variable.
+	}
+	return decryptedPassword
+}
+
+// DecryptNextCharacter decrypts next character from byte array.
+// Alters the byte array to remove already parsed bytes.
+func DecryptNextCharacter(encryptedPasswordBytes []byte) (byte, []byte) {
+	if len(encryptedPasswordBytes) <= 0 {
+		// In case encryptedPasswordBytes param was empty,
+		// stop the flow here returning '0'.
+		return 0, encryptedPasswordBytes
+	}
+
+	a := encryptedPasswordBytes[0]                      // Obtain first character to parse.
+	b := encryptedPasswordBytes[1]                      // Obtain second character to parse.
+	encryptedPasswordBytes = encryptedPasswordBytes[2:] // Remove already parsed characters.
+	return DecryptCharacter(a, b), encryptedPasswordBytes
+}
+
+// DecryptCharacter decrypts character from two bytes.
+func DecryptCharacter(a, b byte) byte {
+	return ^(((a << 4) + b) ^ PasswordMagic) & PasswordFlag
+}

BIN
winscpwd.exe