forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Processors] Mime-Type Detection (elastic#22940)
* Add mimetype processor * Add mimetype detection for packetbeat * Update changelog * Rev go.sum * Refactor for reusability and rename to detect_mime_type * reformat imports * update docs * Update maxHeaderSize name and add comment on the fallback behavior (cherry picked from commit 5f52979)
- Loading branch information
Andrew Stucki
committed
Dec 8, 2020
1 parent
97d94ff
commit d42fb7c
Showing
16 changed files
with
393 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10142,11 +10142,11 @@ Contents of probable licence file $GOMODCACHE/github.com/gorhill/[email protected] | |
|
||
-------------------------------------------------------------------------------- | ||
Dependency : github.com/h2non/filetype | ||
Version: v1.0.12 | ||
Version: v1.1.1-0.20201130172452-f60988ab73d5 | ||
Licence type (autodetected): MIT | ||
-------------------------------------------------------------------------------- | ||
|
||
Contents of probable licence file $GOMODCACHE/github.com/h2non/filetype@v1.0.12/LICENSE: | ||
Contents of probable licence file $GOMODCACHE/github.com/h2non/filetype@v1.1.1-0.20201130172452-f60988ab73d5/LICENSE: | ||
|
||
The MIT License | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
// Licensed to Elasticsearch B.V. under one or more contributor | ||
// license agreements. See the NOTICE file distributed with | ||
// this work for additional information regarding copyright | ||
// ownership. Elasticsearch B.V. licenses this file to you under | ||
// the Apache License, Version 2.0 (the "License"); you may | ||
// not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
package mime | ||
|
||
import ( | ||
"encoding/json" | ||
"encoding/xml" | ||
"net/http" | ||
"strings" | ||
|
||
"github.com/h2non/filetype" | ||
) | ||
|
||
const ( | ||
// size for mime detection, office file | ||
// detection requires ~8kb to detect properly | ||
maxHeaderSize = 8192 | ||
) | ||
|
||
// DetectBytes tries to detect a mime-type based off | ||
// of a chunk of bytes passed into the function | ||
func DetectBytes(data []byte) string { | ||
header := data | ||
if len(data) > maxHeaderSize { | ||
header = data[:maxHeaderSize] | ||
} | ||
kind, err := filetype.Match(header) | ||
if err == nil && kind != filetype.Unknown { | ||
// we have a known filetype, return | ||
return kind.MIME.Value | ||
} | ||
// if the above fails, try and sniff with http sniffing | ||
netType := http.DetectContentType(header) | ||
// try and parse any sort of text as json or xml | ||
if strings.HasPrefix(netType, "text/plain") { | ||
if detected := detectEncodedText(data); detected != "" { | ||
return detected | ||
} | ||
} | ||
// The fallback for http.DetectContentType is "application/octet-stream" | ||
// meaning that if we see it, we were unable to determine the type and | ||
// we just know we're dealing with a chunk of some sort of bytes. Rather | ||
// than reporting the fallback, we'll just say we were unable to detect | ||
// the type. | ||
if netType == "application/octet-stream" { | ||
return "" | ||
} | ||
return netType | ||
} | ||
|
||
func detectEncodedText(data []byte) string { | ||
// figure out how to optimize this so we don't have to try and parse the whole payload | ||
// every time | ||
if json.Valid(data) { | ||
return "application/json" | ||
} | ||
if xml.Unmarshal(data, new(interface{})) == nil { | ||
return "text/xml" | ||
} | ||
return "" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
// Licensed to Elasticsearch B.V. under one or more contributor | ||
// license agreements. See the NOTICE file distributed with | ||
// this work for additional information regarding copyright | ||
// ownership. Elasticsearch B.V. licenses this file to you under | ||
// the Apache License, Version 2.0 (the "License"); you may | ||
// not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
package mime | ||
|
||
import ( | ||
"encoding/hex" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestMimeType(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
expectedType string | ||
body string | ||
}{ | ||
{ | ||
name: "html", | ||
expectedType: "text/html; charset=utf-8", | ||
body: "<html>Test</html>", | ||
}, | ||
{ | ||
name: "pe", | ||
expectedType: "application/vnd.microsoft.portable-executable", | ||
body: convertToData(t, "4d5a90000300000004000000ffff"), | ||
}, | ||
{ | ||
name: "elf", | ||
expectedType: "application/x-executable", | ||
body: convertToData(t, "7f454c460101010000000000000000000300030001000000f0dc01003400000080a318000000000034002000080028001e001d0001"), | ||
}, | ||
{ | ||
name: "macho", | ||
expectedType: "application/x-mach-binary", | ||
body: convertToData(t, "cffaedfe0700000103000000020000001000000058050000850020000000000019000000480000005f5f504147455a45524f"), | ||
}, | ||
{ | ||
name: "json", | ||
expectedType: "application/json", | ||
body: "{}", | ||
}, | ||
{ | ||
name: "xml", | ||
expectedType: "text/xml", | ||
body: "<test></test>", | ||
}, | ||
{ | ||
name: "text", | ||
expectedType: "text/plain; charset=utf-8", | ||
body: "Hello world!", | ||
}, | ||
{ | ||
name: "png", | ||
expectedType: "image/png", | ||
body: convertToData(t, "89504e470d0a1a0a0000000d494844520000025800000258080200000031040f8b0000000467414d410000b18f0bfc610500"), | ||
}, | ||
} | ||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
require.Equal(t, test.expectedType, Detect(test.body)) | ||
}) | ||
} | ||
} | ||
|
||
func convertToData(t *testing.T, sample string) string { | ||
t.Helper() | ||
decoded, err := hex.DecodeString(sample) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
return string(decoded) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// Licensed to Elasticsearch B.V. under one or more contributor | ||
// license agreements. See the NOTICE file distributed with | ||
// this work for additional information regarding copyright | ||
// ownership. Elasticsearch B.V. licenses this file to you under | ||
// the Apache License, Version 2.0 (the "License"); you may | ||
// not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
package mime | ||
|
||
// Detect tries to detect a mime-type based off | ||
// of a byte string passed into the function | ||
func Detect(data string) string { | ||
return DetectBytes([]byte(data)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
// Licensed to Elasticsearch B.V. under one or more contributor | ||
// license agreements. See the NOTICE file distributed with | ||
// this work for additional information regarding copyright | ||
// ownership. Elasticsearch B.V. licenses this file to you under | ||
// the Apache License, Version 2.0 (the "License"); you may | ||
// not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
package actions | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/pkg/errors" | ||
|
||
"github.com/elastic/beats/v7/libbeat/beat" | ||
"github.com/elastic/beats/v7/libbeat/common" | ||
"github.com/elastic/beats/v7/libbeat/mime" | ||
"github.com/elastic/beats/v7/libbeat/processors" | ||
"github.com/elastic/beats/v7/libbeat/processors/checks" | ||
) | ||
|
||
func init() { | ||
processors.RegisterPlugin("detect_mime_type", | ||
checks.ConfigChecked(NewDetectMimeType, | ||
checks.RequireFields("field", "target"), | ||
checks.AllowedFields("field", "target"))) | ||
} | ||
|
||
type mimeTypeProcessor struct { | ||
Field string `config:"field"` | ||
Target string `config:"target"` | ||
} | ||
|
||
// NewDetectMimeType constructs a new mime processor. | ||
func NewDetectMimeType(cfg *common.Config) (processors.Processor, error) { | ||
mimeType := &mimeTypeProcessor{} | ||
if err := cfg.Unpack(mimeType); err != nil { | ||
return nil, errors.Wrapf(err, "fail to unpack the detect_mime_type configuration") | ||
} | ||
|
||
return mimeType, nil | ||
} | ||
|
||
func (m *mimeTypeProcessor) Run(event *beat.Event) (*beat.Event, error) { | ||
valI, err := event.GetValue(m.Field) | ||
if err != nil { | ||
// doesn't have the required fieldd value to analyze | ||
return event, nil | ||
} | ||
val, _ := valI.(string) | ||
if val == "" { | ||
// wrong type or not set | ||
return event, nil | ||
} | ||
if mimeType := mime.Detect(val); mimeType != "" { | ||
event.Fields.DeepUpdate(common.MapStr{ | ||
m.Target: mimeType, | ||
}) | ||
} | ||
return event, nil | ||
} | ||
|
||
func (m *mimeTypeProcessor) String() string { | ||
return fmt.Sprintf("detect_mime_type=%+v->%+v", m.Field, m.Target) | ||
} |
Oops, something went wrong.