Skip to content

Commit c9759cd

Browse files
authored
Merge pull request #34 from CybercentreCanada/feature_change_cli_change_to_no_flag_functionality
First Pass of changes
2 parents 044412d + e2d12ad commit c9759cd

File tree

3 files changed

+56
-65
lines changed

3 files changed

+56
-65
lines changed

README.md

+42-52
Original file line numberDiff line numberDiff line change
@@ -79,39 +79,34 @@ yara_validator_cli.py -h
7979
| |__| |__| |___ ___) | | |/ ___ \| _ < / ___ \
8080
\____\____\____|____/ |_/_/ \_\_| \_\/_/ \_\
8181
82-
usage: yara_validator_cli.py [-h] [-r] [-n] [-v] [-vv] [-f] [-w] [-s] [-g]
82+
usage: yara_validator_cli.py [-h] [-r] [-n] [-v] [-vv] [-f] [-w] [-s]
8383
[-i | -c]
8484
paths [paths ...]
8585
86-
CCCS YARA script to run the CCCS YARA validator, if the -i or -c flags are not
87-
provided no changes will be made to the files. The default behavior without
88-
either of the -i or -c flags is to return the validity of the file or files if
89-
the -i or -c flag had been used. Use the -g flag to check the current validity
90-
of the file or files.
86+
CCCS YARA script to run the CCCS YARA validator, use the -i or -c flags to
87+
generate the id, fingerprint, version, first_imported, or last_modified (if
88+
not already present) and add them to the file.
9189
9290
positional arguments:
93-
paths A list of files or folders to be analyzed.
91+
paths A list of files or folders to be analyzed.
9492
9593
optional arguments:
96-
-h, --help show this help message and exit
97-
-r, --recursive Recursively search folders provided.
98-
-n, --no-changes Makes no changes and outputs potential results to the
99-
output.
100-
-v, --verbose Verbose mode, will print why a rule was invalid.
101-
-vv, --very-verbose Very-verbose mode, will printout what rule is about to
102-
be processed, the invalid rules, the reasons they are
103-
invalid and all contents of the rule.
104-
-f, --fail Fail mode, only prints messages about invalid rules.
105-
-w, --warnings This mode will ignore warnings and proceed with other
106-
behaviors if the rule is valid.
107-
-s, --standard This prints the YARA standard to the screen.
108-
-g, --generate-values
109-
Generate-values, this is true by default use this flag
110-
to prevent values from being generated.
111-
-i, --in-place Modifies valid files in place, mutually exclusive with
112-
-c.
113-
-c, --create-files Writes a new file for each valid file, mutually
114-
exclusive with -i.
94+
-h, --help show this help message and exit
95+
-r, --recursive Recursively search folders provided.
96+
-n, --no-changes Makes no changes and outputs potential results to the
97+
output.
98+
-v, --verbose Verbose mode, will print why a rule was invalid.
99+
-vv, --very-verbose Very-verbose mode, will printout what rule is about to
100+
be processed, the invalid rules, the reasons they are
101+
invalid and all contents of the rule.
102+
-f, --fail Fail mode, only prints messages about invalid rules.
103+
-w, --warnings This mode will ignore warnings and proceed with other
104+
behaviors if the rule is valid.
105+
-s, --standard This prints the YARA standard to the screen.
106+
-i, --in-place Modifies valid files in place, mutually exclusive with
107+
-c.
108+
-c, --create-files Writes a new file for each valid file, mutually
109+
exclusive with -i.
115110
```
116111

117112

@@ -197,38 +192,33 @@ yara_validator_cli.py -h
197192
| |__| |__| |___ ___) | | |/ ___ \| _ < / ___ \
198193
\____\____\____|____/ |_/_/ \_\_| \_\/_/ \_\
199194
200-
usage: yara_validator_cli.py [-h] [-r] [-n] [-v] [-vv] [-f] [-w] [-s] [-g]
195+
usage: yara_validator_cli.py [-h] [-r] [-n] [-v] [-vv] [-f] [-w] [-s]
201196
[-i | -c]
202197
paths [paths ...]
203198
204-
CCCS YARA script to run the CCCS YARA validator, if the -i or -c flags are not
205-
provided no changes will be made to the files. The default behavior without
206-
either of the -i or -c flags is to return the validity of the file or files if
207-
the -i or -c flag had been used. Use the -g flag to check the current validity
208-
of the file or files.
199+
CCCS YARA script to run the CCCS YARA validator, use the -i or -c flags to
200+
generate the id, fingerprint, version, first_imported, or last_modified (if
201+
not already present) and add them to the file.
209202
210203
positional arguments:
211-
paths A list of files or folders to be analyzed.
204+
paths A list of files or folders to be analyzed.
212205
213206
optional arguments:
214-
-h, --help show this help message and exit
215-
-r, --recursive Recursively search folders provided.
216-
-n, --no-changes Makes no changes and outputs potential results to the
217-
output.
218-
-v, --verbose Verbose mode, will print why a rule was invalid.
219-
-vv, --very-verbose Very-verbose mode, will printout what rule is about to
220-
be processed, the invalid rules, the reasons they are
221-
invalid and all contents of the rule.
222-
-f, --fail Fail mode, only prints messages about invalid rules.
223-
-w, --warnings This mode will ignore warnings and proceed with other
224-
behaviors if the rule is valid.
225-
-s, --standard This prints the YARA standard to the screen.
226-
-g, --generate-values
227-
Generate-values, this is true by default use this flag
228-
to prevent values from being generated.
229-
-i, --in-place Modifies valid files in place, mutually exclusive with
230-
-c.
231-
-c, --create-files Writes a new file for each valid file, mutually
232-
exclusive with -i.
207+
-h, --help show this help message and exit
208+
-r, --recursive Recursively search folders provided.
209+
-n, --no-changes Makes no changes and outputs potential results to the
210+
output.
211+
-v, --verbose Verbose mode, will print why a rule was invalid.
212+
-vv, --very-verbose Very-verbose mode, will printout what rule is about to
213+
be processed, the invalid rules, the reasons they are
214+
invalid and all contents of the rule.
215+
-f, --fail Fail mode, only prints messages about invalid rules.
216+
-w, --warnings This mode will ignore warnings and proceed with other
217+
behaviors if the rule is valid.
218+
-s, --standard This prints the YARA standard to the screen.
219+
-i, --in-place Modifies valid files in place, mutually exclusive with
220+
-c.
221+
-c, --create-files Writes a new file for each valid file, mutually
222+
exclusive with -i.
233223
```
234224

