Skip to content

Instantly share code, notes, and snippets.

@terrorbyte
Created December 10, 2015 19:36
Show Gist options
  • Save terrorbyte/fd34faf0c96ab60222d3 to your computer and use it in GitHub Desktop.
Save terrorbyte/fd34faf0c96ab60222d3 to your computer and use it in GitHub Desktop.
Dirty fileshare
package main
import (
"crypto/rand"
"crypto/sha256"
"encoding/hex"
"flag"
"fmt"
"github.com/terrorbyte/archivex"
"net"
"net/http"
"os"
"path"
)
//TODO
//-s [-c cert -p pub]: SSL
//-x val: Do not terminate, return val
//-a hash: Dynamic hash
var uid string
var file string
//[-n file] [-i interface] [-t <raw,zip,tar,gz,xz>] FILE01 [FILEN]
var name = flag.String("n", "", "File name to output")
//TODO
//var inter = flag.String("i", "", "Interface to listen on")
var ftype = flag.String("t", "raw", "File type to output as. Valid types are raw, zip, tar, tar.gz")
var port = flag.String("p", "11311", "Port to use")
func fileHandler(w http.ResponseWriter, r *http.Request) {
fileReq := r.URL.Path[len("/files/") : len("/files/")+len(uid)]
if fileReq == uid {
fmt.Println("[+] " + r.RemoteAddr + " downloading file")
if *name != "" {
w.Header().Set("Content-Disposition", "attachment; filename="+*name)
} else {
w.Header().Set("Content-Disposition", "attachment; filename="+path.Base(file))
}
w.Header().Set("Content-Type", r.Header.Get("Content-Type"))
http.ServeFile(w, r, file)
} else {
hijackClose(w, r)
os.Exit(7)
}
fmt.Println("[+] " + r.RemoteAddr + " downloaded file")
os.Exit(0)
}
func hijackClose(w http.ResponseWriter, r *http.Request) {
hj, ok := w.(http.Hijacker)
if !ok {
os.Exit(1)
}
conn, _, err := hj.Hijack()
if err != nil {
os.Exit(1)
}
fmt.Fprintf(os.Stderr, "[!] "+r.URL.Path+": "+r.RemoteAddr+" you must visit the provided link. Closing\n")
conn.Close()
os.Exit(1)
}
func randHash(b int) (string, error) {
randVal := make([]byte, b)
_, err := rand.Read(randVal)
hash := sha256.New()
hash.Write(randVal)
id := hash.Sum(nil)
return hex.EncodeToString(id), err
}
func getIP() ([]string, error) {
ifaces, err := net.InterfaceAddrs()
if err != nil {
return nil, err
}
var ipList []string
for _, iface := range ifaces {
if ipnet, err := iface.(*net.IPNet); err && !ipnet.IP.IsLoopback() {
if ipnet.IP.To4() != nil {
ipList = append(ipList, ipnet.IP.String())
}
}
}
return ipList, nil
}
func dynamicArchive(format string, out string, fileList ...string) error {
if format == "zip" {
zip := new(archivex.ZipFile)
zip.Create(out)
for _, f := range fileList {
fileInfo, err := os.Stat(f)
if err != nil {
fmt.Fprintf(os.Stderr, "[-] Could not find file")
os.Exit(3)
}
if fileInfo.IsDir() {
zip.AddAll(f, true)
} else {
zip.AddFile(f)
}
}
zip.Close()
} else if format == "tar" || format == "tar.gz" {
tar := new(archivex.TarFile)
if format == "tar.gz" {
tar.Compressed = true
}
tar.Create(out)
for _, f := range fileList {
fileInfo, err := os.Stat(f)
if err != nil {
fmt.Fprintf(os.Stderr, "[-] Could not find file")
os.Exit(3)
}
if fileInfo.IsDir() {
tar.AddAll(f, true)
} else {
tar.AddFile(f)
}
}
tar.Close()
}
return nil
}
func main() {
flag.Parse()
uid, _ = randHash(32)
if len(flag.Args()) < 1 {
fmt.Fprintf(os.Stderr, "[!] File required\n")
flag.Usage()
os.Exit(2)
} else if len(flag.Args()) > 1 || *ftype != "raw" {
dynamicArchive(*ftype, "/tmp/"+uid+"."+*ftype, flag.Args()...)
file = "/tmp/" + uid + "." + *ftype
} else {
fileInfo, err := os.Stat(flag.Args()[0])
if err != nil {
fmt.Fprintf(os.Stderr, "[!] File does not exist\n")
os.Exit(4)
}
if fileInfo.IsDir() {
fmt.Println("[+] Compressing directory")
dynamicArchive(*ftype, "/tmp/"+flag.Args()[0]+"."+*ftype, flag.Args()[0])
file = "/tmp/" + flag.Args()[0] + "." + *ftype
} else {
file = flag.Args()[0]
}
}
fmt.Printf("[+] Files: %v\n", flag.Args())
ip, err := getIP()
//TODO err and check for multiple ip's
//fmt.Println("[?] " + file)
fmt.Println("[+] Visit: http://" + ip[0] + ":11311/files/" + uid + "/")
http.HandleFunc("/files/", fileHandler)
http.HandleFunc("/", hijackClose)
err = http.ListenAndServe(ip[0]+":"+*port, nil)
if err != nil {
fmt.Fprintf(os.Stderr, "[!] Someone is already sharing or port is used... Please wait\n")
os.Exit(6)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment