Skip to content

Commit 8451c47

Browse files
committed
feat(log):log files can be saved by default
1 parent d2b6c97 commit 8451c47

11 files changed

Lines changed: 81 additions & 7 deletions

File tree

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
.DS_Store
22
.vscode/
33
go.sum
4-
temp/
4+
temp/
5+
logs/

pkg/providers/alibaba/alibaba.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"log"
77
"strings"
8+
"time"
89

910
_dns "github.com/404tk/cloudtoolkit/pkg/providers/alibaba/dns"
1011
_ecs "github.com/404tk/cloudtoolkit/pkg/providers/alibaba/ecs"
@@ -220,4 +221,10 @@ func (p *Provider) EventDump(sourceIp string) {
220221
events = append(events, _event)
221222
}
222223
table.Output(events)
224+
if utils.DoSave {
225+
filename := time.Now().Format("20060102150405.log")
226+
path := fmt.Sprintf("%s/%s_eventdump_%s", utils.LogDir, p.Name(), filename)
227+
table.FileOutput(path, events)
228+
log.Printf("[+] Output written to [%s]\n", path)
229+
}
223230
}

runner/console/completer.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ var optionsDesc = map[string]string{
4444
utils.AzureSubscriptionId: "Subscription ID (Optional)",
4545
utils.GCPserviceAccountJSON: "GCP Credential encoded through Base64",
4646
utils.Metadata: "Set the payload with additional arguments (Optional)",
47+
utils.Save: "Save log file",
4748
}
4849

4950
var opt = []prompt.Suggest{

runner/console/executor.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ func set(args []string) {
9898
fmt.Printf("%s => %s\n", args[0], args[1])
9999
}
100100
}
101-
if args[0] == "payload" {
101+
if args[0] == utils.Payload {
102102
switch args[1] {
103103
case "backdoor-user":
104104
config[utils.Metadata] = utils.BackdoorUser
@@ -108,6 +108,14 @@ func set(args []string) {
108108
config[utils.Metadata] = utils.EventDump
109109
}
110110
}
111+
if args[0] == utils.Save {
112+
if args[1] == "true" {
113+
utils.DoSave = true
114+
utils.CheckLogDir()
115+
} else {
116+
utils.DoSave = false
117+
}
118+
}
111119
}
112120

113121
func run(ctx context.Context) {

runner/console/sessions.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,16 @@ func internation(uuid string) {
7979
}
8080
if provider, ok := m[utils.Provider]; ok {
8181
config = m
82+
if v, ok := config["save"]; ok {
83+
if v == "true" {
84+
utils.DoSave = true
85+
utils.CheckLogDir()
86+
} else {
87+
utils.DoSave = false
88+
}
89+
} else {
90+
set([]string{"save", "true"})
91+
}
8292
p := prompt.New(
8393
Executor,
8494
actionCompleter,

runner/console/use.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ func Use(args []string) {
1616
for _, m := range modules {
1717
if m.Text == args[0] {
1818
loadModule(args[0])
19+
if config["save"] == "true" {
20+
utils.DoSave = true
21+
utils.CheckLogDir()
22+
} else {
23+
utils.DoSave = false
24+
}
1925
return
2026
}
2127
}
@@ -34,6 +40,7 @@ func loadModule(m string) {
3440

3541
config[utils.Provider] = m
3642
config[utils.Payload] = "cloudlist" // Default use cloudlist
43+
config[utils.Save] = "true" // Default save log file
3744

3845
p := prompt.New(
3946
Executor,

runner/payloads/cloudlist.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@ import (
44
"context"
55
"fmt"
66
"log"
7+
"time"
78

89
"github.com/404tk/cloudtoolkit/pkg/inventory"
10+
"github.com/404tk/cloudtoolkit/utils"
911
"github.com/404tk/cloudtoolkit/utils/table"
1012
)
1113

@@ -27,10 +29,15 @@ func (p CloudList) Run(ctx context.Context, config map[string]string) {
2729
case <-ctx.Done():
2830
return
2931
default:
32+
filename := time.Now().Format("20060102150405.log")
33+
path := fmt.Sprintf("%s/%s_cloudlist_%s", utils.LogDir, i.Providers.Name(), filename)
3034
pprint := func(len int, tag string, res interface{}) {
3135
if len > 0 {
3236
fmt.Println(tag, "results:")
3337
table.Output(res)
38+
if utils.DoSave {
39+
table.FileOutput(path, res)
40+
}
3441
}
3542
}
3643

@@ -46,8 +53,11 @@ func (p CloudList) Run(ctx context.Context, config map[string]string) {
4653
if resources.Sms.DailySize > 0 {
4754
fmt.Printf("[*] The total number of SMS messages sent today is %v.\n", resources.Sms.DailySize)
4855
}
49-
50-
log.Println("[+] Done.")
56+
if utils.DoSave {
57+
log.Printf("[+] Output written to [%s]\n", path)
58+
} else {
59+
log.Println("[+] Done.")
60+
}
5161
}
5262
}
5363

runner/payloads/event_dump.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func (p EventDump) Run(ctx context.Context, config map[string]string) {
2525
}
2626
}
2727
i.Providers.EventDump(sourceIp)
28-
log.Println("[+] Done.")
28+
// log.Println("[+] Done.")
2929
}
3030

3131
func (p EventDump) Desc() string {

utils/const.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const (
88
SecurityToken = "token"
99
Region = "region"
1010
Version = "version"
11+
Save = "save"
1112
)
1213

1314
const (
@@ -28,6 +29,8 @@ const (
2829
EventDump = "dump all"
2930
)
3031

31-
const (
32+
var (
33+
DoSave bool
34+
LogDir = "logs"
3235
IpInfo = "https://ipinfo.io/ip"
3336
)

utils/table/table.go

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import (
1515
func Output(slice interface{}) {
1616
coln, rows, err := parse(slice)
1717
if err != nil {
18-
log.Println("[-] ", err)
18+
log.Println("[-]", err)
1919
}
2020
table := tablewriter.NewWriter(os.Stdout)
2121
table.SetHeader(coln)
@@ -26,6 +26,25 @@ func Output(slice interface{}) {
2626
table.Render()
2727
}
2828

29+
func FileOutput(filename string, slice interface{}) {
30+
coln, rows, err := parse(slice)
31+
if err != nil {
32+
log.Println("[-]", err)
33+
}
34+
file, err := os.OpenFile(filename, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0644)
35+
if err != nil {
36+
log.Println("[-]", err)
37+
}
38+
defer file.Close()
39+
table := tablewriter.NewWriter(file)
40+
table.SetHeader(coln)
41+
42+
for _, v := range rows {
43+
table.Append(v)
44+
}
45+
table.Render()
46+
}
47+
2948
func parse(slice interface{}) (
3049
coln []string, // name of columns
3150
rows [][]string, // rows of content

0 commit comments

Comments
 (0)