Skip to content

Commit

Permalink
feat(files): add support of multi-post
Browse files Browse the repository at this point in the history
  • Loading branch information
pandatix committed Feb 27, 2024
1 parent 0ff97fa commit b486e30
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 8 deletions.
23 changes: 18 additions & 5 deletions api/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
}
Expand Down Expand Up @@ -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
Expand Down
8 changes: 5 additions & 3 deletions api/setup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
})
Expand Down
18 changes: 18 additions & 0 deletions examples/setup/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down

0 comments on commit b486e30

Please sign in to comment.