Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add nitf #294

Merged
merged 9 commits into from
Jul 6, 2020
Merged

Add nitf #294

merged 9 commits into from
Jul 6, 2020

Conversation

riverloopsec
Copy link
Contributor

@riverloopsec riverloopsec commented Jun 8, 2020

This PR adds the NITF (National Image Transition Format) format specification.

Please see attached ZIP that has a selected set of NITF images collected from around the internet and carefully selected to have good coverage for testing purposes -
corpora.zip. In addition, https://github.com/codice/imaging-nitf/tree/master/shared-test-resources/src/main/resources is another repository of these files.

We have conducted extensive analysis of the security of this format using this as one of our methods for exploring it.

Credit to @TACIXAT for implementation.

image/nitf.ksy Outdated
ks-version: 0.9
encoding: UTF-8
endian: be
license: CC-BY-SA-4.0
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not usually used for software.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated to MIT.

image/nitf.ksy Outdated
type: encrypt
- id: fbkgc
size: 3
- id: oname
Copy link
Contributor

@KOLANICH KOLANICH Jun 10, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess names should be more meaningful. For original names there is -orig-id. Could you also add doc to each?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated these for many of the items. Full descriptions in the source doc linked are long and prose, so included some shortened ones where I felt the field could use more context.

image/nitf.ksy Outdated
clasnfo:
instances:
total_size:
value: (1 + 2 + 11 + 2 + 20 + 2 + 8 + 4 + 1 + 8 + 43 + 1 + 40 + 1 + 8 + 15)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sizeof<...>

Copy link
Member

@generalmimon generalmimon Jul 3, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@riverloopsec @rmspeers As @KOLANICH points out, there's really no need for such a lengthy and error-prone summation. Exactly this problem is addressed by the sizeof operator (make sure you're using the latest unstable compiler version available from https://kaitai.io/#download, or you can use https://ide.kaitai.io/devel/, which has constantly the latest development version out of the box). But I noticed you already indicated ks-version: 0.9 in the top-level meta section, so this shouldn't be a problem.

I saw that you're using it somewhere like reclasnfo.total_size. You can replace this usage with either

Choose the one that suits you best. In both cases, you can delete this manually created attribute total_size, the size of the whole clasnfo type will be calculated automatically.

@rmspeers
Copy link
Contributor

rmspeers commented Jul 3, 2020

@KOLANICH can you look at this we ran into when updating? This is produced by KSV, no errors on KSC.

Compilation OK
... processing image/nitf.ksy 0
...... loading nitf.rb
Classes loaded OK, main class = Nitf
Traceback (most recent call last):
	8: from /usr/local/bin/ksv:23:in `<main>'
	7: from /usr/local/bin/ksv:23:in `load'
	6: from /Library/Ruby/Gems/2.6.0/gems/kaitai-struct-visualizer-0.7/bin/ksv:53:in `<top (required)>'
	5: from /Library/Ruby/Gems/2.6.0/gems/kaitai-struct-visualizer-0.7/lib/kaitai/struct/visualizer/visualizer.rb:13:in `run'
	4: from /Library/Ruby/Gems/2.6.0/gems/kaitai-struct-visualizer-0.7/lib/kaitai/struct/visualizer/parser.rb:32:in `load'
	3: from /var/folders/11/nn476p9172xb4__85r_ncw6h0000gn/T/d20200703-41609-v4w6iu/nitf.rb:28:in `_read'
	2: from /var/folders/11/nn476p9172xb4__85r_ncw6h0000gn/T/d20200703-41609-v4w6iu/nitf.rb:28:in `times'
	1: from /var/folders/11/nn476p9172xb4__85r_ncw6h0000gn/T/d20200703-41609-v4w6iu/nitf.rb:31:in `block in _read'
/var/folders/11/nn476p9172xb4__85r_ncw6h0000gn/T/d20200703-41609-v4w6iu/nitf.rb:229:in `_read': undefined method `total_size' for nil:NilClass (NoMethodError)

