forked from 5haman/maltego-btc
-
Notifications
You must be signed in to change notification settings - Fork 1
/
transform.go
287 lines (257 loc) · 6.73 KB
/
transform.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
package main
import (
"fmt"
"log"
"strconv"
"github.com/glennzw/maltegogo"
)
type Transform struct {
Type string
Direction string
Value string
Id string
Balance string
Url string
LinkColor string
Weight int
LinkLabel string
IconURL string
Time uint64
}
type TransformList struct {
Id string
EntityList []Transform
}
var (
baseWeight = 10.0
)
func GetTransform(query string, Type string) (list TransformList) {
list.Id = query
log.Println("start transform: [", query, Type, "]")
switch Type {
case "WalletInfo", "WalletInOut", "WalletIn", "WalletOut":
WalletTransform(query, &list)
case "Wallet2Addresses":
Wallet2AddressesTransform(query, &list)
case "Address2Wallet":
Address2WalletTransform(query, &list)
default:
log.Println("error:", "unknown transform type: "+Type)
return
}
log.Println("finish transform: [", query, Type, "]")
return
}
func FilterTransform(query string, Type string, list *TransformList) {
list2 := []Transform{}
for _, ent := range list.EntityList {
switch Type {
case "WalletInfo":
if ent.Type == "btc.BtcWallet" && ent.Direction != "in" && ent.Direction != "out" {
list2 = append(list2, ent)
}
case "WalletInOut":
if ent.Type == "btc.BtcWallet" {
list2 = append(list2, ent)
}
case "WalletIn":
if ent.Type == "btc.BtcWallet" && ent.Direction == "in" {
list2 = append(list2, ent)
}
case "WalletOut":
if ent.Type == "btc.BtcWallet" && ent.Direction == "out" {
list2 = append(list2, ent)
}
case "Wallet2Addresses":
if ent.Type == "maltego.BTCAddress" {
list2 = append(list2, ent)
}
case "Address2Wallet":
if ent.Type == "btc.BtcWallet" {
list2 = append(list2, ent)
}
}
}
list.EntityList = list2
return
}
func GetWeight(amount float64) (weight int) {
weight = int(amount * baseWeight)
if weight < 1 {
weight = 1
}
return
}
func WalletTransform(query string, list *TransformList) {
c := map[string]int{}
m := map[string]Transform{}
wallet := GetWallet(query)
myLabel := wallet.WalletId
myIcon := config.IconWallet
myBalance := 0.0
if len(wallet.WalletTxs) > 0 {
myBalance = wallet.WalletTxs[0].Balance
}
if wallet.Label != "" {
myLabel = wallet.Label
myIcon = config.IconService
}
m[wallet.WalletId] = Transform{
Type: "btc.BtcWallet",
Value: myLabel,
Id: wallet.WalletId,
Url: WalletURL + wallet.WalletId,
Balance: strconv.FormatFloat(myBalance, 'f', -1, 64),
IconURL: myIcon,
}
c[wallet.WalletId] = 1
for _, tx := range wallet.WalletTxs {
if tx.Type == "sent" {
// Add outgoing links to other wallets
for _, out := range tx.WalletTxOutputs {
if out.WalletId != query && c[out.WalletId] == 0 {
count := 0
amount := 0.0
for _, tx2 := range wallet.WalletTxs {
for _, out2 := range tx2.WalletTxOutputs {
if out2.WalletId == out.WalletId {
count++
amount += out2.Amount
}
}
}
linkLabel := strconv.FormatFloat(amount, 'f', -1, 64) + " BTC. Txs: " + strconv.Itoa(count)
Label := out.WalletId
Icon := config.IconWallet
if out.Label != "" {
Label = out.Label
Icon = config.IconService
}
m[out.WalletId] = Transform{
Type: "btc.BtcWallet",
Direction: "out",
Value: Label,
Id: out.WalletId,
Url: WalletURL + out.WalletId,
LinkColor: config.LinkWalletColor,
Weight: GetWeight(amount),
LinkLabel: linkLabel,
IconURL: Icon,
Time: tx.Time,
}
c[out.WalletId] = 1
}
}
} else {
// Add incoming links from other wallets
if c[tx.WalletId] == 0 {
count := 0
amount := 0.0
for _, tx2 := range wallet.WalletTxs {
if tx2.WalletId == tx.WalletId {
count++
amount += tx2.Amount
}
}
Label := tx.WalletId
Icon := config.IconWallet
if tx.Label != "" {
Label = tx.Label
Icon = config.IconService
}
linkLabel := strconv.FormatFloat(amount, 'f', -1, 64) + " BTC. Txs: " + strconv.Itoa(count)
m[tx.WalletId] = Transform{
Type: "btc.BtcWallet",
Direction: "in",
Value: Label,
Id: tx.WalletId,
Url: WalletURL + tx.WalletId,
LinkColor: config.LinkWalletColor,
Weight: GetWeight(amount),
LinkLabel: linkLabel,
IconURL: Icon,
Time: tx.Time,
}
c[tx.WalletId] = 1
}
}
}
for _, NewEnt := range m {
list.EntityList = append(list.EntityList, NewEnt)
}
}
func Wallet2AddressesTransform(query string, list *TransformList) {
c := map[string]int{}
m := map[string]Transform{}
addresses := GetWalletAddresses(query)
for _, addr := range addresses.Addresses {
m[addr.Address] = Transform{
Type: "maltego.BTCAddress",
Direction: "in",
Value: addr.Address,
Id: addr.Address,
Balance: strconv.FormatFloat(addr.Balance, 'f', -1, 64),
LinkColor: config.LinkAddressColor,
LinkLabel: strconv.FormatFloat(addr.Balance, 'f', -1, 64) + " BTC",
Weight: GetWeight(addr.Balance),
}
c[addr.Address]++
}
// add address inputs/outputs
for _, NewEnt := range m {
list.EntityList = append(list.EntityList, NewEnt)
}
}
func Address2WalletTransform(query string, list *TransformList) {
c := map[string]int{}
m := map[string]Transform{}
wallet := GetAddressWallet(query)
// Add incoming links from other wallets
if c[wallet.WalletId] == 0 {
Label := wallet.WalletId
Icon := config.IconWallet
if wallet.Label != "" {
Label = wallet.Label
Icon = config.IconService
}
m[wallet.WalletId] = Transform{
Type: "btc.BtcWallet",
Direction: "out",
Value: Label,
Url: WalletURL + wallet.WalletId,
Id: wallet.WalletId,
LinkColor: config.LinkAddressColor,
IconURL: Icon,
}
c[wallet.WalletId] = 1
}
// add address inputs/outputs
for _, NewEnt := range m {
list.EntityList = append(list.EntityList, NewEnt)
}
}
func PrintTransform(list *TransformList) {
tr := &maltegogo.MaltegoTransform{}
// generate transform result
for _, ent := range list.EntityList {
NewEnt := tr.AddEntity(ent.Type, ent.Value)
NewEnt.SetType(ent.Type)
NewEnt.AddProperty(ent.Type, ent.Type, "stict", ent.Value)
NewEnt.AddProperty("id", "id", "stict", ent.Id)
NewEnt.AddProperty("balance", "balance", "stict", ent.Balance)
NewEnt.AddProperty("url", "url", "stict", ent.Url)
if ent.Weight != 0 {
NewEnt.SetWeight(ent.Weight)
}
if ent.LinkColor != "" {
NewEnt.SetLinkColor(ent.LinkColor)
}
NewEnt.SetLinkLabel(ent.LinkLabel)
NewEnt.SetIconURL(ent.IconURL)
if ent.Direction == "in" {
NewEnt.AddProperty("link#maltego.link.direction", "link#maltego.link.direction", "stict", "output-to-input")
}
}
// print transform result
fmt.Println(tr.ReturnOutput())
}