blint 2.3.1__tar.gz → 2.4.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (40) hide show
  1. {blint-2.3.1 → blint-2.4.0}/PKG-INFO +53 -24
  2. {blint-2.3.1 → blint-2.4.0}/README.md +25 -5
  3. {blint-2.3.1 → blint-2.4.0}/blint/cli.py +99 -56
  4. {blint-2.3.1 → blint-2.4.0}/blint/config.py +78 -0
  5. blint-2.4.0/blint/db.py +150 -0
  6. blint-2.4.0/blint/lib/__init__.py +0 -0
  7. blint-2.4.0/blint/lib/analysis.py +354 -0
  8. {blint-2.3.1/blint → blint-2.4.0/blint/lib}/android.py +14 -11
  9. {blint-2.3.1/blint → blint-2.4.0/blint/lib}/binary.py +29 -25
  10. {blint-2.3.1/blint → blint-2.4.0/blint/lib}/checks.py +1 -1
  11. blint-2.4.0/blint/lib/runners.py +385 -0
  12. {blint-2.3.1/blint → blint-2.4.0/blint/lib}/sbom.py +172 -132
  13. {blint-2.3.1/blint → blint-2.4.0/blint/lib}/utils.py +92 -20
  14. {blint-2.3.1 → blint-2.4.0}/blint/logger.py +1 -1
  15. {blint-2.3.1 → blint-2.4.0}/pyproject.toml +38 -27
  16. blint-2.3.1/blint/analysis.py +0 -714
  17. {blint-2.3.1 → blint-2.4.0}/LICENSE +0 -0
  18. {blint-2.3.1 → blint-2.4.0}/blint/__init__.py +0 -0
  19. {blint-2.3.1 → blint-2.4.0}/blint/cyclonedx/README.md +0 -0
  20. {blint-2.3.1 → blint-2.4.0}/blint/cyclonedx/__init__.py +0 -0
  21. {blint-2.3.1 → blint-2.4.0}/blint/cyclonedx/spdx.py +0 -0
  22. {blint-2.3.1 → blint-2.4.0}/blint/cyclonedx/spec.py +0 -0
  23. {blint-2.3.1 → blint-2.4.0}/blint/data/__init__.py +0 -0
  24. {blint-2.3.1 → blint-2.4.0}/blint/data/annotations/__init__.py +0 -0
  25. {blint-2.3.1 → blint-2.4.0}/blint/data/annotations/review_entries_generic.yml +0 -0
  26. {blint-2.3.1 → blint-2.4.0}/blint/data/annotations/review_entries_pe.yml +0 -0
  27. {blint-2.3.1 → blint-2.4.0}/blint/data/annotations/review_exe_go.yml +0 -0
  28. {blint-2.3.1 → blint-2.4.0}/blint/data/annotations/review_imports_pe.yml +0 -0
  29. {blint-2.3.1 → blint-2.4.0}/blint/data/annotations/review_methods_generic.yml +0 -0
  30. {blint-2.3.1 → blint-2.4.0}/blint/data/annotations/review_methods_mips.yml +0 -0
  31. {blint-2.3.1 → blint-2.4.0}/blint/data/annotations/review_monero_generic.yml +0 -0
  32. {blint-2.3.1 → blint-2.4.0}/blint/data/annotations/review_monero_go.yml +0 -0
  33. {blint-2.3.1 → blint-2.4.0}/blint/data/annotations/review_monero_rust.yml +0 -0
  34. {blint-2.3.1 → blint-2.4.0}/blint/data/annotations/review_rootkits_win.yml +0 -0
  35. {blint-2.3.1 → blint-2.4.0}/blint/data/annotations/review_symbols_antiforensic.yml +0 -0
  36. {blint-2.3.1 → blint-2.4.0}/blint/data/annotations/review_symbols_generic.yml +0 -0
  37. {blint-2.3.1 → blint-2.4.0}/blint/data/annotations/review_symbols_hooka.yml +0 -0
  38. {blint-2.3.1 → blint-2.4.0}/blint/data/annotations/review_symbols_macho.yml +0 -0
  39. {blint-2.3.1 → blint-2.4.0}/blint/data/annotations/review_symbols_rust.yml +0 -0
  40. {blint-2.3.1 → blint-2.4.0}/blint/data/rules.yml +0 -0
@@ -1,35 +1,44 @@
1
- Metadata-Version: 2.1
1
+ Metadata-Version: 2.3
2
2
  Name: blint
3
- Version: 2.3.1
3
+ Version: 2.4.0
4
4
  Summary: Linter and SBOM generator for binary files.
5
- Home-page: https://github.com/OWASP-dep-scan/blint
6
5
  License: MIT
7
6
  Keywords: linter,binary,security,sast
8
- Author: Prabhu Subramanian
9
- Author-email: prabhu@appthreat.com
7
+ Author: Team AppThreat
8
+ Author-email: cloud@appthreat.com
10
9
  Requires-Python: >=3.10,<3.14
11
10
  Classifier: Development Status :: 5 - Production/Stable
12
11
  Classifier: Intended Audience :: Developers
13
12
  Classifier: Intended Audience :: System Administrators
14
- Classifier: License :: OSI Approved :: MIT License
15
- Classifier: Operating System :: OS Independent
16
- Classifier: Programming Language :: Python :: 3
13
+ Classifier: Topic :: Utilities
14
+ Classifier: Topic :: Security
17
15
  Classifier: Programming Language :: Python :: 3.10
18
16
  Classifier: Programming Language :: Python :: 3.11
19
17
  Classifier: Programming Language :: Python :: 3.12
20
18
  Classifier: Programming Language :: Python :: 3.13
21
- Classifier: Topic :: Security
22
- Classifier: Topic :: Utilities
23
- Requires-Dist: PyYAML (>=6.0.1,<7.0.0)
24
- Requires-Dist: ar (>=0.9.1,<0.10.0)
25
- Requires-Dist: defusedxml (>=0.7.1,<0.8.0)
26
- Requires-Dist: lief (>=0.15.1,<0.16.0)
27
- Requires-Dist: orjson (>=3.10.6,<4.0.0)
28
- Requires-Dist: pydantic[email] (>=2.8.2,<3.0.0)
29
- Requires-Dist: rich (>=13.7.1,<14.0.0)
19
+ Classifier: Operating System :: OS Independent
20
+ Provides-Extra: db
21
+ Provides-Extra: dev
22
+ Requires-Dist: PyYAML (>=6.0.2)
23
+ Requires-Dist: appdirs (>=1.4.4)
24
+ Requires-Dist: apsw (>=3.49.1.0)
25
+ Requires-Dist: ar (>=0.9.1)
26
+ Requires-Dist: black (>=25.1.0) ; extra == "dev"
27
+ Requires-Dist: custom-json-diff (>=2.1.5)
28
+ Requires-Dist: defusedxml (>=0.7.1)
29
+ Requires-Dist: flake8 (>=7.1.2) ; extra == "dev"
30
+ Requires-Dist: lief (>=0.16.4)
31
+ Requires-Dist: oras (>=0.2.25) ; extra == "db"
32
+ Requires-Dist: orjson (>=3.10.15)
33
+ Requires-Dist: packageurl-python (>=0.16.0)
34
+ Requires-Dist: pydantic[email] (>=2.10.6)
35
+ Requires-Dist: pyinstaller (>=6.12.0) ; extra == "dev"
36
+ Requires-Dist: pylint (>=3.3.4) ; extra == "dev"
37
+ Requires-Dist: pytest (>=8.3.4) ; extra == "dev"
38
+ Requires-Dist: pytest-cov (>=6.0.0) ; extra == "dev"
39
+ Requires-Dist: rich (>=13.9.4)
30
40
  Requires-Dist: symbolic (==10.2.1)
31
- Project-URL: CI, https://github.com/AppThreat/blint/actions
32
- Project-URL: Repository, https://github.com/OWASP-dep-scan/blint
41
+ Project-URL: CI, https://github.com/owasp-dep-scan/blint/actions
33
42
  Description-Content-Type: text/markdown
34
43
 
35
44
  # BLint