Note that I tried to switch ks-version to 0.8, which actually may be what broke this - rather than any changes to the format. Does that make sense?

image/nitf.ksy Outdated
seq:
- id: des_base
type: data_sub_header_base
- id: desoflw
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. should these 2 fields be separated in a struct?
  2. please don't merge words in variable names.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

desoflw is one from the original data, I'll go move these to orig-id and put instead other ids.

image/nitf.ksy Outdated
type: tre_header
band_info:
seq:
- id: n_band_representation
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should n_band_ be ommitted?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had it in there per the doc naming (pg 97 of reference), but have removed.

image/nitf.ksy Outdated
- id: image_date_time
-orig-id: idatim
type: date_time
doc: 'UTC time of image acquisition in the format CCYYMMDDhhmmss: CC century, YY last two digits of the year, MM month, DD day, hh hour, mm minute, ss second'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The format of the time string docs should be moved into the struct representing time.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved

@rmspeers
Copy link
Contributor

rmspeers commented Jul 3, 2020

FYI for a basic single image test see attached rgb16.ntf:

rgb16.ntf.zip

image/nitf.ksy Outdated
Comment on lines 263 to 267
- id: image_data_mask
type: image_data_mask
if: has_mask
- id: image_data_field
size: _parent.header.linfo[idx].li.to_i - image_data_mask.total_size
Copy link
Member

@generalmimon generalmimon Jul 3, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Parsing of the file rgb16.ntf you provided in #294 (comment) fails due to image_data_mask being null (or more specifically, undefined in JavaScript - tested on https://ide.kaitai.io/devel/), because you're unconditionally reading its total_size attribute, whether it is null or not. Shouldn't it have if: has_mask as well?

@rmspeers
Copy link
Contributor

rmspeers commented Jul 4, 2020

@generalmimon / @KOLANICH - let me know if you see any other issues to resolve still.

image/nitf.ksy Outdated Show resolved Hide resolved
image/nitf.ksy Outdated Show resolved Hide resolved
image/nitf.ksy Outdated Show resolved Hide resolved
image/nitf.ksy Outdated Show resolved Hide resolved
image/nitf.ksy Outdated Show resolved Hide resolved
image/nitf.ksy Outdated Show resolved Hide resolved
image/nitf.ksy Outdated Show resolved Hide resolved
Ryan Speers and others added 2 commits July 4, 2020 12:33
Apply suggestions from PR.

Co-authored-by: Petr Pučil <[email protected]>
image/nitf.ksy Outdated
Comment on lines 180 to 192
- id: sctlh
type: str
size: 2
- id: srel
type: str
size: 20
- id: sdctp
type: str
size: 2
- id: sdcdt
type: str
size: 8
- id: sdcxm
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see that you improved a lot of field namings in 77c94a2, but I still feel that these (and similar ones) deserve a more meaningful id.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Anything else we need to do or is this good to merge?

Comment on lines +17 to +24
doc: |
The NITF (National Image Transition Format) format is a file format developed by the U.S. Government for
storing imagery, e.g. from satellites.

According to the [foreword of the specification](https://gwg.nga.mil/ntb/baseline/docs/2500c/2500C.pdf):
> The National Imagery Transmission Format Standard (NITFS) is the suite of standards for formatting digital
> imagery and imagery-related products and exchanging them among members of the Intelligence Community (IC) as
> defined by the Executive Order 12333, and other United States Government departments and agencies."
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rmspeers A lot better! 👍

Copy link
Member

@generalmimon generalmimon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall looks good to me, you did a nice job! Just one small thing before we merge it (feel free to just apply the suggestion).

image/nitf.ksy Outdated Show resolved Hide resolved
Co-authored-by: Petr Pučil <[email protected]>
Copy link
Member

@generalmimon generalmimon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@riverloopsec @rmspeers Thanks for your huge contribution, merging in!

@generalmimon generalmimon merged commit b86f71e into kaitai-io:master Jul 6, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants