This repository has been archived by the owner on Nov 19, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 392
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add create-evil-tar.go and create-evil-zip.go
- Loading branch information
Showing
2 changed files
with
145 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package main | ||
|
||
import ( | ||
"archive/tar" | ||
"log" | ||
"os" | ||
"time" | ||
) | ||
|
||
func main() { | ||
// Create a buffer to write our archive to. | ||
fw, err := os.Create("double-evil.tar") | ||
if nil != err { | ||
log.Fatal(err) | ||
return | ||
} | ||
|
||
// Create a new tar archive. | ||
w := tar.NewWriter(fw) | ||
|
||
// Write the evil symlink, it points outside of the target directory | ||
h := &tar.Header{ | ||
Name: "bad/file.txt", | ||
Typeflag: 2, | ||
Linkname: "../../badfile.txt", | ||
ModTime: time.Now(), | ||
} | ||
|
||
err = w.WriteHeader(h) | ||
|
||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
// Write safe files to the archive. | ||
var files = []struct { | ||
Name, Body string | ||
}{ | ||
{"goodfile.txt", "hello world"}, | ||
{"morefile.txt", "hello world"}, | ||
{"bad/file.txt", "Mwa-ha-ha"}, | ||
} | ||
for _, file := range files { | ||
h := &tar.Header{ | ||
Name: file.Name, | ||
Typeflag: 0, | ||
Size: int64(len(file.Body)), | ||
ModTime: time.Now(), | ||
} | ||
err := w.WriteHeader(h) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
_, err = w.Write([]byte(file.Body)) | ||
|
||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
} | ||
|
||
// Close the in-memory archive so that it writes trailing data | ||
err = w.Close() | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
// close the on-disk archive so that it flushes all bytes | ||
if err = fw.Close(); err != nil { | ||
log.Fatal(err) | ||
return | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package main | ||
|
||
import ( | ||
"archive/zip" | ||
"log" | ||
"os" | ||
"time" | ||
) | ||
|
||
func main() { | ||
// Create a buffer to write our archive to. | ||
fw, err := os.Create("double-evil.zip") | ||
if nil != err { | ||
log.Fatal(err) | ||
return | ||
} | ||
|
||
// Create a new zip archive. | ||
w := zip.NewWriter(fw) | ||
|
||
// Write the evil symlink | ||
h := &zip.FileHeader{ | ||
Name: "bad/file.txt", | ||
Method: zip.Deflate, | ||
Modified: time.Now(), | ||
} | ||
h.SetMode(os.ModeSymlink) | ||
header, err := w.CreateHeader(h) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
// The evil symlink points outside of the target directory | ||
_, err = header.Write([]byte("../../badfile.txt")) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
// Write safe files to the archive. | ||
var files = []struct { | ||
Name, Body string | ||
}{ | ||
{"goodfile.txt", "hello world"}, | ||
{"morefile.txt", "hello world"}, | ||
{"bad/file.txt", "Mwa-ha-ha"}, | ||
} | ||
for _, file := range files { | ||
h := &zip.FileHeader{ | ||
Name: file.Name, | ||
Method: zip.Deflate, | ||
Modified: time.Now(), | ||
} | ||
|
||
header, err := w.CreateHeader(h) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
_, err = header.Write([]byte(file.Body)) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
} | ||
|
||
// close the in-memory archive so that it writes trailing data | ||
if err = w.Close(); err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
// close the on-disk archive so that it flushes all bytes | ||
if err = fw.Close(); err != nil { | ||
log.Fatal(err) | ||
return | ||
} | ||
} |