Skip to content

Commit eb5dbbc

Browse files
authored
feat: add support for retrieving private files and URLs. (#115)
https://b.corp.google.com/issues/36292914
1 parent 2aac1eb commit eb5dbbc

File tree

12 files changed

+101
-30
lines changed

12 files changed

+101
-30
lines changed

cmd/cmd.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,12 @@ func addOutputFlag(flags *pflag.FlagSet) {
9898
"directory where downloaded files are put")
9999
}
100100

101+
func addPrivateFlag(flags *pflag.FlagSet) {
102+
flags.BoolP(
103+
"private", "P", false,
104+
"include private ones (requires private scanning privileges)")
105+
}
106+
101107
func addFilterFlag(flags *pflag.FlagSet) {
102108
flags.StringP(
103109
"filter", "f", "",

cmd/collection.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ func NewCollectionCmd() *cobra.Command {
6060
cmd.AddCommand(NewCollectionDeleteCmd())
6161
cmd.AddCommand(NewCollectionRemoveItemsCmd())
6262

63-
addRelationshipCmds(cmd, "collections", "collection", "[collection]")
63+
addRelationshipCmds(cmd, "collections", "collection", "[collection]", false)
6464
addThreadsFlag(cmd.Flags())
6565
addIncludeExcludeFlags(cmd.Flags())
6666
addIDOnlyFlag(cmd.Flags())

cmd/domain.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func NewDomainCmd() *cobra.Command {
5454
},
5555
}
5656

57-
addRelationshipCmds(cmd, "domains", "domain", "[domain]")
57+
addRelationshipCmds(cmd, "domains", "domain", "[domain]", false)
5858

5959
addThreadsFlag(cmd.Flags())
6060
addIncludeExcludeFlags(cmd.Flags())

cmd/file.go

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,11 @@
1414
package cmd
1515

1616
import (
17-
"github.com/VirusTotal/vt-cli/utils"
1817
"regexp"
1918

19+
"github.com/VirusTotal/vt-cli/utils"
2020
"github.com/spf13/cobra"
21+
"github.com/spf13/viper"
2122
)
2223

2324
var fileCmdHelp = `Get information about one or more files.
@@ -50,18 +51,26 @@ func NewFileCmd() *cobra.Command {
5051
if err != nil {
5152
return err
5253
}
53-
return p.GetAndPrintObjects(
54-
"files/%s",
55-
utils.StringReaderFromCmdArgs(args),
56-
re)
54+
if viper.GetBool("private") {
55+
return p.GetAndPrintObjectsWithFallback(
56+
[]string{"files/%s", "private/files/%s"},
57+
utils.StringReaderFromCmdArgs(args),
58+
re)
59+
} else {
60+
return p.GetAndPrintObjects(
61+
"files/%s",
62+
utils.StringReaderFromCmdArgs(args),
63+
re)
64+
}
5765
},
5866
}
5967

60-
addRelationshipCmds(cmd, "files", "file", "[hash]")
68+
addRelationshipCmds(cmd, "files", "file", "[hash]", true)
6169

6270
addThreadsFlag(cmd.Flags())
6371
addIncludeExcludeFlags(cmd.Flags())
6472
addIDOnlyFlag(cmd.Flags())
73+
addPrivateFlag(cmd.Flags())
6574

6675
return cmd
6776
}

cmd/ip.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@
1414
package cmd
1515

1616
import (
17-
"github.com/VirusTotal/vt-cli/utils"
1817
"regexp"
1918

19+
"github.com/VirusTotal/vt-cli/utils"
20+
2021
"github.com/spf13/cobra"
2122
)
2223

@@ -55,7 +56,7 @@ func NewIPCmd() *cobra.Command {
5556
},
5657
}
5758

58-
addRelationshipCmds(cmd, "ip_addresses", "ip_address", "[ip]")
59+
addRelationshipCmds(cmd, "ip_addresses", "ip_address", "[ip]", false)
5960

6061
addThreadsFlag(cmd.Flags())
6162
addIncludeExcludeFlags(cmd.Flags())

cmd/monitor.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,7 @@ func NewMonitorCmd() *cobra.Command {
474474
cmd.AddCommand(NewMonitorItemsSetDetailsCmd())
475475
cmd.AddCommand(NewMonitorItemsDeleteDetailsCmd())
476476

477-
addRelationshipCmds(cmd, "monitor/items", "monitor_item", "[monitor_id]")
477+
addRelationshipCmds(cmd, "monitor/items", "monitor_item", "[monitor_id]", false)
478478

479479
return cmd
480480
}

cmd/monitorpartner.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ func NewMonitorPartnerCmd() *cobra.Command {
165165
cmd.AddCommand(NewMonitorPartnerHashesListCmd())
166166
cmd.AddCommand(NewMonitorPartnerHashDownloadCmd())
167167

168-
addRelationshipCmds(cmd, "monitor_partner/hashes", "monitor_hash", "[sha256]")
168+
addRelationshipCmds(cmd, "monitor_partner/hashes", "monitor_hash", "[sha256]", false)
169169

170170
return cmd
171171
}

cmd/relationship.go

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,12 @@ import (
1717
"encoding/base64"
1818
"encoding/gob"
1919
"fmt"
20-
"github.com/VirusTotal/vt-cli/utils"
2120
"os"
2221
"path"
2322
"sync"
2423

24+
"github.com/VirusTotal/vt-cli/utils"
25+
2526
vt "github.com/VirusTotal/vt-go"
2627
homedir "github.com/mitchellh/go-homedir"
2728
"github.com/spf13/cobra"
@@ -69,7 +70,7 @@ func getRelatedObjects(collection, objectID, relationship string, limit int) ([]
6970
}
7071

7172
// NewRelationshipCmd returns a new instance of the 'relationship' command.
72-
func NewRelationshipCmd(collection, relationship, use, description string) *cobra.Command {
73+
func NewRelationshipCmd(collection, relationship, use, description string, private_flag bool) *cobra.Command {
7374
cmd := &cobra.Command{
7475
Args: cobra.ExactArgs(1),
7576
Use: fmt.Sprintf("%s %s", relationship, use),
@@ -85,6 +86,9 @@ func NewRelationshipCmd(collection, relationship, use, description string) *cobr
8586
if err != nil {
8687
return err
8788
}
89+
if viper.GetBool("private") {
90+
collection = "private/" + collection
91+
}
8892
url := vt.URL("%s/%s/%s", collection, objectID, relationship)
8993
return p.PrintCollection(url)
9094
},
@@ -95,18 +99,26 @@ func NewRelationshipCmd(collection, relationship, use, description string) *cobr
9599
addLimitFlag(cmd.Flags())
96100
addCursorFlag(cmd.Flags())
97101

102+
if private_flag {
103+
addPrivateFlag(cmd.Flags())
104+
}
105+
98106
return cmd
99107
}
100108

101109
// NewRelationshipsCmd returns a new instance of the 'relationships' command.
102-
func NewRelationshipsCmd(collection, objectType, use string) *cobra.Command {
110+
func NewRelationshipsCmd(collection, objectType, use string, private_flag bool) *cobra.Command {
103111
cmd := &cobra.Command{
104112
Use: fmt.Sprintf("relationships %s", use),
105113
Short: "Get all relationships.",
106114
Args: cobra.ExactArgs(1),
107115
RunE: func(cmd *cobra.Command, args []string) error {
108116
var wg sync.WaitGroup
109117
var sm sync.Map
118+
if viper.GetBool("private") {
119+
objectType = "private_" + objectType
120+
collection = "private/" + collection
121+
}
110122
for _, r := range objectRelationshipsMap[objectType] {
111123
wg.Add(1)
112124
go func(relationshipName string) {
@@ -148,13 +160,17 @@ func NewRelationshipsCmd(collection, objectType, use string) *cobra.Command {
148160
addIncludeExcludeFlags(cmd.Flags())
149161
addLimitFlag(cmd.Flags())
150162

163+
if private_flag {
164+
addPrivateFlag(cmd.Flags())
165+
}
166+
151167
return cmd
152168
}
153169

154-
func addRelationshipCmds(cmd *cobra.Command, collection, objectType, use string) {
170+
func addRelationshipCmds(cmd *cobra.Command, collection, objectType, use string, private_flag bool) {
155171
relationships := objectRelationshipsMap[objectType]
156172
for _, r := range relationships {
157-
cmd.AddCommand(NewRelationshipCmd(collection, r.Name, use, r.Description))
173+
cmd.AddCommand(NewRelationshipCmd(collection, r.Name, use, r.Description, private_flag))
158174
}
159-
cmd.AddCommand(NewRelationshipsCmd(collection, objectType, use))
175+
cmd.AddCommand(NewRelationshipsCmd(collection, objectType, use, private_flag))
160176
}

cmd/threat_profile.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func NewThreatProfileCmd() *cobra.Command {
4343
},
4444
}
4545

46-
addRelationshipCmds(cmd, "threat_profiles", "threat_profile", "[id]")
46+
addRelationshipCmds(cmd, "threat_profiles", "threat_profile", "[id]", false)
4747
addThreadsFlag(cmd.Flags())
4848
addIncludeExcludeFlags(cmd.Flags())
4949
addIDOnlyFlag(cmd.Flags())

cmd/url.go

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@ package cmd
1515

1616
import (
1717
"encoding/base64"
18+
"regexp"
19+
1820
"github.com/VirusTotal/vt-cli/utils"
1921
"github.com/spf13/cobra"
20-
"regexp"
22+
"github.com/spf13/viper"
2123
)
2224

2325
var urlCmdHelp = `Get information about one or more URLs.
@@ -35,7 +37,6 @@ var urlCmdExample = ` vt url https://www.virustotal.com
3537
vt url f1177df4692356280844e1d5af67cc4a9eccecf77aa61c229d483b7082c70a8e
3638
cat list_of_urls | vt url -`
3739

38-
3940
// Regular expressions used for validating a URL identifier.
4041
var urlID = regexp.MustCompile(`[0-9a-fA-F]{64}`)
4142

@@ -55,7 +56,7 @@ func NewURLCmd() *cobra.Command {
5556
}
5657
r := utils.NewMappedStringReader(
5758
utils.StringReaderFromCmdArgs(args),
58-
func (url string) string {
59+
func(url string) string {
5960
if urlID.MatchString(url) {
6061
// The user provided a URL identifier as returned by
6162
// VirusTotal's API, which consists in the URL's SHA-256.
@@ -66,15 +67,27 @@ func NewURLCmd() *cobra.Command {
6667
// encoded as base64 before being used.
6768
return base64.RawURLEncoding.EncodeToString([]byte(url))
6869
})
69-
return p.GetAndPrintObjects("urls/%s", r, nil)
70+
71+
if viper.GetBool("private") {
72+
return p.GetAndPrintObjectsWithFallback(
73+
[]string{"urls/%s", "private/urls/%s"},
74+
r,
75+
nil)
76+
} else {
77+
return p.GetAndPrintObjects(
78+
"urls/%s",
79+
r,
80+
nil)
81+
}
7082
},
7183
}
7284

73-
addRelationshipCmds(cmd, "urls", "url", "[url]")
85+
addRelationshipCmds(cmd, "urls", "url", "[url]", true)
7486

7587
addThreadsFlag(cmd.Flags())
7688
addIncludeExcludeFlags(cmd.Flags())
7789
addIDOnlyFlag(cmd.Flags())
90+
addPrivateFlag(cmd.Flags())
7891

7992
return cmd
8093
}

0 commit comments

Comments
 (0)