mirror of
https://github.com/minio/minio.git
synced 2025-03-06 08:40:08 -05:00
Extract all files from encrypted stream with inspect (#20937)
Allow multiple private keys and extract all files from streams. Place files in the folder with `.enc` removed. Do basic checks so streams cannot traverse outside of the folder.
This commit is contained in:
parent
727a803bc0
commit
b312f13473
@ -22,6 +22,9 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
|
"unicode/utf8"
|
||||||
|
|
||||||
"github.com/minio/madmin-go/v3/estream"
|
"github.com/minio/madmin-go/v3/estream"
|
||||||
)
|
)
|
||||||
@ -30,18 +33,18 @@ type keepFileErr struct {
|
|||||||
error
|
error
|
||||||
}
|
}
|
||||||
|
|
||||||
func extractInspectV2(pk []byte, r io.Reader, w io.Writer, okMsg string) error {
|
func extractInspectV2(pks [][]byte, r io.Reader, extractDir string) error {
|
||||||
privKey, err := bytesToPrivateKey(pk)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("decoding key returned: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
sr, err := estream.NewReader(r)
|
sr, err := estream.NewReader(r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
for _, pk := range pks {
|
||||||
|
privKey, err := bytesToPrivateKey(pk)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("decoding key returned: %w", err)
|
||||||
|
}
|
||||||
sr.SetPrivateKey(privKey)
|
sr.SetPrivateKey(privKey)
|
||||||
|
}
|
||||||
sr.ReturnNonDecryptable(true)
|
sr.ReturnNonDecryptable(true)
|
||||||
|
|
||||||
// Debug corrupted streams.
|
// Debug corrupted streams.
|
||||||
@ -60,33 +63,29 @@ func extractInspectV2(pk []byte, r io.Reader, w io.Writer, okMsg string) error {
|
|||||||
return errors.New("no data found on stream")
|
return errors.New("no data found on stream")
|
||||||
}
|
}
|
||||||
if errors.Is(err, estream.ErrNoKey) {
|
if errors.Is(err, estream.ErrNoKey) {
|
||||||
if stream.Name == "inspect.zip" {
|
fmt.Println("Skipping", stream.Name, "no private key")
|
||||||
return errors.New("incorrect private key")
|
|
||||||
}
|
|
||||||
if err := stream.Skip(); err != nil {
|
if err := stream.Skip(); err != nil {
|
||||||
return fmt.Errorf("stream skip: %w", err)
|
return fmt.Errorf("stream skip: %w", err)
|
||||||
}
|
}
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if extracted {
|
|
||||||
return keepFileErr{fmt.Errorf("next stream: %w", err)}
|
|
||||||
}
|
|
||||||
return fmt.Errorf("next stream: %w", err)
|
return fmt.Errorf("next stream: %w", err)
|
||||||
}
|
}
|
||||||
if stream.Name == "inspect.zip" {
|
if strings.Contains(stream.Name, "..") || !utf8.ValidString(stream.Name) {
|
||||||
if extracted {
|
return fmt.Errorf("invalid stream name: %q", stream.Name)
|
||||||
return keepFileErr{errors.New("multiple inspect.zip streams found")}
|
|
||||||
}
|
}
|
||||||
_, err := io.Copy(w, stream)
|
|
||||||
|
dst := filepath.Join(extractDir, stream.Name)
|
||||||
|
os.Mkdir(extractDir, 0o755)
|
||||||
|
w, err := os.Create(dst)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("creating output file: %w", err)
|
||||||
|
}
|
||||||
|
_, err = io.Copy(w, stream)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("reading inspect stream: %w", err)
|
return fmt.Errorf("reading inspect stream: %w", err)
|
||||||
}
|
}
|
||||||
fmt.Println(okMsg)
|
fmt.Printf("Extracted: %s\n", dst)
|
||||||
extracted = true
|
extracted = true
|
||||||
continue
|
|
||||||
}
|
|
||||||
if err := stream.Skip(); err != nil {
|
|
||||||
return fmt.Errorf("stream skip: %w", err)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -51,15 +51,34 @@ func main() {
|
|||||||
generateKeys()
|
generateKeys()
|
||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
}
|
}
|
||||||
var privateKey []byte
|
var privateKeys [][]byte
|
||||||
if *keyHex == "" {
|
if *keyHex == "" {
|
||||||
if b, err := os.ReadFile(*privKeyPath); err == nil {
|
// Attempt to load private key(s)
|
||||||
privateKey = b
|
n := 1
|
||||||
fmt.Println("Using private key from", *privKeyPath)
|
var base, ext string
|
||||||
|
base = *privKeyPath
|
||||||
|
if idx := strings.LastIndexByte(base, '.'); idx != -1 {
|
||||||
|
ext = base[idx:]
|
||||||
|
base = base[:idx]
|
||||||
|
}
|
||||||
|
for {
|
||||||
|
// Automatically read "file.ext", "file-2.ext", "file-3.ext"...
|
||||||
|
fn := base + ext
|
||||||
|
if n > 1 {
|
||||||
|
fn = fmt.Sprintf("%s-%d%s", base, n, ext)
|
||||||
|
}
|
||||||
|
|
||||||
|
if b, err := os.ReadFile(fn); err == nil {
|
||||||
|
privateKeys = append(privateKeys, b)
|
||||||
|
fmt.Println("Added private key from", fn)
|
||||||
|
} else {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
n++
|
||||||
}
|
}
|
||||||
|
|
||||||
// Prompt for decryption key if no --key or --private-key are provided
|
// Prompt for decryption key if no --key or --private-key are provided
|
||||||
if len(privateKey) == 0 && !*stdin {
|
if len(privateKeys) == 0 && !*stdin {
|
||||||
reader := bufio.NewReader(os.Stdin)
|
reader := bufio.NewReader(os.Stdin)
|
||||||
fmt.Print("Enter Decryption Key: ")
|
fmt.Print("Enter Decryption Key: ")
|
||||||
|
|
||||||
@ -91,17 +110,20 @@ func main() {
|
|||||||
var err error
|
var err error
|
||||||
inputs, err = filepathx.Glob(flag.Args()[0])
|
inputs, err = filepathx.Glob(flag.Args()[0])
|
||||||
fatalErr(err)
|
fatalErr(err)
|
||||||
|
if len(inputs) == 0 {
|
||||||
|
fmt.Println("Usage: No input found")
|
||||||
|
}
|
||||||
default:
|
default:
|
||||||
flag.Usage()
|
flag.Usage()
|
||||||
fatalIf(true, "Only 1 file can be decrypted")
|
fatalIf(true, "Only 1 file can be decrypted")
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
for _, input := range inputs {
|
for _, input := range inputs {
|
||||||
processFile(input, privateKey)
|
processFile(input, privateKeys)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func processFile(inputFileName string, privateKey []byte) {
|
func processFile(inputFileName string, privateKeys [][]byte) {
|
||||||
// Calculate the output file name
|
// Calculate the output file name
|
||||||
var outputFileName string
|
var outputFileName string
|
||||||
switch {
|
switch {
|
||||||
@ -115,6 +137,14 @@ func processFile(inputFileName string, privateKey []byte) {
|
|||||||
outputFileName = inputFileName + ".decrypted"
|
outputFileName = inputFileName + ".decrypted"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Open the input and create the output file
|
||||||
|
input, err := os.Open(inputFileName)
|
||||||
|
fatalErr(err)
|
||||||
|
defer input.Close()
|
||||||
|
|
||||||
|
// Decrypt the inspect data
|
||||||
|
switch {
|
||||||
|
case *keyHex != "":
|
||||||
// Backup any already existing output file
|
// Backup any already existing output file
|
||||||
_, err := os.Stat(outputFileName)
|
_, err := os.Stat(outputFileName)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
@ -123,24 +153,15 @@ func processFile(inputFileName string, privateKey []byte) {
|
|||||||
fatalErr(err)
|
fatalErr(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Open the input and create the output file
|
|
||||||
input, err := os.Open(inputFileName)
|
|
||||||
fatalErr(err)
|
|
||||||
defer input.Close()
|
|
||||||
output, err := os.Create(outputFileName)
|
output, err := os.Create(outputFileName)
|
||||||
fatalErr(err)
|
fatalErr(err)
|
||||||
|
|
||||||
// Decrypt the inspect data
|
|
||||||
msg := fmt.Sprintf("output written to %s", outputFileName)
|
msg := fmt.Sprintf("output written to %s", outputFileName)
|
||||||
|
|
||||||
switch {
|
|
||||||
case *keyHex != "":
|
|
||||||
err = extractInspectV1(*keyHex, input, output, msg)
|
err = extractInspectV1(*keyHex, input, output, msg)
|
||||||
case len(privateKey) != 0:
|
|
||||||
err = extractInspectV2(privateKey, input, output, msg)
|
|
||||||
}
|
|
||||||
output.Close()
|
output.Close()
|
||||||
|
case len(privateKeys) != 0:
|
||||||
|
outputFileName := strings.TrimSuffix(outputFileName, ".zip")
|
||||||
|
err = extractInspectV2(privateKeys, input, outputFileName)
|
||||||
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
||||||
var keep keepFileErr
|
var keep keepFileErr
|
||||||
|
Loading…
x
Reference in New Issue
Block a user