-
Notifications
You must be signed in to change notification settings - Fork 43
/
codec_grpc_methods.go
998 lines (932 loc) · 36.7 KB
/
codec_grpc_methods.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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
package codegrpc
import (
"bytes"
"context"
"crypto/md5"
"crypto/sha1"
"crypto/sha256"
"crypto/sha512"
_ "embed"
"encoding/gob"
"encoding/json"
"hash"
"regexp"
"strings"
"github.com/BurntSushi/toml"
"github.com/dlclark/regexp2"
"github.com/tidwall/gjson"
"github.com/yaklang/yaklang/common/authhack"
"github.com/yaklang/yaklang/common/consts"
"github.com/yaklang/yaklang/common/go-funk"
"github.com/yaklang/yaklang/common/log"
"github.com/yaklang/yaklang/common/mutate"
"github.com/yaklang/yaklang/common/utils"
"github.com/yaklang/yaklang/common/utils/lowhttp"
"github.com/yaklang/yaklang/common/utils/tlsutils"
"github.com/yaklang/yaklang/common/yak"
"github.com/yaklang/yaklang/common/yak/yakdoc"
"github.com/yaklang/yaklang/common/yak/yaklib/codec"
"github.com/yaklang/yaklang/common/yakgrpc/yakit"
"github.com/yaklang/yaklang/common/yakgrpc/ypb"
"github.com/yaklang/yaklang/common/yserx"
)
//go:embed codec.gob.gzip
var codecDoc []byte
var (
CodecLibs *yakdoc.ScriptLib
CodecLibsDoc []*ypb.CodecMethod // 记录函数的数据,参数类型等,用于前端生成样式
)
type outputType = string
var (
OUTPUT_RAW = "raw"
OUTPUT_HEX = "hex"
OUTPUT_BASE64 = "base64"
)
func init() {
buf, err := utils.GzipDeCompress(codecDoc)
if err != nil {
log.Warnf("load embed yak document error: %v", err)
}
var CodecDocumentHelper *yakdoc.DocumentHelper
decoder := gob.NewDecoder(bytes.NewReader(buf))
if err := decoder.Decode(&CodecDocumentHelper); err != nil {
log.Warnf("load embed yak document error: %v", err)
}
CodecLibs = CodecDocumentHelper.StructMethods["github.com/yaklang/yaklang/common/yak/yaklib/codec/codegrpc.CodecExecFlow"]
for funcName, funcInfo := range CodecLibs.Functions {
var CodecMethod ypb.CodecMethod
_, err = toml.Decode(funcInfo.Document, &CodecMethod)
if err != nil {
continue
}
CodecMethod.CodecMethod = funcName
CodecLibsDoc = append(CodecLibsDoc, &CodecMethod)
}
}
type CodecExecFlow struct {
Text []byte
Flow []*ypb.CodecWork
}
func NewCodecExecFlow(text []byte, flow []*ypb.CodecWork) *CodecExecFlow {
return &CodecExecFlow{
Text: text,
Flow: flow,
}
}
func decodeHexKeyAndIV(k string, i string) ([]byte, []byte, error) {
key, err := codec.DecodeHex(k)
if err != nil {
return nil, nil, err
}
if funk.IsEmpty(key) {
key = nil
}
iv, err := codec.DecodeHex(i)
if err != nil {
return nil, nil, err
}
if funk.IsEmpty(iv) {
iv = nil
}
return key, iv, nil
}
func encodeData(text []byte, output outputType) []byte {
switch output {
case OUTPUT_RAW:
return text
case OUTPUT_HEX:
return []byte(codec.EncodeToHex(text))
case OUTPUT_BASE64:
return []byte(codec.EncodeBase64(text))
default:
return text
}
}
func decodeData(text []byte, input outputType) []byte {
var data []byte
var err error
switch input {
case OUTPUT_RAW:
return text
case OUTPUT_HEX:
data, err = codec.DecodeHex(string(text))
if err != nil {
return text
}
case OUTPUT_BASE64:
data, err = codec.DecodeBase64(string(text))
if err != nil {
return text
}
default:
return text
}
if funk.IsEmpty(data) {
return nil
}
return data
}
// Tag = "加密"
// CodecName = "AES对称加密"
// Desc ="""高级加密标准(AES)是美国联邦信息处理标准(FIPS)。它是在一个历时5年的过程中,从15个竞争设计中选出的。
// Key:根据密钥的大小,将使用以下算法:
// 16字节 = AES-128
// 24字节 = AES-192
// 32字节 = AES-256
// 你可以使用其中一个KDF操作生成基于密码的密钥。"""
// Params = [
// { Name = "key", Type = "inputSelect", Required = true,Label = "Key", Connector ={ Name = "keyType", Type = "select", DefaultValue = "hex", Options = ["hex", "raw", "base64"], Required = true ,Label = "key格式"} },
// { Name = "IV", Type = "inputSelect", Required = false ,Label = "IV", Connector ={ Name = "ivType", Type = "select", DefaultValue = "hex", Options = ["hex", "raw", "base64"], Required = true ,Label = "IV格式"} },
// { Name = "mode", Type = "select", DefaultValue = "CBC",Options = ["CBC", "ECB", "GCM"], Required = true, Label = "Mode"},
// { Name = "output", Type = "select", DefaultValue = "hex", Options = ["hex", "raw", "base64"], Required = true ,Label = "输出格式"}
// ]
func (flow *CodecExecFlow) AESEncrypt(key string, keyType string, IV string, ivType string, mode string, output outputType) error {
var data []byte
var err error
decodeKey := decodeData([]byte(key), keyType)
decodeIV := decodeData([]byte(IV), ivType)
switch mode {
case "CBC":
data, err = codec.AESCBCEncrypt(decodeKey, flow.Text, decodeIV)
case "ECB":
data, err = codec.AESECBEncrypt(decodeKey, flow.Text, decodeIV)
case "GCM":
data, err = codec.AESGCMEncrypt(decodeKey, flow.Text, decodeIV)
default:
return utils.Error("AESEncryptEx: unknown mode")
}
if err == nil {
flow.Text = encodeData(data, output)
}
return err
}
// Tag = "解密"
// CodecName = "AES对称解密"
// Desc = """高级加密标准(AES)是美国联邦信息处理标准(FIPS)。它是在一个历时5年的过程中,从15个竞争设计中选出的。
// Key:根据密钥的大小,将使用以下算法:
// 16字节 = AES-128
// 24字节 = AES-192
// 32字节 = AES-256"""
// Params = [
// { Name = "key", Type = "inputSelect", Required = true,Label = "Key", Connector ={ Name = "keyType", Type = "select", DefaultValue = "hex", Options = ["hex", "raw", "base64"], Required = true ,Label = "key格式"} },
// { Name = "IV", Type = "inputSelect", Required = false ,Label = "IV", Connector ={ Name = "ivType", Type = "select", DefaultValue = "hex", Options = ["hex", "raw", "base64"], Required = true ,Label = "IV格式"} },
// { Name = "mode", Type = "select", DefaultValue = "CBC",Options = ["CBC", "ECB", "GCM"], Required = true, Label = "Mode"},
// { Name = "input", Type = "select", DefaultValue = "hex", Options = ["hex", "raw", "base64"], Required = true,Label = "输入格式"}
// ]
func (flow *CodecExecFlow) AESDecrypt(key string, keyType string, IV string, ivType string, mode string, input outputType) error {
var data []byte
var err error
decodeKey := decodeData([]byte(key), keyType)
decodeIV := decodeData([]byte(IV), ivType)
inputText := decodeData(flow.Text, input)
switch mode {
case "CBC":
data, err = codec.AESCBCDecrypt(decodeKey, inputText, decodeIV)
case "ECB":
data, err = codec.AESECBDecrypt(decodeKey, inputText, decodeIV)
case "GCM":
data, err = codec.AESGCMDecrypt(decodeKey, inputText, decodeIV)
default:
return utils.Error("AESEncryptEx: unknown mode")
}
if err == nil {
flow.Text = data
}
return err
}
// Tag = "加密"
// CodecName = "SM4对称加密"
// Desc = """SM4是一个128位的块密码,目前被确定为中国的国家标准(GB/T 32907-2016)。支持多种块密码模式。"""
// Params = [
// { Name = "key", Type = "inputSelect", Required = true,Label = "Key", Connector ={ Name = "keyType", Type = "select", DefaultValue = "hex", Options = ["hex", "raw", "base64"], Required = true ,Label = "key格式"} },
// { Name = "IV", Type = "inputSelect", Required = false ,Label = "IV", Connector ={ Name = "ivType", Type = "select", DefaultValue = "hex", Options = ["hex", "raw", "base64"], Required = true ,Label = "IV格式"} },
// { Name = "mode", Type = "select", DefaultValue = "CBC",Options = ["CBC", "ECB", "GCM", "CFB", "OFB"], Required = true, Label = "Mode"},
// { Name = "output", Type = "select", DefaultValue = "hex", Options = ["hex", "raw","base64"], Required = true,Label = "输出格式"}
// ]
func (flow *CodecExecFlow) SM4Encrypt(key string, keyType string, IV string, ivType string, mode string, output outputType) error {
var data []byte
var err error
decodeKey := decodeData([]byte(key), keyType)
decodeIV := decodeData([]byte(IV), ivType)
switch mode {
case "CBC":
data, err = codec.SM4CBCEnc(decodeKey, flow.Text, decodeIV)
case "ECB":
data, err = codec.SM4ECBEnc(decodeKey, flow.Text, decodeIV)
case "GCM":
data, err = codec.SM4GCMEnc(decodeKey, flow.Text, decodeIV)
case "CFB":
data, err = codec.SM4CFBEnc(decodeKey, flow.Text, decodeIV)
case "OFB":
data, err = codec.SM4OFBEnc(decodeKey, flow.Text, decodeIV)
default:
return utils.Error("AESEncryptEx: unknown mode")
}
if err == nil {
flow.Text = encodeData(data, output)
}
return err
}
// Tag = "解密"
// CodecName = "SM4对称解密"
// Desc = """SM4是一个128位的块密码,目前被确定为中国的国家标准(GB/T 32907-2016)。支持多种块密码模式。"""
// Params = [
// { Name = "key", Type = "inputSelect", Required = true,Label = "Key", Connector ={ Name = "keyType", Type = "select", DefaultValue = "hex", Options = ["hex", "raw", "base64"], Required = true ,Label = "key格式"} },
// { Name = "IV", Type = "inputSelect", Required = false ,Label = "IV", Connector ={ Name = "ivType", Type = "select", DefaultValue = "hex", Options = ["hex", "raw", "base64"], Required = true ,Label = "IV格式"} },
// { Name = "mode", Type = "select",DefaultValue = "CBC", Options = ["CBC", "ECB", "GCM", "CFB", "OFB"], Required = true, Label = "Mode"},
// { Name = "input", Type = "select", DefaultValue = "hex", Options = ["hex", "raw", "base64"], Required = true ,Label = "输入格式"}
// ]
func (flow *CodecExecFlow) SM4Decrypt(key string, keyType string, IV string, ivType string, mode string, input outputType) error {
var data []byte
var err error
decodeKey := decodeData([]byte(key), keyType)
decodeIV := decodeData([]byte(IV), ivType)
inputText := decodeData(flow.Text, input)
switch mode {
case "CBC":
data, err = codec.SM4CBCDec(decodeKey, inputText, decodeIV)
case "ECB":
data, err = codec.SM4ECBDec(decodeKey, inputText, decodeIV)
case "GCM":
data, err = codec.SM4GCMDec(decodeKey, inputText, decodeIV)
case "CFB":
data, err = codec.SM4CFBDec(decodeKey, inputText, decodeIV)
case "OFB":
data, err = codec.SM4OFBDec(decodeKey, inputText, decodeIV)
default:
return utils.Error("AESEncryptEx: unknown mode")
}
if err == nil {
flow.Text = data
}
return err
}
// Tag = "加密"
// CodecName = "DES对称加密"
// Desc = """DES(Data Encryption Standard)是一种对称密钥加密算法,使用固定有效长度为56位的密钥对数据进行64位的分组加密。尽管曾广泛使用,但由于密钥太短,现已被认为不够安全。"""
// Params = [
// { Name = "key", Type = "inputSelect", Required = true,Label = "Key", Connector ={ Name = "keyType", Type = "select", DefaultValue = "hex", Options = ["hex", "raw", "base64"], Required = true ,Label = "key格式"} },
// { Name = "IV", Type = "inputSelect", Required = false ,Label = "IV", Connector ={ Name = "ivType", Type = "select", DefaultValue = "hex", Options = ["hex", "raw", "base64"], Required = true ,Label = "IV格式"} },
// { Name = "mode", Type = "select",DefaultValue = "CBC", Options = ["CBC", "ECB"], Required = true , Label = "Mode"},
// { Name = "output", Type = "select", DefaultValue = "hex", Options = ["hex", "raw","base64"], Required = true,Label = "输出格式"}
// ]
func (flow *CodecExecFlow) DESEncrypt(key string, keyType string, IV string, ivType string, mode string, output outputType) error {
var data []byte
var err error
decodeKey := decodeData([]byte(key), keyType)
decodeIV := decodeData([]byte(IV), ivType)
switch mode {
case "CBC":
data, err = codec.DESCBCEnc(decodeKey, flow.Text, decodeIV)
case "ECB":
data, err = codec.DESECBEnc(decodeKey, flow.Text)
default:
return utils.Error("AESEncryptEx: unknown mode")
}
if err == nil {
flow.Text = encodeData(data, output)
}
return err
}
// Tag = "解密"
// CodecName = "DES对称解密"
// Desc = """DES(Data Encryption Standard)是一种对称密钥加密算法,使用固定有效长度为56位的密钥对数据进行64位的分组加密。尽管曾广泛使用,但由于密钥太短,现已被认为不够安全。"""
// Params = [
// { Name = "key", Type = "inputSelect", Required = true,Label = "Key", Connector ={ Name = "keyType", Type = "select", DefaultValue = "hex", Options = ["hex", "raw", "base64"], Required = true ,Label = "key格式"} },
// { Name = "IV", Type = "inputSelect", Required = false ,Label = "IV", Connector ={ Name = "ivType", Type = "select", DefaultValue = "hex", Options = ["hex", "raw", "base64"], Required = true ,Label = "IV格式"} },
// { Name = "mode", Type = "select",DefaultValue = "CBC", Options = ["CBC", "ECB"], Required = true , Label = "Mode"},
// { Name = "input", Type = "select", DefaultValue = "hex", Options = ["hex", "raw", "base64"], Required = true ,Label = "输入格式"}
// ]
func (flow *CodecExecFlow) DESDecrypt(key string, keyType string, IV string, ivType string, mode string, input outputType) error {
var data []byte
var err error
decodeKey := decodeData([]byte(key), keyType)
decodeIV := decodeData([]byte(IV), ivType)
inputText := decodeData(flow.Text, input)
switch mode {
case "CBC":
data, err = codec.DESCBCDec(decodeKey, inputText, decodeIV)
case "ECB":
data, err = codec.DESECBDec(decodeKey, inputText)
default:
return utils.Error("AESEncryptEx: unknown mode")
}
if err == nil {
flow.Text = data
}
return err
}
// Tag = "加密"
// CodecName = "TripleDES对称加密"
// Desc = """TripleDES(3DES)是DES的改进版,通过连续三次应用DES算法(可以使用三个不同的密钥)来增加加密的强度,提供了更高的安全性。"""
// Params = [
// { Name = "key", Type = "inputSelect", Required = true,Label = "Key", Connector ={ Name = "keyType", Type = "select", DefaultValue = "hex", Options = ["hex", "raw", "base64"], Required = true ,Label = "key格式"} },
// { Name = "IV", Type = "inputSelect", Required = false ,Label = "IV", Connector ={ Name = "ivType", Type = "select", DefaultValue = "hex", Options = ["hex", "raw", "base64"], Required = true ,Label = "IV格式"} },
// { Name = "mode", Type = "select",DefaultValue = "CBC", Options = ["CBC", "ECB"], Required = true, Label = "Mode"},
// { Name = "output", Type = "select",DefaultValue = "hex", Options = ["hex", "raw","base64"], Required = true ,Label = "输出格式"}
// ]
func (flow *CodecExecFlow) TripleDESEncrypt(key string, keyType string, IV string, ivType string, mode string, output outputType) error {
var data []byte
var err error
decodeKey := decodeData([]byte(key), keyType)
decodeIV := decodeData([]byte(IV), ivType)
switch mode {
case "CBC":
data, err = codec.TripleDES_CBCEnc(decodeKey, flow.Text, decodeIV)
case "ECB":
data, err = codec.TripleDES_ECBEnc(decodeKey, flow.Text)
default:
return utils.Error("AESEncryptEx: unknown mode")
}
if err == nil {
flow.Text = encodeData(data, output)
}
return err
}
// Tag = "解密"
// CodecName = "TripleDES对称解密"
// Desc = """TripleDES(3DES)是DES的改进版,通过连续三次应用DES算法(可以使用三个不同的密钥)来增加加密的强度,提供了更高的安全性。"""
// Params = [
// { Name = "key", Type = "inputSelect", Required = true,Label = "Key", Connector ={ Name = "keyType", Type = "select", DefaultValue = "hex", Options = ["hex", "raw", "base64"], Required = true ,Label = "key格式"} },
// { Name = "IV", Type = "inputSelect", Required = false ,Label = "IV", Connector ={ Name = "ivType", Type = "select", DefaultValue = "hex", Options = ["hex", "raw", "base64"], Required = true ,Label = "IV格式"} },
// { Name = "mode", Type = "select",DefaultValue = "CBC", Options = ["CBC", "ECB"], Required = true , Label = "Mode"},
// { Name = "input", Type = "select",DefaultValue = "hex", Options = ["hex", "raw", "base64"], Required = true ,Label = "输入格式"}
// ]
func (flow *CodecExecFlow) TripleDESDecrypt(key string, keyType string, IV string, ivType string, mode string, input outputType) error {
var data []byte
var err error
decodeKey := decodeData([]byte(key), keyType)
decodeIV := decodeData([]byte(IV), ivType)
inputText := decodeData(flow.Text, input)
switch mode {
case "CBC":
data, err = codec.TripleDES_CBCDec(decodeKey, inputText, decodeIV)
case "ECB":
data, err = codec.TripleDES_ECBDec(decodeKey, inputText)
default:
return utils.Error("AESEncryptEx: unknown mode")
}
if err == nil {
flow.Text = data
}
return err
}
// Tag = "加密"
// CodecName = "RSA加密"
// Desc = """RSA加密算法是一种非对称加密算法,在公开密钥加密和电子商业中被广泛使用。RSA是被研究得最广泛的公钥算法,从提出后经历了各种攻击的考验,逐渐为人们接受,普遍认为是目前最优秀的公钥方案之一。"""
// Params = [
// { Name = "pubKey", Type = "text", Required = true,Label = "pem公钥"},
// { Name = "encryptSchema", Type = "select",DefaultValue = "RSA-OAEP", Options = ["RSA-OAEP", "PKCS1v15"], Required = true, Label = "填充方式"},
// { Name = "algorithm", Type = "select",DefaultValue = "SHA-256", Options = ["SHA-1", "SHA-256","SHA-384","SHA-512","MD5"], Required = true ,Label = "hash算法"}
// ]
func (flow *CodecExecFlow) RSAEncrypt(pubKey string, encryptSchema string, algorithm string) error {
var data []byte
var err error
var hashFunc hash.Hash
switch algorithm { // choose alg
case "SHA-256":
hashFunc = sha256.New()
case "SHA-384":
hashFunc = sha512.New384()
case "SHA-512":
hashFunc = sha512.New()
case "MD5":
hashFunc = md5.New()
case "SHA-1":
fallthrough
default:
hashFunc = sha1.New()
}
switch encryptSchema {
case "RSA-OAEP":
data, err = tlsutils.PemPkcsOAEPEncryptWithHash([]byte(pubKey), flow.Text, hashFunc)
case "PKCS1v15":
data, err = tlsutils.PemPkcs1v15Encrypt([]byte(pubKey), flow.Text)
default:
return utils.Error("RSA encrypt error: 未知的填充方式")
}
if err == nil {
flow.Text = data
}
return err
}
// Tag = "解密"
// CodecName = "RSA解密"
// Desc = """RSA加密算法是一种非对称加密算法,在公开密钥加密和电子商业中被广泛使用。RSA是被研究得最广泛的公钥算法,从提出后经历了各种攻击的考验,逐渐为人们接受,普遍认为是目前最优秀的公钥方案之一。"""
// Params = [
// { Name = "priKey", Type = "text", Required = true,Label = "pem私钥"},
// { Name = "decryptSchema", Type = "select",DefaultValue = "RSA-OAEP", Options = ["RSA-OAEP", "PKCS1v15"], Required = true, Label = "填充方式"},
// { Name = "algorithm", Type = "select",DefaultValue = "SHA-256", Options = ["SHA-1", "SHA-256","SHA-384","SHA-512","MD5"], Required = true ,Label = "hash算法"}
// ]
func (flow *CodecExecFlow) RSADecrypt(priKey string, decryptSchema string, algorithm string) error {
var data []byte
var err error
var hashFunc hash.Hash
switch algorithm { // choose alg
case "SHA-256":
hashFunc = sha256.New()
case "SHA-384":
hashFunc = sha512.New384()
case "SHA-512":
hashFunc = sha512.New()
case "MD5":
hashFunc = md5.New()
case "SHA-1":
fallthrough
default:
hashFunc = sha1.New()
}
switch decryptSchema {
case "RSA-OAEP":
data, err = tlsutils.PemPkcsOAEPDecryptWithHash([]byte(priKey), flow.Text, hashFunc)
case "PKCS1v15":
data, err = tlsutils.PemPkcs1v15Decrypt([]byte(priKey), flow.Text)
default:
return utils.Error("RSA decrypt error: 未知的填充方式")
}
if err == nil {
flow.Text = data
}
return err
}
// Tag = "Java"
// CodecName = "反序列化"
// Desc = """Java反序列化是一种将字节流转换为Java对象的机制,以便可以在网络上传输或将其保存到文件中。
// Yak中提供了两种反序列化方式: dumper 和 object-stream ,其中object-stream是Yak独有的一种伪代码表达形式,更直观易读"""
// Params = [
// { Name = "input", Type = "select",DefaultValue = "raw", Options = ["raw", "hex", "base64"], Required = true , Label = "输入格式"},
// { Name = "output", Type = "select",DefaultValue = "dumper", Options = ["dumper", "object-stream"], Required = true , Label = "输出格式"}
// ]
func (flow *CodecExecFlow) JavaUnserialize(input string, output string) error {
var err error
raw := flow.Text
switch input {
case "raw":
raw = flow.Text
case "hex":
raw, err = codec.DecodeHex(string(flow.Text))
if err != nil {
return err
}
case "base64":
raw, err = codec.DecodeBase64(string(flow.Text))
if err != nil {
return err
}
default:
return utils.Error("JavaUnserialize: unknown input mod")
}
switch output {
case "dumper":
raw = []byte(yserx.JavaSerializedDumper(raw))
case "object-stream":
objs, err := yserx.ParseJavaSerialized(raw)
if err != nil {
return err
}
raw, err = yserx.ToJson(objs)
if err != nil {
return err
}
}
flow.Text = raw
return nil
}
// Tag = "Java"
// CodecName = "序列化"
// Desc = """Java序列化是一种将Java对象转换为字节流的机制,以便可以在网络上传输或将其保存到文件中。 """
// Params = [
// { Name = "output", Type = "select",DefaultValue = "raw", Options = ["raw", "hex", "base64"], Required = true , Label = "输出格式"}
// ]
func (flow *CodecExecFlow) JavaSerialize(output string) error {
var err error
obj, err := yserx.FromJson(flow.Text)
if err != nil {
return err
}
raw := yserx.MarshalJavaObjects(obj...)
switch output {
case "raw":
flow.Text = raw
case "hex":
flow.Text = []byte(codec.EncodeToHex(raw))
case "base64":
flow.Text = []byte(codec.EncodeBase64(raw))
default:
return utils.Error("JavaUnserialize: unknown input mod")
}
return nil
}
// Tag = "编码"
// CodecName = "base64编码"
// Desc = """Base64是一种基于64个可打印字符来表示二进制数据的表示方法。常用于在通常处理文本数据的场合,表示、传输、存储一些二进制数据,包括MIME的电子邮件及XML的一些复杂数据。
// eg: yak -> eWFr"""
// Params = [
// { Name = "Alphabet", Type = "select",DefaultValue = "standard", Options = ["standard", "urlsafe"], Required = true,Label = "Alphabet"}
// ]
func (flow *CodecExecFlow) Base64Encode(Alphabet string) error {
switch Alphabet {
case "standard":
flow.Text = []byte(codec.EncodeBase64(flow.Text))
case "urlsafe":
flow.Text = []byte(codec.EncodeBase64Url(flow.Text))
default:
return utils.Error("Base64Encode: unknown alphabet")
}
return nil
}
// Tag = "解码"
// CodecName = "base64解码"
// Desc = """Base64是一种基于64个可打印字符来表示二进制数据的表示方法。常用于在通常处理文本数据的场合,表示、传输、存储一些二进制数据,包括MIME的电子邮件及XML的一些复杂数据。
// eg: eWFr -> yak"""
// Params = [
// { Name = "Alphabet", Type = "select",DefaultValue = "standard",Options = ["standard", "urlsafe"], Required = true,Lable = "Alphabet" }
// ]
func (flow *CodecExecFlow) Base64Decode(Alphabet string) error {
var raw []byte
var err error
switch Alphabet {
case "standard":
raw, err = codec.DecodeBase64(string(flow.Text))
if err != nil {
return err
}
case "urlsafe":
raw, err = codec.DecodeBase64Url(flow.Text)
if err != nil {
return err
}
default:
return utils.Error("Base64Encode: unknown alphabet")
}
flow.Text = raw
return nil
}
// Tag = "编码"
// CodecName = "HTML编码"
// Desc = """HTML编码是一种将特殊字符转换为HTML实体的编码方式。"""
// Params = [
// { Name = "entityRef", Type = "select",DefaultValue = "named", Options = ["dec", "hex", "named"], Required = true ,Label = "实体编码格式"},
// { Name = "fullEncode", Type = "checkbox", Required = true , Label = "全部编码"}
// ]
func (flow *CodecExecFlow) HtmlEncode(entityRef string, fullEncode bool) error {
flow.Text = []byte(codec.EncodeHtmlEntityEx(flow.Text, entityRef, fullEncode))
return nil
}
// Tag = "解码"
// CodecName = "HTML解码"
// Desc = """HTML编码是一种将特殊字符转换为HTML实体的编码方式。"""
func (flow *CodecExecFlow) HtmlDecode() error {
flow.Text = []byte(codec.UnescapeHtmlString(string(flow.Text)))
return nil
}
// Tag = "编码"
// CodecName = "URL编码"
// Desc = """URL编码,又称百分号编码,是一种互联网标准,用于将非ASCII字符、保留字符或任何可能在URL中产生歧义的字符转换为一个百分号后跟两位十六进制数的形式,以确保网络传输的无歧义性和安全性。"""
// Params = [
// { Name = "fullEncode", Type = "checkbox", Required = true , Label = "全部编码"}
// ]
func (flow *CodecExecFlow) URLEncode(fullEncode bool) error {
if fullEncode {
flow.Text = []byte(codec.EncodeUrlCode(flow.Text))
} else {
flow.Text = []byte(codec.QueryEscape(string(flow.Text)))
}
return nil
}
// Tag = "解码"
// CodecName = "URL解码"
// Desc = """URL编码,又称百分号编码,是一种互联网标准,用于将非ASCII字符、保留字符或任何可能在URL中产生歧义的字符转换为一个百分号后跟两位十六进制数的形式,以确保网络传输的无歧义性和安全性。"""
func (flow *CodecExecFlow) URLDecode() error {
res, err := codec.QueryUnescape(string(flow.Text))
if err != nil {
return err
}
flow.Text = []byte(res)
return nil
}
// Tag = "编码"
// CodecName = "十六进制编码"
// Desc = """十六进制编码是一种数字表示法,使用0到9和A到F共16个字符来表示数值。在计算机科学中,它广泛用于简化二进制数据的表示,因为每4位二进制数(比特)可以用单个十六进制数精确表示。"""
func (flow *CodecExecFlow) HexEncode() error {
flow.Text = []byte(codec.EncodeToHex(flow.Text))
return nil
}
// Tag = "解码"
// CodecName = "十六进制解码"
// Desc = """十六进制编码是一种数字表示法,使用0到9和A到F共16个字符来表示数值。在计算机科学中,它广泛用于简化二进制数据的表示,因为每4位二进制数(比特)可以用单个十六进制数精确表示。"""
func (flow *CodecExecFlow) HexDecode() error {
res, err := codec.DecodeHex(string(flow.Text))
if err != nil {
return err
}
flow.Text = res
return nil
}
// Tag = "编码"
// CodecName = "Unicode 编码"
// Desc = """Unicode 编解码是将世界各种文字符号映射到唯一码点,并通过编码方案(如UTF-8、UTF-16)转为字节序列的过程,以支持全球文本的统一表示和处理。"""
func (flow *CodecExecFlow) UnicodeEncode() error {
flow.Text = []byte(codec.JsonUnicodeEncode(string(flow.Text)))
return nil
}
// Tag = "解码"
// CodecName = "Unicode 中文解码"
// Desc = """Unicode 编解码是将世界各种文字符号映射到唯一码点,并通过编码方案(如UTF-8、UTF-16)转为字节序列的过程,以支持全球文本的统一表示和处理。"""
func (flow *CodecExecFlow) UnicodeDecode() error {
flow.Text = []byte(codec.JsonUnicodeDecode(string(flow.Text)))
return nil
}
// Tag = "Hash"
// CodecName = "MD5"
// Desc = """MD5是一种广泛使用的加密哈希函数,它接受任意长度的输入并输出固定长度(128位)的哈希值。常用于验证数据完整性,但不适用于安全加密,因为存在碰撞漏洞。"""
func (flow *CodecExecFlow) MD5() error {
flow.Text = []byte(codec.Md5(flow.Text))
return nil
}
// Tag = "Hash"
// CodecName = "SM3"
// Desc = """SM3是一种密码哈希函数,由中国国家密码管理局发布,输出长度为256位。它用于确保数据的完整性和一致性,与MD5和SHA-1相比,SM3设计更安全,主要应用于中国的商用密码系统中。"""
func (flow *CodecExecFlow) SM3() error {
flow.Text = codec.SM3(flow.Text)
return nil
}
// Tag = "Hash"
// CodecName = "SHA-1"
// Desc = """SHA-1(安全哈希算法1)是一种加密哈希函数,输出160位哈希值,用于确保数据完整性。虽然曾广泛应用于安全领域,但由于潜在的安全漏洞,现在不再推荐用于敏感数据保护。"""
func (flow *CodecExecFlow) SHA1() error {
flow.Text = []byte(codec.Sha1(flow.Text))
return nil
}
// Tag = "Hash"
// CodecName = "SHA-2"
// Desc = """SHA-2是安全哈希算法家族的一部分,包括多个版本(如SHA-256和SHA-512),输出哈希值长度不同,用于数据完整性验证和数字签名,相较于SHA-1提供更强的安全性。"""
// Params = [
// { Name = "size", Type = "select",DefaultValue = "SHA-512", Options = ["SHA-224", "SHA-256","SHA-384","SHA-512"], Required = true ,Label = "哈希版本"}
// ]
func (flow *CodecExecFlow) SHA2(size string) error {
switch size {
case "SHA-224":
flow.Text = []byte(codec.Sha224(flow.Text))
case "SHA-256":
flow.Text = []byte(codec.Sha256(flow.Text))
case "SHA-384":
flow.Text = []byte(codec.Sha384(flow.Text))
case "SHA-512":
fallthrough
default:
flow.Text = []byte(codec.Sha512(flow.Text))
}
return nil
}
// Tag = "数据美化"
// CodecName = "Json处理"
// Desc = """JSON(JavaScript Object Notation)是一种轻量级数据交换格式,易于人阅读和编写,同时也易于机器解析和生成。它基于JavaScript语言标准,但独立于语言,被广泛应用于网络应用程序中数据的传输。"""
// Params = [
// { Name = "mode", Type = "select",DefaultValue = "两格缩进", Options = ["四格缩进", "两格缩进","压缩"], Required = true ,Label = "处理方式"}
// ]
func (flow *CodecExecFlow) JsonFormat(mode string) error {
var dst interface{}
err := json.Unmarshal(flow.Text, &dst)
if err != nil {
return err
}
var res []byte
switch mode {
case "两格缩进":
res, err = json.MarshalIndent(dst, "", " ")
case "压缩":
res, err = json.Marshal(dst)
case "四格缩进":
fallthrough
default:
res, err = json.MarshalIndent(dst, "", " ")
}
if err != nil {
return err
}
flow.Text = res
return nil
}
// Tag = "其他"
// CodecName = "生成数据包"
// Desc = """生成HTTP数据包,支持使用cURL和URL"""
// Params = [
// { Name = "mode", Type = "select",DefaultValue = "URL", Options = ["cURL", "URL"], Required = true ,Label = "输入格式"}
// ]
func (flow *CodecExecFlow) MakePacket(mode string) error {
var res []byte
var err error
switch mode {
case "cURL":
res, err = lowhttp.CurlToHTTPRequest(string(flow.Text))
case "URL":
res, err = lowhttp.UrlToHTTPRequest(strings.TrimSpace(string(flow.Text)))
default:
return utils.Error("MakeHTTPPacket: unknown mode")
}
if err != nil {
return err
}
flow.Text = res
return nil
}
// Tag = "其他"
// CodecName = "数据包生成cURL命令"
// Desc = """通过数据包生成cURL命令,以导出数据包"""
// Params = [
// { Name = "https", Type = "checkbox", Required = true , Label = "https"}
// ]
func (flow *CodecExecFlow) Packet2cURL(https bool) error {
cmd, err := lowhttp.GetCurlCommand(https, flow.Text)
if err != nil {
return utils.Errorf("codec[%v] failed: %s", `packet-to-curl`, err)
}
flow.Text = []byte(cmd.String())
return nil
}
// Tag = "其他"
// CodecName = "JWT解析"
// Desc = """JWT(JSON Web Token)是一种开放标准(RFC 7519),用于在网络应用间安全地传输声明信息,通常用于身份验证和信息交换。"""
func (flow *CodecExecFlow) JwtParse() error {
token, key, err := authhack.JwtParse(string(flow.Text))
if err != nil || token == nil {
return utils.Errorf("codec JWT解析 failed: %s", err)
}
flow.Text, err = json.MarshalIndent(map[string]interface{}{
"raw": token.Raw,
"alg": token.Method.Alg(),
"is_valid": token.Valid,
"brute_secret_key_finished": token.Valid,
"header": token.Header,
"claims": token.Claims,
"secret_key": utils.EscapeInvalidUTF8Byte(key),
}, "", " ")
return nil
}
// Tag = "其他"
// CodecName = "JWT签名"
// Desc = """JWT(JSON Web Token)是一种开放标准(RFC 7519),用于在网络应用间安全地传输声明信息,通常用于身份验证和信息交换。"""
// Params = [
// { Name = "algorithm", Type = "select",DefaultValue = "HS256",Options = ["ES384","ES256","ES512","HS256","HS384","HS512","PS256","PS384","PS512","RS256","RS384","RS512","None"], Required = true , Label = "签名算法"},
// { Name = "key", Type = "input", Required = true , Label = "JWT密钥"},
// { Name = "isBase64", Type = "checkbox", Required = true , Label = "base64编码"},
// ]
func (flow *CodecExecFlow) JwtSign(algorithm string, key []byte, isBase64 bool) error {
if !gjson.Valid(string(flow.Text)) {
return utils.Error("codec JWT签名失败: json格式错误")
}
data := make(map[string]interface{})
var err error
gjson.Parse(string(flow.Text)).ForEach(func(key, value gjson.Result) bool {
data[key.String()] = value.Value()
return true
})
if isBase64 {
key, err = codec.DecodeBase64(string(key))
if err != nil {
return utils.Wrapf(err, "codec JWT签名失败")
}
}
jwtSign, err := authhack.JwtGenerate(algorithm, data, "", key)
if err != nil {
return utils.Wrapf(err, "codec JWT签名失败")
}
flow.Text = []byte(jwtSign)
return nil
}
// Tag = "其他"
// CodecName = "fuzztag渲染"
// Desc = """渲染fuzztag"""
func (flow *CodecExecFlow) Fuzz() error {
res, err := mutate.FuzzTagExec(flow.Text, mutate.Fuzz_WithEnableDangerousTag())
if err != nil {
return err
}
flow.Text = []byte(strings.Join(res, "\n"))
return nil
}
// Tag = "其他"
// CodecName = "Replace"
// Desc = """替换字符串处理本文"""
// Params = [
// { Name = "find", Type = "input", Required = true , Label = "Find"},
// { Name = "replace", Type = "input", Required = false , Label = "Replace"},
// { Name = "findType", Type = "select",DefaultValue = "regexp",Options = ["regexp","raw"], Required = true , Label = "查找方式"},
// { Name = "Global", Type = "checkbox", Required = true , Label = "全部匹配"},
// { Name = "IgnoreCase", Type = "checkbox", Required = true , Label = "忽略大小写"},
// { Name = "Multiline", Type = "checkbox", Required = true , Label = "多行匹配"},
// ]
func (flow *CodecExecFlow) Replace(find string, replace string, findType string, Global, Multiline, IgnoreCase bool) error {
count := 1
if Global {
count = -1
}
if findType == "raw" {
find = regexp.QuoteMeta(find)
}
regFlag := regexp2.None
if Multiline {
regFlag = regFlag | regexp2.Multiline
}
if IgnoreCase {
regFlag = regFlag | regexp2.IgnoreCase
}
reg, err := regexp2.Compile(find, regFlag)
if err != nil {
return err
}
text, err := reg.Replace(string(flow.Text), replace, -1, count)
if err != nil {
return err
}
flow.Text = []byte(text)
return nil
}
// Tag = "其他"
// CodecName = "Find"
// Desc = """替换字符串处理本文"""
// Params = [
// { Name = "find", Type = "input", Required = true , Label = "Find"},
// { Name = "findType", Type = "select",DefaultValue = "regexp",Options = ["regexp","raw"], Required = true , Label = "查找方式"},
// { Name = "Global", Type = "checkbox", Required = true , Label = "全部匹配"},
// { Name = "IgnoreCase", Type = "checkbox", Required = true , Label = "忽略大小写"},
// { Name = "Multiline", Type = "checkbox", Required = true , Label = "多行匹配"},
// ]
func (flow *CodecExecFlow) Find(find string, findType string, Global, Multiline, IgnoreCase bool) error {
if findType == "raw" {
find = regexp.QuoteMeta(find)
}
regFlag := regexp2.None
if Multiline {
regFlag = regFlag | regexp2.Multiline
}
if IgnoreCase {
regFlag = regFlag | regexp2.IgnoreCase
}
reg, err := regexp2.Compile(find, regFlag)
if err != nil {
return err
}
match, err := reg.FindStringMatch(string(flow.Text))
if err != nil || match == nil { // match fail return []byte("")
flow.Text = []byte("")
return nil
}
text := match.String()
if Global {
for {
match, err = reg.FindNextMatch(match)
if err != nil || match == nil {
break
}
text = strings.Join([]string{text, match.String()}, "\n")
}
}
flow.Text = []byte(text)
return nil
}
// Tag = "Yak脚本"
// CodecName = "本地Codec插件"
// Desc = """本地Codec插件"""
// Params = [
// { Name = "pluginName", Type = "search", Required = true , Label = "插件名"},
// ]
func (flow *CodecExecFlow) CodecPlugin(pluginName string) error {
script, err := yakit.GetYakScriptByName(consts.GetGormProfileDatabase(), pluginName)
if err != nil {
return err
}
engine, err := yak.NewScriptEngine(1000).ExecuteEx(script.Content, map[string]interface{}{
"YAK_FILENAME": pluginName,
})
if err != nil {
return utils.Errorf("execute file %s code failed: %s", pluginName, err.Error())
}
pluginRes, err := engine.CallYakFunction(context.Background(), "handle", []interface{}{string(flow.Text)})
if err != nil {
return utils.Errorf("import %v' s handle failed: %s", pluginName, err)
}
flow.Text = utils.InterfaceToBytes(pluginRes)
return nil
}
// Tag = "Yak脚本"
// CodecName = "临时Codec插件"
// Desc = """自定义临时Codec插件"""
// Params = [
// { Name = "pluginContent", Type = "monaco", Required = true , Label = "插件内容"},
// ]
func (flow *CodecExecFlow) CustomCodecPlugin(pluginContent string) error {
engine, err := yak.NewScriptEngine(1000).ExecuteEx(pluginContent, map[string]interface{}{
"YAK_FILENAME": "temp-codec",
})
if err != nil {
return utils.Errorf("execute file %s code failed: %s", "temp-codec", err.Error())
}
pluginRes, err := engine.CallYakFunction(context.Background(), "handle", []interface{}{string(flow.Text)})
if err != nil {
return utils.Errorf("import %v' s handle failed: %s", "temp-codec", err)
}
flow.Text = utils.InterfaceToBytes(pluginRes)
return nil
}