@@ -76,28 +85,28 @@ You can download single binary builds from the [blint-bin releases](https://gith
76
85
  ## Usage
77
86
 
78
87
  ```shell
79
- usage: blint [-h] [-i SRC_DIR_IMAGE [SRC_DIR_IMAGE ...]] [-o REPORTS_DIR] [--no-error] [--no-banner]
80
- [--no-reviews] [--suggest-fuzzable]
81
- {sbom} ...
88
+ usage: blint [-h] [-i SRC_DIR_IMAGE [SRC_DIR_IMAGE ...]] [-o REPORTS_DIR] [--no-error] [--no-banner] [--no-reviews] [--suggest-fuzzable] [--use-blintdb] {sbom} ...
82
89
 
83
90
  Binary linter and SBOM generator.
84
91
 
85
92
  options:
86
93
  -h, --help show this help message and exit
87
- -i SRC_DIR_IMAGE [SRC_DIR_IMAGE ...], --src SRC_DIR_IMAGE [SRC_DIR_IMAGE ...]
94
+ -i, --src SRC_DIR_IMAGE [SRC_DIR_IMAGE ...]
88
95
  Source directories, container images or binary files. Defaults to current directory.
89
- -o REPORTS_DIR, --reports REPORTS_DIR
96
+ -o, --reports REPORTS_DIR
90
97
  Reports directory. Defaults to reports.
91
98
  --no-error Continue on error to prevent build from breaking.
92
99
  --no-banner Do not display banner.
93
100
  --no-reviews Do not perform method reviews.
94
101
  --suggest-fuzzable Suggest functions and symbols for fuzzing based on a dictionary.
102
+ --use-blintdb Use blintdb for symbol resolution. Use environment variables: BLINTDB_IMAGE_URL, BLINTDB_HOME, and BLINTDB_REFRESH for customization.
95
103
 
96
104
  sub-commands:
97
105
  Additional sub-commands
98
106
 
99
107
  {sbom}
100
108
  sbom Command to generate SBOM for supported binaries.
109
+ db Command to manage the pre-compiled database.
101
110
  ```
102
111
 
103
112
  ### SBOM sub-command
@@ -120,6 +129,18 @@ options:
120
129
  Directories containing pre-build and build BOMs. Use to improve the precision.
121
130
  ```
122
131
 
132
+ ### DB sub-command
133
+
134
+ ```shell
135
+ usage: blint db [-h] [--download] [--image-url IMAGE_URL]
136
+
137
+ options:
138
+ -h, --help show this help message and exit
139
+ --download Download the pre-compiled database to the /Volumes/Work/blintdb/ directory. Use the environment variable `BLINTDB_HOME` to override.
140
+ --image-url IMAGE_URL
141
+ Blintdb image url. Defaults to ghcr.io/appthreat/blintdb-vcpkg-arm64:v1. The environment variable `BLINTDB_IMAGE_URL` is an alternative way to set this value.
142
+ ```
143
+
123
144
  To test any binary, including default commands
124
145
 
125
146
  ```bash
@@ -154,12 +175,20 @@ To parse all files, including `.dex` files, pass `--deep` argument.
154
175
  blint sbom -i /path/to/apk -o bom.json --deep
155
176
  ```
156
177
 
178
+ Component identification for C/C++ binaries could be improved with [blintdb](https://github.com/AppThreat/blint-db). To download the pre-compiled database (SQLite format), first run the `db` command followed by the `sbom` command.
179
+
180
+ ```shell
181
+ blint db
182
+ blint sbom -i /path/to/binary -o bom.json --deep
183
+ ```
184
+
157
185
  The following binaries are supported:
158
186
 
159
187
  - Android (apk/aab)
160
188
  - Dotnet executable binaries
161
189
  - Go binaries
162
190
  - Rust binaries
191
+ - c/c++ binaries (WIP)
163
192
 
164
193
  ```shell
165
194
  blint sbom -i /path/to/go-binaries -o bom.json --deep
@@ -42,28 +42,28 @@ You can download single binary builds from the [blint-bin releases](https://gith
42
42
  ## Usage
43
43
 
44
44
  ```shell
45
- usage: blint [-h] [-i SRC_DIR_IMAGE [SRC_DIR_IMAGE ...]] [-o REPORTS_DIR] [--no-error] [--no-banner]
46
- [--no-reviews] [--suggest-fuzzable]
47
- {sbom} ...
45
+ usage: blint [-h] [-i SRC_DIR_IMAGE [SRC_DIR_IMAGE ...]] [-o REPORTS_DIR] [--no-error] [--no-banner] [--no-reviews] [--suggest-fuzzable] [--use-blintdb] {sbom} ...
48
46
 
49
47
  Binary linter and SBOM generator.
50
48
 
51
49
  options:
52
50
  -h, --help show this help message and exit
53
- -i SRC_DIR_IMAGE [SRC_DIR_IMAGE ...], --src SRC_DIR_IMAGE [SRC_DIR_IMAGE ...]
51
+ -i, --src SRC_DIR_IMAGE [SRC_DIR_IMAGE ...]
54
52
  Source directories, container images or binary files. Defaults to current directory.
55
- -o REPORTS_DIR, --reports REPORTS_DIR
53
+ -o, --reports REPORTS_DIR
56
54
  Reports directory. Defaults to reports.
57
55
  --no-error Continue on error to prevent build from breaking.
58
56
  --no-banner Do not display banner.
59
57
  --no-reviews Do not perform method reviews.
60
58
  --suggest-fuzzable Suggest functions and symbols for fuzzing based on a dictionary.
59
+ --use-blintdb Use blintdb for symbol resolution. Use environment variables: BLINTDB_IMAGE_URL, BLINTDB_HOME, and BLINTDB_REFRESH for customization.
61
60
 
62
61
  sub-commands:
63
62
  Additional sub-commands
64
63
 
65
64
  {sbom}
66
65
  sbom Command to generate SBOM for supported binaries.
66
+ db Command to manage the pre-compiled database.
67
67
  ```
68
68
 
69
69
  ### SBOM sub-command
@@ -86,6 +86,18 @@ options:
86
86
  Directories containing pre-build and build BOMs. Use to improve the precision.
87
87
  ```
88
88
 
89
+ ### DB sub-command
90
+
91
+ ```shell
92
+ usage: blint db [-h] [--download] [--image-url IMAGE_URL]
93
+
94
+ options:
95
+ -h, --help show this help message and exit
96
+ --download Download the pre-compiled database to the /Volumes/Work/blintdb/ directory. Use the environment variable `BLINTDB_HOME` to override.
97
+ --image-url IMAGE_URL
98
+ Blintdb image url. Defaults to ghcr.io/appthreat/blintdb-vcpkg-arm64:v1. The environment variable `BLINTDB_IMAGE_URL` is an alternative way to set this value.
99
+ ```
100
+
89
101
  To test any binary, including default commands
90
102
 
91
103
  ```bash
@@ -120,12 +132,20 @@ To parse all files, including `.dex` files, pass `--deep` argument.
120
132
  blint sbom -i /path/to/apk -o bom.json --deep
121
133
  ```
122
134
 
135
+ Component identification for C/C++ binaries could be improved with [blintdb](https://github.com/AppThreat/blint-db). To download the pre-compiled database (SQLite format), first run the `db` command followed by the `sbom` command.
136
+
137
+ ```shell
138
+ blint db
139
+ blint sbom -i /path/to/binary -o bom.json --deep
140
+ ```
141
+
123
142
  The following binaries are supported:
124
143
 
125
144
  - Android (apk/aab)
126
145
  - Dotnet executable binaries
127
146
  - Go binaries
128
147
  - Rust binaries
148
+ - c/c++ binaries (WIP)
129
149
 
130
150
  ```shell
131
151
  blint sbom -i /path/to/go-binaries -o bom.json --deep
@@ -2,14 +2,12 @@
2
2
  # -*- coding: utf-8 -*-
3
3
 
4
4
  import argparse
5
- import logging
6
5
  import os
7
- import sys
8
6
 
9
- from blint.analysis import AnalysisRunner, report
7
+ from blint.lib.runners import run_default_mode, run_sbom_mode
8
+ from blint.config import BlintOptions, BLINTDB_HOME, BLINTDB_IMAGE_URL, BLINTDB_LOC
9
+ from blint.lib.utils import blintdb_setup
10
10
  from blint.logger import LOG
11
- from blint.sbom import generate
12
- from blint.utils import gen_file_list
13
11
 
14
12
  BLINT_LOGO = """
15
13
  ██████╗ ██╗ ██╗███╗ ██╗████████╗
@@ -25,10 +23,25 @@ def build_args():
25
23
  """
26
24
  Constructs command line arguments for the blint tool
27
25
  """
26
+ parser = build_parser()
27
+ return parser.parse_args()
28
+
29
+
30
+ def build_parser():
28
31
  parser = argparse.ArgumentParser(
29
32
  prog="blint",
30
33
  description="Binary linter and SBOM generator.",
31
34
  )
35
+ parser.set_defaults(
36
+ deep_mode=False,
37
+ sbom_output="",
38
+ stdout_mode=False,
39
+ exports_prefix=[],
40
+ src_dir_boms=[],
41
+ sbom_mode=False,
42
+ db_mode=False,
43
+ quiet_mode=False,
44
+ )
32
45
  parser.add_argument(
33
46
  "-i",
34
47
  "--src",
@@ -73,6 +86,21 @@ def build_args():
73
86
  dest="suggest_fuzzable",
74
87
  help="Suggest functions and symbols for fuzzing based on a dictionary.",
75
88
  )
89
+ parser.add_argument(
90
+ "--use-blintdb",
91
+ action="store_true",
92
+ default=os.path.exists(BLINTDB_LOC),
93
+ dest="use_blintdb",
94
+ help=f"Use blintdb for symbol resolution. Defaults to true if the file exists at {BLINTDB_LOC}. Use environment variables: BLINTDB_IMAGE_URL, BLINTDB_HOME, and BLINTDB_REFRESH for customization.",
95
+ )
96
+ parser.add_argument(
97
+ "-q",
98
+ "--quiet",
99
+ action="store_true",
100
+ default=False,
101
+ dest="quiet_mode",
102
+ help="Disable logging and progress bars.",
103
+ )
76
104
  # sbom commmand
77
105
  subparsers = parser.add_subparsers(
78
106
  title="sub-commands",
@@ -82,14 +110,14 @@ def build_args():
82
110
  sbom_parser = subparsers.add_parser(
83
111
  "sbom", help="Command to generate SBOM for supported binaries."
84
112
  )
113
+ sbom_parser.set_defaults(sbom_mode=True)
85
114
  sbom_parser.add_argument(
86
115
  "-i",
87
116
  "--src",
88
117
  dest="src_dir_image",
89
118
  action="extend",
90
119
  nargs="+",
91
- help="Source directories, container images or binary files. Defaults "
92
- "to current directory.",
120
+ help="Source directories, container images or binary files. Defaults to current directory.",
93
121
  )
94
122
  sbom_parser.add_argument(
95
123
  "-o",
@@ -112,6 +140,14 @@ def build_args():
112
140
  dest="stdout_mode",
113
141
  help="Print the SBOM to stdout instead of a file.",
114
142
  )
143
+ sbom_parser.add_argument(
144
+ "-q",
145
+ "--quiet",
146
+ action="store_true",
147
+ default=False,
148
+ dest="quiet_mode",
149
+ help="Disable logging and progress bars.",
150
+ )
115
151
  sbom_parser.add_argument(
116
152
  "--exports-prefix",
117
153
  default=[],
@@ -127,7 +163,33 @@ def build_args():
127
163
  nargs="+",
128
164
  help="Directories containing pre-build and build BOMs. Use to improve the precision.",
129
165
  )
130
- return parser.parse_args()
166
+ db_parser = subparsers.add_parser(
167
+ "db", help="Command to manage the pre-compiled database."
168
+ )
169
+ db_parser.set_defaults(db_mode=True)
170
+ db_parser.add_argument(
171
+ "--download",
172
+ action="store_true",
173
+ default=True,
174
+ dest="download_mode",
175
+ help=f"Download the pre-compiled database to the {BLINTDB_HOME} directory. Use the environment variable `BLINTDB_HOME` to override.",
176
+ )
177
+ db_parser.add_argument(
178
+ "--image-url",
179
+ dest="image_url",
180
+ choices=["ghcr.io/appthreat/blintdb-vcpkg:v1",
181
+ "ghcr.io/appthreat/blintdb-vcpkg-arm64:v1",
182
+ "ghcr.io/appthreat/blintdb-vcpkg-darwin-arm64:v1",
183
+ "ghcr.io/appthreat/blintdb-vcpkg-musl:v1",
184
+ "ghcr.io/appthreat/blintdb-meson:v1",
185
+ "ghcr.io/appthreat/blintdb-meson-arm64:v1",
186
+ "ghcr.io/appthreat/blintdb-meson-darwin-arm64:v1",
187
+ "ghcr.io/appthreat/blintdb-meson-musl:v1",
188
+ ],
189
+ default=BLINTDB_IMAGE_URL,
190
+ help=f"Blintdb image url. Defaults to {BLINTDB_IMAGE_URL}. The environment variable `BLINTDB_IMAGE_URL` is an alternative way to set this value.",
191
+ )
192
+ return parser
131
193
 
132
194
 
133
195
  def parse_input(src):
@@ -153,68 +215,49 @@ def parse_input(src):
153
215
  def handle_args():
154
216
  """Handles the command-line arguments.
155
217
 
156
- This function parses the command-line arguments and returns the parsed
157
- arguments, reports directory, and source directory.
218
+ This function parses the command-line arguments and returns a BlintOptions object
158
219
 
159
220
  Returns:
160
- tuple: A tuple containing the parsed arguments, reports directory, and
161
- source directory.
221
+ BlintOptions: A class containing the parsed command-line arguments
162
222
  """
163
223
  args = build_args()
164
224
  if not args.no_banner and args.subcommand_name != "sbom":
165
225
  print(BLINT_LOGO)
166
- if not args.src_dir_image:
167
- args.src_dir_image = [os.getcwd()]
168
- if not os.getenv("CI"):
169
- src_dirs = args.src_dir_image
170
- else:
171
- src_dirs = parse_input(args.src_dir_image)
172
-
173
- # Create reports directory
174
- reports_dir = args.reports_dir
175
-
176
- for src in src_dirs:
177
- if not os.path.exists(src):
178
- LOG.error(f"{src} is an invalid file or directory!")
179
- sys.exit(1)
180
- return args, reports_dir, src_dirs
226
+ blint_options = BlintOptions(
227
+ deep_mode=args.deep_mode,
228
+ exports_prefix=args.exports_prefix,
229
+ fuzzy=args.suggest_fuzzable,
230
+ no_error=args.noerror,
231
+ no_reviews=args.no_reviews,
232
+ reports_dir=args.reports_dir,
233
+ sbom_mode=args.sbom_mode,
234
+ quiet_mode=args.quiet_mode,
235
+ db_mode=args.db_mode,
236
+ image_url=args.image_url if args.db_mode else None,
237
+ sbom_output=args.sbom_output,
238
+ src_dir_boms=args.src_dir_boms,
239
+ src_dir_image=args.src_dir_image,
240
+ stdout_mode=args.stdout_mode,
241
+ use_blintdb=args.use_blintdb
242
+ )
243
+ return blint_options
181
244
 
182
245
 
183
246
  def main():
184
247
  """Main function of the blint tool"""
185
- args, reports_dir, src_dirs = handle_args()
248
+ blint_options = handle_args()
249
+ if blint_options.quiet_mode:
250
+ LOG.disabled = True
251
+ blintdb_setup(blint_options)
186
252
 
187
253
  # SBOM command
188
- if args.subcommand_name == "sbom":
189
- if args.stdout_mode:
190
- sbom_output = sys.stdout
191
- LOG.setLevel(logging.ERROR)
192
- else:
193
- if args.sbom_output:
194
- sbom_output = args.sbom_output
195
- if os.path.isdir(sbom_output):
196
- sbom_output = os.path.join(sbom_output, "bom-post-build.cdx.json")
197
- else:
198
- sbom_output = os.path.join(os.getcwd(), "bom-post-build.cdx.json")
199
- sbom_output_dir = os.path.dirname(sbom_output)
200
- if sbom_output_dir and not os.path.exists(sbom_output_dir):
201
- os.makedirs(sbom_output_dir)
202
- generate(src_dirs, sbom_output, args.deep_mode, args.exports_prefix, args.src_dir_boms)
254
+ if blint_options.sbom_mode:
255
+ run_sbom_mode(blint_options)
256
+ elif blint_options.db_mode:
257
+ return
203
258
  # Default case
204
259
  else:
205
- if reports_dir and not os.path.exists(reports_dir):
206
- os.makedirs(reports_dir)
207
- files = gen_file_list(src_dirs)
208
- analyzer = AnalysisRunner()
209
- findings, reviews, fuzzables = analyzer.start(
210
- files, reports_dir, args.no_reviews, args.suggest_fuzzable
211
- )
212
- report(src_dirs, reports_dir, findings, reviews, files, fuzzables)
213
-
214
- if os.getenv("CI") and not args.noerror:
215
- for f in findings:
216
- if f['severity'] == 'critical':
217
- sys.exit(1)
260
+ run_default_mode(blint_options)
218
261
 
219
262
 
220
263
  if __name__ == "__main__":
@@ -1,6 +1,14 @@
1
1
  # pylint: disable=too-many-lines
2
+ import sys
3
+ from dataclasses import dataclass, field
2
4
  import os
5
+ import platform
3
6
  import re
7
+ from typing import List
8
+ from appdirs import user_data_dir
9
+
10
+ ARCH = platform.machine()
11
+ SYSTEM = platform.system().lower()
4
12
 
5
13
  # Default ignore list
6
14
  ignore_directories = [
@@ -1268,6 +1276,63 @@ def get_int_from_env(name, default):
1268
1276
  return int(get_float_from_env(name, default))
1269
1277
 
1270
1278
 
1279
+ @dataclass
1280
+ class BlintOptions:
1281
+ """
1282
+ A data class representing the command-line options for the blint tool.
1283
+
1284
+ Attributes:
1285
+ deep_mode (bool): Flag indicating whether to perform deep analysis.
1286
+ exports_prefix (list): Prefixes to determine exported symbols.
1287
+ fuzzy (bool): Flag indicating whether to perform fuzzy analysis.
1288
+ no_error (bool): Flag indicating whether to suppress error messages.
1289
+ no_reviews (bool): Flag indicating whether to perform symbol reviews.
1290
+ reports_dir (str): The path to the reports directory.
1291
+ sbom_mode (bool): Flag for the sbom sub-command.
1292
+ quiet_mode (bool): Flag to disable all logging and progress bars.
1293
+ db_mode (bool): Flag for the db sub-command.
1294
+ image_url (str): blintdb download url. Must be OCI compatible.
1295
+ src_dir_image (list): A list of source directories.
1296
+ sbom_output (str): The path to the output file.
1297
+ deep_mode (bool): Flag indicating whether to perform deep analysis.
1298
+ src_dir_boms (list): Directory containing pre-build and build sboms.
1299
+ use_blintdb (bool): Flag indicating whether or not to utilize blint-db
1300
+ """
1301
+ deep_mode: bool = False
1302
+ exports_prefix: List = field(default_factory=list)
1303
+ fuzzy: bool = False
1304
+ no_error: bool = False
1305
+ no_reviews: bool = False
1306
+ reports_dir: str = ""
1307
+ sbom_mode: bool = False
1308
+ quiet_mode: bool = False
1309
+ db_mode: bool = False
1310
+ image_url: str = ""
1311
+ sbom_output: str = ""
1312
+ sbom_output_dir: str = ""
1313
+ src_dir_boms: List = field(default_factory=list)
1314
+ src_dir_image: List = field(default_factory=list)
1315
+ stdout_mode: bool = False
1316
+ use_blintdb: bool = False
1317
+
1318
+ def __post_init__(self):
1319
+ if not self.src_dir_image and not (self.sbom_mode and self.src_dir_boms):
1320
+ self.sources = [os.getcwd()]
1321
+ if not self.reports_dir:
1322
+ self.reports_dir = os.getcwd()
1323
+ if self.sbom_mode:
1324
+ if self.stdout_mode:
1325
+ self.sbom_output = sys.stdout
1326
+ elif not self.sbom_output:
1327
+ self.sbom_output = os.path.join(self.reports_dir, "bom-post-build.cdx.json")
1328
+ self.sbom_output_dir = os.path.join(self.reports_dir)
1329
+ elif os.path.isdir(self.sbom_output):
1330
+ self.sbom_output_dir = self.sbom_output
1331
+ self.sbom_output = os.path.join(self.sbom_output, "bom-post-build.cdx.json")
1332
+ else:
1333
+ self.sbom_output_dir = os.path.dirname(self.sbom_output)
1334
+
1335
+
1271
1336
  # PII related symbols
1272
1337
  PII_WORDS = (
1273
1338
  "FirstName",
@@ -1338,3 +1403,16 @@ FIRST_STAGE_WORDS = (
1338
1403
  "shlwapi.dll",
1339
1404
  "DeleteCriticalSection"
1340
1405
  )
1406
+
1407
+ # Setting blintdb
1408
+ BLINTDB_HOME = os.getenv("BLINTDB_HOME", user_data_dir("blintdb"))
1409
+ BLINTDB_LOC = os.path.join(BLINTDB_HOME, "blint.db")
1410
+
1411
+ BLINTDB_IMAGE_URL = os.getenv("BLINTDB_IMAGE_URL",
1412
+ "ghcr.io/appthreat/blintdb-vcpkg-darwin-arm64:v1" if SYSTEM == "darwin" else "ghcr.io/appthreat/blintdb-vcpkg:v1")
1413
+ BLINTDB_REFRESH = os.getenv("BLINTDB_REFRESH", False)
1414
+ if BLINTDB_REFRESH in ["true", "True", "1"]:
1415
+ BLINTDB_REFRESH = True
1416
+
1417
+ SYMBOLS_LOOKUP_BATCH_LEN = get_int_from_env("SYMBOLS_LOOKUP_BATCH_LEN", 32000)
1418
+ MIN_MATCH_SCORE = get_int_from_env("MIN_MATCH_SCORE", 10)
@@ -0,0 +1,150 @@
1
+ # SPDX-FileCopyrightText: AppThreat <cloud@appthreat.com>
2
+ #
3
+ # SPDX-License-Identifier: MIT
4
+ import concurrent
5
+ import concurrent.futures
6
+ from functools import lru_cache
7
+ import os
8
+ import sys
9
+ from contextlib import closing
10
+
11
+ import apsw
12
+
13
+ from blint.config import BLINTDB_LOC, MIN_MATCH_SCORE, SYMBOLS_LOOKUP_BATCH_LEN
14
+ from blint.logger import LOG
15
+
16
+ DEBUG_MODE = os.getenv("SCAN_DEBUG_MODE") == "debug"
17
+ db_conn: apsw.Connection = None
18
+ DB_FILE_SEP = "///" if sys.platform == "win32" else "//"
19
+
20
+
21
+ def get(db_file: str = BLINTDB_LOC, read_only=True) -> apsw.Connection:
22
+ if not os.path.exists(db_file):
23
+ return
24
+ if not db_file.startswith("file:") and db_file != ":memory:":
25
+ db_file = f"file:{DB_FILE_SEP}{os.path.abspath(db_file)}"
26
+ global db_conn
27
+ ro_flags = apsw.SQLITE_OPEN_URI | apsw.SQLITE_OPEN_NOFOLLOW | apsw.SQLITE_OPEN_READONLY
28
+ db_conn = apsw.Connection(db_file, flags=ro_flags)
29
+ return db_conn
30
+
31
+
32
+ def return_batch_binaries_detected(symbols_list):
33
+ """
34
+ Current scoring algorithm along with batching
35
+ """
36
+ # Detected binaries and their score
37
+ binaries_detected_dict = {}
38
+ # The export ids that led to the match
39
+ binaries_eid_dict = {}
40
+ if not isinstance(symbols_list, list):
41
+ raise TypeError(f"Incorrect type symbols_lists should be List not {type(symbols_list)}")
42
+
43
+ # Errors not being caught here
44
+ output_list = get_bid_using_ename_batch(symbols_list)
45
+
46
+ eid_list = [it[0] for it in output_list]
47
+ bid_2d_list = [it[1] for it in output_list]
48
+ for it in range(len(eid_list)):
49
+ bid_list = bid_2d_list[it].split(",")
50
+ score = 1 / len(bid_list)
51
+ for bid in bid_list:
52
+ matching_binaries = find_binary_from_db(bid)
53
+ if matching_binaries:
54
+ for (bname, pname, purl) in matching_binaries:
55
+ binaries_eid_dict[purl] = eid_list
56
+ if purl in binaries_detected_dict:
57
+ binaries_detected_dict[purl] += score
58
+ else:
59
+ binaries_detected_dict[purl] = score
60
+ return binaries_detected_dict, binaries_eid_dict
61
+
62
+
63
+ def get_bid_using_ename_batch(batch_export_name):
64
+ """Retrieves binary IDs associated with a batch of export names from a SQLite database.
65
+
66
+ This function queries the database to find binary IDs linked to specific export names in a batch.
67
+ It uses the BLINTDB_LOC environment variable to connect to the SQLite database and performs a grouped query.
68
+
69
+ Args:
70
+ batch_export_name (list): A list of export names to search for in the database.
71
+
72
+ Returns:
73
+ list: A list of tuples containing export IDs and their corresponding concatenated binary IDs.
74
+
75
+ Raises:
76
+ apsw.Error: If there are any database connection or query execution issues.
77
+ """
78
+ connection = get()
79
+ if not connection:
80
+ return None
81
+ with closing(connection.cursor()) as c:
82
+ place_holders = "?, " * (len(batch_export_name) - 1) + "?"
83
+ if len(batch_export_name) > 0:
84
+ output_string = f"SELECT eid, group_concat(bid) from BinariesExports where eid IN (SELECT rowid from Exports where infunc IN ({place_holders})) group by eid"
85
+ c.execute(output_string, batch_export_name)
86
+ res = c.fetchall()
87
+ return res
88
+
89
+
90
+ @lru_cache(maxsize=1000)
91
+ def find_binary_from_db(bid):
92
+ """Retrieves the binary name for a given binary ID from a SQLite database. The function performs a lookup to fetch the corresponding binary name.
93
+
94
+ This function connects to a SQLite database using the BLINTDB_LOC environment variable and executes a query to retrieve the binary name based on the provided binary ID. If no matching binary is found, it returns None.
95
+
96
+ Args:
97
+ bid (int): The binary identifier to search for in the database.
98
+
99
+ Returns:
100
+ str or None: The name of the binary if found, otherwise None.
101
+
102
+ Raises:
103
+ apsw.Error: If there are any database connection or query execution issues.
104
+ """
105
+ connection = get()
106
+ if not connection:
107
+ return None
108
+ res = connection.execute(
109
+ "SELECT bname, pname, purl from Binaries JOIN Projects on Binaries.pid = Projects.pid WHERE Binaries.bid = ?",
110
+ (bid,))
111
+ return frozenset(res.fetchall())
112
+
113
+
114
+ def detect_binaries_utilized(symbols_list) -> tuple[set, dict]:
115
+ """Simple Voting algorithm
116
+ for a given symbols. e.g. XRenderAddGlyphs
117
+ we count the number of binaries associated to this function
118
+ e.g. which is one in this example XRenderAddGlyphs
119
+ is associated with 'libXrender-0.9.10/libxrender.so'.
120
+ so one is added to score, we want all the detections to have a score greater than MIN_MATCH_SCORE.
121
+ """
122
+ if not symbols_list:
123
+ return set(), {}
124
+ bin_detected_dict = {}
125
+ binaries_eid_dict = {}
126
+ eid_list = [symbol["name"] for symbol in symbols_list]
127
+ LOG.debug(f"Attempting to find the binaries for {len(eid_list)} symbols.")
128
+ # creates a 2D array with SYMBOLS_LOOKUP_BATCH_LEN, SYMBOLS_LOOKUP_BATCH_LEN eids are processed in a single query
129
+ eid_2d_list = [eid_list[i: i + SYMBOLS_LOOKUP_BATCH_LEN] for i in range(0, len(eid_list), SYMBOLS_LOOKUP_BATCH_LEN)]
130
+
131
+ # for eid in eid_list:
132
+ with concurrent.futures.ProcessPoolExecutor() as executor:
133
+ futures_bin_detected = {
134
+ executor.submit(return_batch_binaries_detected, it_eid_list): it_eid_list
135
+ for it_eid_list in eid_2d_list
136
+ }
137
+ for future in concurrent.futures.as_completed(futures_bin_detected):
138
+ single_binaries_detected_dict, binaries_eid_dict = future.result()
139
+ for purl, score in single_binaries_detected_dict.items():
140
+ if purl in bin_detected_dict:
141
+ bin_detected_dict[purl] += score
142
+ else:
143
+ bin_detected_dict[purl] = score
144
+ # create a set() and remove false positives
145
+ binary_detected = {purl for purl, score in bin_detected_dict.items() if score > MIN_MATCH_SCORE}
146
+ binary_evidence_eids = {}
147
+ for purl, eids in binaries_eid_dict.items():
148
+ if purl in binary_detected:
149
+ binary_evidence_eids[purl] = eids
150
+ return binary_detected, binary_evidence_eids