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

[Bug] OSError when generating SVG File using to_svg

Open joshfrogers opened this issue 3 years ago • 1 comments

Expected Behavior

ToSvg Generates an SVG File from a layer

Actual Behavior

OSError when generating the SVG File

Traceback (most recent call last):
  File "/home/jrogers/wksp/project/src/project_file.py", line 279, in <module>
    test = GenerateMarkDown(
  File "/home/jrogers/wksp/project/src/project_file.py", line 74, in __init__
    self.generate_attack()
  File "/home/jrogers/wksp/project/src/project_file.py", line 262, in generate_attack
    attack_data.export_layer()
  File "/home/jrogers/wksp/project/src/attack_utils.py", line 76, in export_layer
    svg.to_svg(self.layer, filepath=os.path.join(self.output_path, 'enterprise-attack.svg'))
  File "/home/jrogers/anaconda3/envs/project/lib/python3.10/site-packages/mitreattack/navlayers/exporters/to_svg.py", line 464, in to_svg
    d = self.raw_handle.export(showName=sName, showID=sID, sort=sort, scores=scores,
  File "/home/jrogers/anaconda3/envs/project/lib/python3.10/site-packages/mitreattack/navlayers/exporters/svg_templates.py", line 262, in export
    d, presence, overlay = self._build_headers(lhandle.name, config, lhandle.domain,
  File "/home/jrogers/anaconda3/envs/project/lib/python3.10/site-packages/mitreattack/navlayers/exporters/svg_templates.py", line 90, in _build_headers
    g = SVG_HeaderBlock().build(height=header_height, width=header_width, label='about',
  File "/home/jrogers/anaconda3/envs/project/lib/python3.10/site-packages/mitreattack/navlayers/exporters/svg_objects.py", line 264, in build
    rect2 = HeaderRect(_getstringwidth(label, config.font, 12), _getstringheight(label, config.font, 12),
  File "/home/jrogers/anaconda3/envs/project/lib/python3.10/site-packages/mitreattack/navlayers/exporters/svg_objects.py", line 42, in _getstringwidth
    font = ImageFont.truetype('{}/fonts/{}.ttf'.format(os.path.sep.join(__file__.split(os.path.sep)[:-1]), font),
  File "/home/jrogers/anaconda3/envs/project/lib/python3.10/site-packages/PIL/ImageFont.py", line 878, in truetype
    return freetype(font)
  File "/home/jrogers/anaconda3/envs/project/lib/python3.10/site-packages/PIL/ImageFont.py", line 875, in freetype
    return FreeTypeFont(font, size, index, encoding, layout_engine)
  File "/home/jrogers/anaconda3/envs/project/lib/python3.10/site-packages/PIL/ImageFont.py", line 226, in __init__
    self.font = core.getfont(
OSError: cannot open resource

Steps to Reproduce the Problem

Requirements:

  • Ubuntu 22.04

  • Anaconda Python

    1. Install mitreattack-python as per README
    2. Attempt to export a layer using the to_svg function
    3. Get OSError

Possible Solution

  • fonts folder appears to be missing from mitreattack/navlayers/exporters when installed using pip on an anaconda install (possibly on all installs)
  • copy fonts folder from repo to mitreattack/navlayers/exporters

joshfrogers avatar May 05 '22 16:05 joshfrogers

I encountered the same issue on MacOS 12.3.1 using pip install as specified in the README. I believe the suggested solution in the issue is correct. I cloned the repo locally and copied the fonts folder referenced in the original post over to the python install location. This fixed the issue and the SVG was exported successfully.

william-vanschaik avatar May 11 '22 15:05 william-vanschaik

I experienced the same issue on macOS 12.5.1 and the same workaround fixed. I have submitted PR #87 which includes a permanent fix.

infosecB avatar Aug 29 '22 16:08 infosecB

Thanks all for your contributions, especially @infosecB for PR #87 which I just accepted.

I just released the latest version of this library (1.6.2) to PyPI that has this addressed. I'll close the issue now, but welcome any further discussion if it acts up again.

jondricek avatar Sep 07 '22 22:09 jondricek