-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathCryptoLibraries.qll
More file actions
743 lines (644 loc) · 20.9 KB
/
CryptoLibraries.qll
File metadata and controls
743 lines (644 loc) · 20.9 KB
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
/**
* Provides classes for modelling cryptographic libraries.
*/
import javascript
/**
* Names of cryptographic algorithms, separated into strong and weak variants.
*
* The names are normalized: upper-case, no spaces, dashes or underscores.
*
* The names are inspired by the names used in real world crypto libraries.
*
* The classification into strong and weak are based on Wikipedia, OWASP and google (2017).
*/
private module AlgorithmNames {
predicate isStrongHashingAlgorithm(string name) {
name = "DSA" or
name = "ED25519" or
name = "ES256" or
name = "ECDSA256" or
name = "ES384" or
name = "ECDSA384" or
name = "ES512" or
name = "ECDSA512" or
name = "SHA2" or
name = "SHA224" or
name = "SHA256" or
name = "SHA384" or
name = "SHA512" or
name = "SHA3"
}
predicate isWeakHashingAlgorithm(string name) {
name = "HAVEL128" or
name = "MD2" or
name = "MD4" or
name = "MD5" or
name = "PANAMA" or
name = "RIPEMD" or
name = "RIPEMD128" or
name = "RIPEMD256" or
name = "RIPEMD160" or
name = "RIPEMD320" or
name = "SHA0" or
name = "SHA1"
}
predicate isStrongEncryptionAlgorithm(string name) {
name = "AES" or
name = "AES128" or
name = "AES192" or
name = "AES256" or
name = "AES512" or
name = "RSA" or
name = "RABBIT" or
name = "BLOWFISH"
}
predicate isWeakEncryptionAlgorithm(string name) {
name = "DES" or
name = "3DES" or
name = "TRIPLEDES" or
name = "TDEA" or
name = "TRIPLEDEA" or
name = "ARC2" or
name = "RC2" or
name = "ARC4" or
name = "RC4" or
name = "ARCFOUR" or
name = "ARC5" or
name = "RC5"
}
predicate isStrongPasswordHashingAlgorithm(string name) {
name = "ARGON2" or
name = "PBKDF2" or
name = "BCRYPT" or
name = "SCRYPT"
}
predicate isWeakPasswordHashingAlgorithm(string name) { none() }
}
private import AlgorithmNames
/**
* A cryptographic algorithm.
*/
private newtype TCryptographicAlgorithm =
MkHashingAlgorithm(string name, boolean isWeak) {
isStrongHashingAlgorithm(name) and isWeak = false
or
isWeakHashingAlgorithm(name) and isWeak = true
} or
MkEncryptionAlgorithm(string name, boolean isWeak) {
isStrongEncryptionAlgorithm(name) and isWeak = false
or
isWeakEncryptionAlgorithm(name) and isWeak = true
} or
MkPasswordHashingAlgorithm(string name, boolean isWeak) {
isStrongPasswordHashingAlgorithm(name) and isWeak = false
or
isWeakPasswordHashingAlgorithm(name) and isWeak = true
}
/**
* A cryptographic algorithm.
*/
abstract class CryptographicAlgorithm extends TCryptographicAlgorithm {
/** Gets a textual representation of this element. */
string toString() { result = getName() }
/**
* Gets the name of this algorithm.
*/
abstract string getName();
/**
* Holds if the name of this algorithm matches `name` modulo case,
* white space, dashes and underscores.
*/
bindingset[name]
predicate matchesName(string name) {
name.toUpperCase().regexpReplaceAll("[-_ ]", "") = getName()
}
/**
* Holds if this algorithm is weak.
*/
abstract predicate isWeak();
}
/**
* A hashing algorithm such as `MD5` or `SHA512`.
*/
class HashingAlgorithm extends MkHashingAlgorithm, CryptographicAlgorithm {
string name;
boolean isWeak;
HashingAlgorithm() { this = MkHashingAlgorithm(name, isWeak) }
override string getName() { result = name }
override predicate isWeak() { isWeak = true }
}
/**
* An encryption algorithm such as `DES` or `AES512`.
*/
class EncryptionAlgorithm extends MkEncryptionAlgorithm, CryptographicAlgorithm {
string name;
boolean isWeak;
EncryptionAlgorithm() { this = MkEncryptionAlgorithm(name, isWeak) }
override string getName() { result = name }
override predicate isWeak() { isWeak = true }
}
/**
* A password hashing algorithm such as `PBKDF2` or `SCRYPT`.
*/
class PasswordHashingAlgorithm extends MkPasswordHashingAlgorithm, CryptographicAlgorithm {
string name;
boolean isWeak;
PasswordHashingAlgorithm() { this = MkPasswordHashingAlgorithm(name, isWeak) }
override string getName() { result = name }
override predicate isWeak() { isWeak = true }
}
/**
* An application of a cryptographic algorithm.
*/
abstract class CryptographicOperation extends Expr {
/**
* Gets the input the algorithm is used on, e.g. the plain text input to be encrypted.
*/
abstract Expr getInput();
/**
* Gets the applied algorithm.
*/
abstract CryptographicAlgorithm getAlgorithm();
}
/**
* A key used in a cryptographic algorithm.
*/
abstract class CryptographicKey extends DataFlow::ValueNode { }
/**
* A key used in a cryptographic algorithm, viewed as a `CredentialsExpr`.
*/
class CryptographicKeyCredentialsExpr extends CredentialsExpr {
CryptographicKeyCredentialsExpr() { this = any(CryptographicKey k).asExpr() }
override string getCredentialsKind() { result = "key" }
}
/**
* A model of the asmCrypto library.
*/
private module AsmCrypto {
private class Apply extends CryptographicOperation {
Expr input;
CryptographicAlgorithm algorithm; // non-functional
Apply() {
/*
* ```
* asmCrypto.SHA256.hex(input)
* ```
* matched as:
* ```
* asmCrypto.<algorithmName>._(<input>)
* ```
*/
exists(DataFlow::CallNode mce | this = mce.asExpr() |
exists(DataFlow::SourceNode asmCrypto, string algorithmName |
asmCrypto = DataFlow::globalVarRef("asmCrypto") and
algorithm.matchesName(algorithmName) and
mce = asmCrypto.getAPropertyRead(algorithmName).getAMemberCall(_) and
input = mce.getAnArgument().asExpr()
)
)
}
override Expr getInput() { result = input }
override CryptographicAlgorithm getAlgorithm() { result = algorithm }
}
}
/**
* A model of the browserid-crypto library.
*/
private module BrowserIdCrypto {
private class Key extends CryptographicKey {
Key() { this = any(Apply apply).getKey() }
}
private class Apply extends CryptographicOperation {
CryptographicAlgorithm algorithm; // non-functional
MethodCallExpr mce;
Apply() {
/*
* ```
* var jwcrypto = require("browserid-crypto");
* jwcrypto.generateKeypair({algorithm: 'DSA'}, function() {
* jwcrypto.sign(input, key);
* });
* ```
* matched as:
* ```
* var jwcrypto = require("browserid-crypto");
* jwcrypto.generateKeypair({algorithm: <algorithmName>}, function() {
* jwcrypto.sign(<input>, <key>);
* });
* ```
*/
this = mce and
exists(
DataFlow::SourceNode mod, DataFlow::Node algorithmNameNode, DataFlow::CallNode keygen,
DataFlow::FunctionNode callback
|
mod = DataFlow::moduleImport("browserid-crypto") and
keygen = mod.getAMemberCall("generateKeypair") and
algorithmNameNode = keygen.getOptionArgument(0, "algorithm") and
algorithm.matchesName(algorithmNameNode.getStringValue()) and
callback = keygen.getCallback(1) and
this = mod.getAMemberCall("sign").asExpr()
)
}
override Expr getInput() { result = mce.getArgument(0) }
override CryptographicAlgorithm getAlgorithm() { result = algorithm }
DataFlow::Node getKey() { result.asExpr() = mce.getArgument(1) }
}
}
/**
* A model of the Node.js builtin crypto library.
*/
private module NodeJSCrypto {
private class InstantiatedAlgorithm extends DataFlow::CallNode {
CryptographicAlgorithm algorithm; // non-functional
InstantiatedAlgorithm() {
/*
* ```
* const crypto = require('crypto');
* const cipher = crypto.createCipher('aes192', 'a password');
* ```
* matched as:
* ```
* const crypto = require('crypto');
* const cipher = crypto.createCipher(<algorithmName>, 'a password');
* ```
* Also matches `createHash`, `createHmac`, `createSign` instead of `createCipher`.
*/
exists(DataFlow::SourceNode mod, string createSuffix |
createSuffix = "Hash" or
createSuffix = "Hmac" or
createSuffix = "Sign" or
createSuffix = "Cipher"
|
mod = DataFlow::moduleImport("crypto") and
this = mod.getAMemberCall("create" + createSuffix) and
algorithm.matchesName(getArgument(0).getStringValue())
)
}
CryptographicAlgorithm getAlgorithm() { result = algorithm }
}
private class Apply extends CryptographicOperation, MethodCallExpr {
InstantiatedAlgorithm instantiation;
Apply() {
this = instantiation.getAMethodCall(any(string m | m = "update" or m = "write")).asExpr()
}
override Expr getInput() { result = getArgument(0) }
override CryptographicAlgorithm getAlgorithm() { result = instantiation.getAlgorithm() }
}
private class Key extends CryptographicKey {
Key() {
exists(InstantiatedAlgorithm instantiation, string name |
name = "setPrivateKey" or
name = "sign"
|
this = instantiation.getAMethodCall(name).getArgument(0)
)
or
exists(DataFlow::SourceNode mod, string name, DataFlow::InvokeNode call, int index |
mod = DataFlow::moduleImport("crypto") and
call = mod.getAMemberCall(name) and
this = call.getArgument(index)
|
index = 0 and
(name = "privateDecrypt" or name = "privateEncrypt")
or
index = 1 and
(name = "createCipheriv" or name = "Decipheriv" or name = "createHmac")
)
}
}
}
/**
* A model of the crypto-js library.
*/
private module CryptoJS {
/**
* Matches `CryptoJS.<algorithmName>` and `require("crypto-js/<algorithmName>")`
*/
private DataFlow::SourceNode getAlgorithmExpr(CryptographicAlgorithm algorithm) {
exists(string algorithmName | algorithm.matchesName(algorithmName) |
exists(DataFlow::SourceNode mod | mod = DataFlow::moduleImport("crypto-js") |
result = mod.getAPropertyRead(algorithmName) or
result = mod.getAPropertyRead("Hmac" + algorithmName) // they prefix Hmac
)
or
exists(DataFlow::SourceNode mod |
mod = DataFlow::moduleImport("crypto-js/" + algorithmName) and
result = mod
)
)
}
private DataFlow::CallNode getEncryptionApplication(Expr input, CryptographicAlgorithm algorithm) {
/*
* ```
* var CryptoJS = require("crypto-js");
* CryptoJS.AES.encrypt('my message', 'secret key 123');
* ```
* Matched as:
* ```
* var CryptoJS = require("crypto-js");
* CryptoJS.<algorithmName>.encrypt(<input>, 'secret key 123');
* ```
* Also matches where `CryptoJS.<algorithmName>` has been replaced by `require("crypto-js/<algorithmName>")`
*/
result = getAlgorithmExpr(algorithm).getAMemberCall("encrypt") and
input = result.getArgument(0).asExpr()
}
private DataFlow::CallNode getDirectApplication(Expr input, CryptographicAlgorithm algorithm) {
/*
* ```
* var CryptoJS = require("crypto-js");
* CryptoJS.SHA1("Message", "Key");
* ```
* Matched as:
* ```
* var CryptoJS = require("crypto-js");
* CryptoJS.<algorithmName>(<input>, "Key");
* ```
* An `Hmac`-prefix of <algorithmName> is ignored.
* Also matches where `CryptoJS.<algorithmName>` has been replaced by `require("crypto-js/<algorithmName>")`
*/
result = getAlgorithmExpr(algorithm).getACall() and
input = result.getArgument(0).asExpr()
}
private class Apply extends CryptographicOperation {
Expr input;
CryptographicAlgorithm algorithm; // non-functional
Apply() {
this = getEncryptionApplication(input, algorithm).asExpr() or
this = getDirectApplication(input, algorithm).asExpr()
}
override Expr getInput() { result = input }
override CryptographicAlgorithm getAlgorithm() { result = algorithm }
}
private class Key extends CryptographicKey {
Key() {
exists(DataFlow::SourceNode e, CryptographicAlgorithm algorithm |
e = getAlgorithmExpr(algorithm)
|
exists(string name |
name = "encrypt" or
name = "decrypt"
|
algorithm instanceof EncryptionAlgorithm and
this = e.getAMemberCall(name).getArgument(1)
)
or
algorithm instanceof HashingAlgorithm and
this = e.getACall().getArgument(1)
)
}
}
}
/**
* A model of the TweetNaCl library.
*/
private module TweetNaCl {
private class Apply extends CryptographicOperation {
Expr input;
CryptographicAlgorithm algorithm;
MethodCallExpr mce;
Apply() {
/*
* ```
* require("nacl").sign('my message');
* ```
* Matched as:
* ```
* require("nacl").sign(<input>);
* ```
* Also matches the "hash" method name, and the "nacl-fast" module.
*/
this = mce and
exists(DataFlow::SourceNode mod, string name |
name = "hash" and algorithm.matchesName("SHA512")
or
name = "sign" and algorithm.matchesName("ed25519")
|
(mod = DataFlow::moduleImport("nacl") or mod = DataFlow::moduleImport("nacl-fast")) and
mce = mod.getAMemberCall(name).asExpr() and
mce.getArgument(0) = input
)
}
override Expr getInput() { result = input }
override CryptographicAlgorithm getAlgorithm() { result = algorithm }
}
}
/**
* A model of the hash.js library.
*/
private module HashJs {
/**
* Matches:
* - `require("hash.js").<algorithmName>`()
* - `require("hash.js/lib/hash/<algorithmName>")`()
* - `require("hash.js/lib/hash/sha/<sha-algorithm-suffix>")`()
*/
private DataFlow::CallNode getAlgorithmExpr(CryptographicAlgorithm algorithm) {
exists(string algorithmName | algorithm.matchesName(algorithmName) |
result = DataFlow::moduleMember("hash.js", algorithmName).getACall()
or
exists(DataFlow::SourceNode mod |
mod = DataFlow::moduleImport("hash.js/lib/hash/" + algorithmName)
or
exists(string size |
mod = DataFlow::moduleImport("hash.js/lib/hash/sha/" + size) and
algorithmName = "SHA" + size
)
|
result = mod.getACall()
)
)
}
private class Apply extends CryptographicOperation {
Expr input;
CryptographicAlgorithm algorithm; // non-functional
MethodCallExpr mce;
Apply() {
/*
* ```
* var hash = require("hash.js");
* hash.sha512().update('my message', 'secret key 123');
* ```
* Matched as:
* ```
* var hash = require("hash.js");
* hash.<algorithmName>().update('my message', 'secret key 123');
* ```
* Also matches where `hash.<algorithmName>()` has been replaced by a more specific require a la `require("hash.js/lib/hash/sha/512")`
*/
this = mce and
mce = getAlgorithmExpr(algorithm).getAMemberCall("update").asExpr() and
input = mce.getArgument(0)
}
override Expr getInput() { result = input }
override CryptographicAlgorithm getAlgorithm() { result = algorithm }
}
}
/**
* A model of the forge library.
*/
private module Forge {
private DataFlow::SourceNode getAnImportNode() {
result = DataFlow::moduleImport("forge") or
result = DataFlow::moduleImport("node-forge")
}
abstract private class Cipher extends DataFlow::CallNode {
abstract CryptographicAlgorithm getAlgorithm();
}
private class KeyCipher extends Cipher {
DataFlow::Node key;
CryptographicAlgorithm algorithm; // non-functional
KeyCipher() {
exists(DataFlow::SourceNode mod, string algorithmName |
mod = getAnImportNode() and
algorithm.matchesName(algorithmName)
|
exists(string createName, string cipherName, string cipherPrefix, string cipherSuffix |
// `require('forge').cipher.createCipher("3DES-CBC").update("secret", "key");`
(createName = "createCipher" or createName = "createDecipher") and
this = mod.getAPropertyRead("cipher").getAMemberCall(createName) and
getArgument(0).asExpr().mayHaveStringValue(cipherName) and
cipherName = cipherPrefix + "-" + cipherSuffix and
(
cipherSuffix = "CBC" or
cipherSuffix = "CFB" or
cipherSuffix = "CTR" or
cipherSuffix = "ECB" or
cipherSuffix = "GCM" or
cipherSuffix = "OFB"
) and
algorithmName = cipherPrefix and
key = getArgument(1)
)
or
// `require("forge").rc2.createEncryptionCipher("key").update("secret");`
exists(string createName |
createName = "createEncryptionCipher" or createName = "createDecryptionCipher"
|
this = mod.getAPropertyRead(algorithmName).getAMemberCall(createName) and
key = getArgument(0)
)
)
}
override CryptographicAlgorithm getAlgorithm() { result = algorithm }
DataFlow::Node getKey() { result = key }
}
private class NonKeyCipher extends Cipher {
CryptographicAlgorithm algorithm; // non-functional
NonKeyCipher() {
exists(string algorithmName | algorithm.matchesName(algorithmName) |
// require("forge").md.md5.create().update('The quick brown fox jumps over the lazy dog');
this =
getAnImportNode()
.getAPropertyRead("md")
.getAPropertyRead(algorithmName)
.getAMemberCall("create")
)
}
override CryptographicAlgorithm getAlgorithm() { result = algorithm }
}
private class Apply extends CryptographicOperation {
Expr input;
CryptographicAlgorithm algorithm; // non-functional
MethodCallExpr mce;
Apply() {
this = mce and
exists(Cipher cipher |
mce = cipher.getAMemberCall("update").asExpr() and
mce.getArgument(0) = input and
algorithm = cipher.getAlgorithm()
)
}
override Expr getInput() { result = input }
override CryptographicAlgorithm getAlgorithm() { result = algorithm }
}
private class Key extends CryptographicKey {
Key() { this = any(KeyCipher cipher).getKey() }
}
}
/**
* A model of the md5 library.
*/
private module Md5 {
private class Apply extends CryptographicOperation {
Expr input;
CryptographicAlgorithm algorithm;
CallExpr call;
Apply() {
// `require("md5")("message");`
this = call and
exists(DataFlow::SourceNode mod |
mod = DataFlow::moduleImport("md5") and
algorithm.matchesName("MD5") and
call = mod.getACall().asExpr() and
call.getArgument(0) = input
)
}
override Expr getInput() { result = input }
override CryptographicAlgorithm getAlgorithm() { result = algorithm }
}
}
/**
* A model of the bcrypt, bcryptjs, bcrypt-nodejs libraries.
*/
private module Bcrypt {
private class Apply extends CryptographicOperation {
Expr input;
CryptographicAlgorithm algorithm;
MethodCallExpr mce;
Apply() {
// `require("bcrypt").hash(password);` with minor naming variations
this = mce and
exists(DataFlow::SourceNode mod, string moduleName, string methodName |
algorithm.matchesName("BCRYPT") and
(
moduleName = "bcrypt" or
moduleName = "bcryptjs" or
moduleName = "bcrypt-nodejs"
) and
(
methodName = "hash" or
methodName = "hashSync"
) and
mod = DataFlow::moduleImport(moduleName) and
mce = mod.getAMemberCall(methodName).asExpr() and
mce.getArgument(0) = input
)
}
override Expr getInput() { result = input }
override CryptographicAlgorithm getAlgorithm() { result = algorithm }
}
}
/**
* A model of the hasha library.
*/
private module Hasha {
private class Apply extends CryptographicOperation {
Expr input;
CryptographicAlgorithm algorithm;
CallExpr call;
Apply() {
// `require('hasha')('unicorn', { algorithm: "md5" });`
this = call and
exists(DataFlow::SourceNode mod, string algorithmName, Expr algorithmNameNode |
mod = DataFlow::moduleImport("hasha") and
call = mod.getACall().asExpr() and
call.getArgument(0) = input and
algorithm.matchesName(algorithmName) and
call.hasOptionArgument(1, "algorithm", algorithmNameNode) and
algorithmNameNode.mayHaveStringValue(algorithmName)
)
}
override Expr getInput() { result = input }
override CryptographicAlgorithm getAlgorithm() { result = algorithm }
}
/**
* Provides classes for working with the `express-jwt` package (https://github.com/auth0/express-jwt);
*/
module ExpressJwt {
private class Key extends CryptographicKey {
Key() { this = DataFlow::moduleMember("express-jwt", "sign").getACall().getArgument(1) }
}
}
}