diff --git a/api/files.go b/api/files.go index 2cebff2..a9ab3a2 100644 --- a/api/files.go +++ b/api/files.go @@ -27,17 +27,16 @@ func (client *Client) GetFiles(params *GetFilesParams, opts ...Option) ([]*File, } type PostFilesParams struct { - File *InputFile - CSRF string // XXX should not be part of the request - Challenge int // TODO May be additional i.e. pages don't need it + Files []*InputFile // XXX backend code shows it could be a list, but not the doc + Challenge int // TODO May be additional i.e. pages don't need it Location *string } func (client *Client) PostFiles(params *PostFilesParams, opts ...Option) ([]*File, error) { // Maps parameters to values mp := map[string]any{ - "file": params.File, - "nonce": params.CSRF, + "file": params.Files, + "nonce": client.nonce, "challenge": params.Challenge, "type": "challenge", } @@ -125,6 +124,20 @@ func encodeMultipart(values map[string]any) (io.Reader, string, error) { } content = file.Content } + } else if files, ok := v.([]*InputFile); ok { + if files == nil { + if fw, err = w.CreateFormFile(k, ""); err != nil { + return nil, "", err + } + content = []byte{} + } else { + for _, file := range files { + if fw, err = w.CreateFormFile(k, file.Name); err != nil { + return nil, "", err + } + content = file.Content + } + } } else { if fw, err = w.CreateFormField(k); err != nil { return nil, "", err diff --git a/api/setup_test.go b/api/setup_test.go index 9feb59b..7abbd5c 100644 --- a/api/setup_test.go +++ b/api/setup_test.go @@ -97,9 +97,11 @@ func Test_F_Setup(t *testing.T) { // 3. Push a file files, err := client.PostFiles(&api.PostFilesParams{ - File: &api.InputFile{ - Name: "icmp.pcap", - Content: []byte("bla bla bla CTFER{flag} bip boop"), + Files: []*api.InputFile{ + { + Name: "icmp.pcap", + Content: []byte("bla bla bla CTFER{flag} bip boop"), + }, }, Challenge: chall.ID, }) diff --git a/examples/setup/main.go b/examples/setup/main.go index 93de8f8..d5c23f3 100644 --- a/examples/setup/main.go +++ b/examples/setup/main.go @@ -76,6 +76,24 @@ func main() { } fmt.Printf(" Created challenge %d\n", ch.ID) + // Add files to it + files, err := client.PostFiles(&ctfd.PostFilesParams{ + Files: []*ctfd.InputFile{ + { + Name: "file1", + Content: []byte(`toto 1`), + }, { + Name: "file2", + Content: []byte(`toto 2`), + }, + }, + Challenge: ch.ID, + }) + if err != nil { + log.Fatalf("Creating files: %s", err) + } + fmt.Printf(" Created %d files\n", len(files)) + // Add a flag to solve it fmt.Println("[~] Updating challenge") f, err := client.PostFlags(&ctfd.PostFlagsParams{