python-fire icon indicating copy to clipboard operation
python-fire copied to clipboard

Accessing Available Indexes

Open joshswimlane opened this issue 5 years ago • 2 comments

I have a project pyattck that is utilizing fire for it's command line interface and I am returning a list of objects.

For example, when using pyattck you can access all techniques from the Enterprise MITRE ATT&CK Framework like so:

from pyattck import Attck

attack = Attck()

for technique in attack.enterprise.techniques:
   print(technique.id)
   print(technique.name)
   # etc.

When using fire, I can access the same data using:

pyattck enterprise techniques 4 id
pyattck enterprise techniques 4 name
# etc.

My question is, how can I specify a range or to print out ALL technique id or name or etc properties using fire?

Ideally I would want to access the INDEXES range by either specifying the range of 0..265 or having a foreach clause.

Is this possible or is there another way of handling this?

Here is an example of calling it without specifying the range number of 1 or 4 or 164 etc.

josh.rickard@Joshs-MacBook-Pro ~ % pyattck enterprise techniques name
ERROR: Unable to index into component with argument: name
Usage: pyattck enterprise techniques <command|index>
  available commands:    append | clear | copy | count | extend | index |
                         insert | pop | remove | reverse | sort
  available indexes:     0..265

For detailed information on this command, run:
  pyattck enterprise techniques --help

tldr: How do you access the indexes or how do you properly deal with indexes in fire?

joshswimlane avatar May 07 '20 19:05 joshswimlane

Python Fire doesn't provide a way to access an attribute of every element of a list at the command line. I'll consider this a feature request.

Here are two alternatives you can consider:

1. Use interactive mode pyattck enterprise techniques -- -i

> for t in component:
>   print(t.name)

The interactive flag will put you in an IPython REPL with component equal to the techniques list, at which point you can query it however you like using Python.

2. Add a __str__ method to Technique

class Technique:
  ...  # add __str__ to the existing class definition
  def __str__(self):
    return self.name

pyattck enterprise techniques This should print the list of techniques, one per line, using the __str__ method you defined (in this example, that would print all the technique names.)


Do either of those satisfy your use case?

dbieber avatar May 07 '20 21:05 dbieber

Sorry, for some reason I lost or didn't hit comment on my response. Again my apologies.

I understand that you can access this via interactive. I have both a repr and str methods implemented but the output is an extremely large json string for each object (if I had to guess 500+ objects). I was hoping that a feature could be implemented to access or | or iterate over each object in a list. An example of functionality that makes sense to me is

pyattck enterprise techniques 0..266 name

This would print each name property of the array of objects in range of 0 to 266. Again sorry for the delay in response.

joshswimlane avatar Oct 13 '20 03:10 joshswimlane