-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupload.go
99 lines (83 loc) · 2.39 KB
/
upload.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package main
import (
"bytes"
"io"
"mime/multipart"
"net/http"
"net/url"
"os"
"path/filepath"
"github.com/pkg/sftp"
"golang.org/x/crypto/ssh"
)
// UploadSFTP performs an SFTP upload using the provided configuration.
func UploadSFTP(cfg Configuration) error {
// Establish an SSH connection to the SFTP server
conn, err := ssh.Dial("tcp", cfg.SftpAddr, &ssh.ClientConfig{
User: cfg.SftpUser,
Auth: []ssh.AuthMethod{ssh.Password(cfg.SftpPass)},
HostKeyCallback: ssh.InsecureIgnoreHostKey(), // Ignore host key verification (insecure)
})
if err != nil {
return err
}
// Create an SFTP client using the SSH connection
client, err := sftp.NewClient(conn)
if err != nil {
return err
}
// Open the target file on the SFTP server for writing
dst, err := client.OpenFile(filepath.Join(cfg.SftpDir, cfg.SftpFile), os.O_WRONLY|os.O_TRUNC|os.O_CREATE)
if err != nil {
return err
}
// Open the local file for reading
src, err := os.Open(filepath.Join(cfg.OutputDir, cfg.OutputFile))
if err != nil {
return err
}
defer src.Close()
// Copy the content of the local file to the SFTP server
_, err = io.Copy(dst, src)
return err
}
// UploadDagobert performs an upload to a Dagobert instance using the provided configuration.
func UploadDagobert(cfg Configuration) error {
body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
// don't fail if hostname can't be resolved, just use empty string instead
hostname, _ := os.Hostname()
// Set multipart fields
writer.WriteField("Type", "Triage")
writer.WriteField("Name", cfg.DagobertFile)
writer.WriteField("Source", hostname)
part, _ := writer.CreateFormFile("File", cfg.DagobertFile)
// Open the local file for reading
src, err := os.Open(filepath.Join(cfg.OutputDir, cfg.OutputFile))
if err != nil {
return err
}
defer src.Close()
// Copy the content of the local file to the multipart message
_, err = io.Copy(part, src)
if err != nil {
return err
}
// Finish the multipart message
err = writer.Close()
if err != nil {
return err
}
dst, err := url.JoinPath(cfg.DagobertAddr, "/cases/", cfg.DagobertCase, "/evidences/new")
if err != nil {
return err
}
req, err := http.NewRequest("POST", dst, body)
if err != nil {
return err
}
req.Header.Set("X-API-Key", cfg.DagobertKey)
req.Header.Set("Content-Type", writer.FormDataContentType())
_, err = http.DefaultClient.Do(req)
return err
}