yara-validator/yara_validator.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -509,7 +509,8 @@ def validation(self, rule_to_validate, rule_to_validate_string, generate_values=
509509
if value.optional == MetadataOpt.REQ_PROVIDED:
510510
valid.update_validity(False, key, 'Missing required metadata')
511511
elif value.optional == MetadataOpt.REQ_OPTIONAL:
512-
valid.update_validity(False, key, 'Missing metadata that could have been generated')
512+
valid.update_validity(False, key, '⚙️ Missing metadata that could have been generated with the -i'
513+
' or -c flag for the cli')
513514
else:
514515
if self.required_fields_index[value.position].count > value.max_count and value.max_count != -1:
515516
valid.update_validity(False, key, 'Too many instances of metadata value.')

yara_validator_cli.py

+12-12
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,9 @@
2424
# Defining the parser and arguments to parse so it be used both when called by the command line and with the git_ci
2525
# function.
2626
parser = argparse.ArgumentParser(description='CCCS YARA script to run the CCCS YARA validator, '
27-
'if the -i or -c flags are not provided no changes '
28-
'will be made to the files. '
29-
'The default behavior without either of the -i or -c flags is to return '
30-
'the validity of the file or files if the -i or -c flag had been used. '
31-
'Use the -g flag to check the current validity of the file or files.')
27+
'use the -i or -c flags to generate the id, fingerprint, version, '
28+
'first_imported, or last_modified (if not already present) and add them '
29+
'to the file.')
3230
parser.add_argument('paths', nargs='+', type=str, default=[],
3331
help='A list of files or folders to be analyzed.')
3432
parser.add_argument('-r', '--recursive', action='store_true', default=False, dest='recursive',
@@ -46,9 +44,6 @@
4644
help='This mode will ignore warnings and proceed with other behaviors if the rule is valid.')
4745
parser.add_argument('-s', '--standard', action='store_true', default=False, dest='standard',
4846
help='This prints the YARA standard to the screen.')
49-
parser.add_argument('-g', '--generate-values', action='store_false', default=True, dest='generatevalues',
50-
help='Generate-values, this is true by default use this flag to prevent values from being'
51-
' generated.')
5247

5348
parser_group = parser.add_mutually_exclusive_group()
5449
parser_group.add_argument('-i', '--in-place', action='store_true', default=False, dest='inplace',
@@ -182,17 +177,22 @@ def __call_validator(options):
182177
y_file=yara_rule_path,
183178
))
184179

185-
yara_file_processor = run_yara_validator(yara_rule_path, options.generatevalues)
186-
what_will_be_done = 'make no changes'
187-
yara_file_output = None
188-
189180
# handle if we want to overwrite or create new files
190181
if options.createfile:
182+
generate_values = True
191183
yara_file_output = get_yara_file_new_path(yara_rule_path)
192184
what_will_be_done = 'create a new file with the {} preface.'.format(YARA_VALID_PREFIX)
193185
elif options.inplace:
186+
generate_values = True
194187
yara_file_output = yara_rule_path
195188
what_will_be_done = 'modify the file in place.'
189+
else:
190+
generate_values = False
191+
what_will_be_done = 'make no changes'
192+
yara_file_output = None
193+
194+
yara_file_processor = run_yara_validator(yara_rule_path, generate_values)
195+
196196

197197
# Prints the output of the validator.
198198
file_message = '{message:39}{y_file}'

0 commit comments

Comments
 (0)