diec and refractoring
This commit is contained in:
parent
a62157e8e5
commit
0d715ccb37
20 changed files with 355 additions and 200 deletions
|
@ -35,7 +35,6 @@ func CreateFile(ctx context.Context, name string, fileBytes []byte) (sqlc.File,
|
|||
err = nil
|
||||
}
|
||||
|
||||
|
||||
//Using UUIDs instead of the file hash to make switching storage backends easier
|
||||
_, err = store.SaveFile(file.ID.String(), fileBytes)
|
||||
if err != nil {
|
||||
|
|
|
@ -11,7 +11,6 @@ import (
|
|||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
|
||||
var pool *pgxpool.Pool
|
||||
|
||||
func Connect() (*pgxpool.Pool, error) {
|
||||
|
|
|
@ -28,3 +28,20 @@ func InsertFileProperties(properties sqlc.InsertFilePropertiesParams) error {
|
|||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// InsertJsonResult() into one of the following tables:
|
||||
// diec, msoffice_mraptor, msoffice_oleid, msoffice_olevba
|
||||
func InsertJsonResult(fileID pgtype.UUID, data []byte, table string) error {
|
||||
query := sqlc.New(pool)
|
||||
var err error
|
||||
switch table {
|
||||
case "diec":
|
||||
err = query.InsertFileDIEC(context.Background(), sqlc.InsertFileDIECParams{FileID: fileID, Data: data})
|
||||
case "msoffice_oleid":
|
||||
|
||||
}
|
||||
if err != nil {
|
||||
slog.Error("Unable to insert DIEC results", "file-uuid", fileID.String(), "error", err)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
|
|
@ -3,4 +3,27 @@ INSERT INTO file_properties (
|
|||
id, sha256, md5, libmagic_mime, libmagic_extension, libmagic_apple
|
||||
) VALUES ($1, $2, $3, $4, $5, $6);
|
||||
|
||||
-- name: GetFileProperties :one
|
||||
SELECT * FROM file_properties
|
||||
WHERE id = $1;
|
||||
|
||||
-- name: InsertFileDIEC :exec
|
||||
INSERT INTO diec (
|
||||
file_id, data
|
||||
) VALUES ($1, $2);
|
||||
|
||||
-- name: InsertFileMsofficeOleid :exec
|
||||
INSERT INTO msoffice_oleid (
|
||||
file_id, data
|
||||
) VALUES ($1, $2);
|
||||
|
||||
-- name: InsertFileMsofficeOlevba :exec
|
||||
INSERT INTO msoffice_olevba (
|
||||
file_id, data
|
||||
) VALUES ($1, $2);
|
||||
|
||||
-- name: InsertFileMsofficeMraptor :exec
|
||||
INSERT INTO msoffice_mraptor (
|
||||
file_id, data
|
||||
) VALUES ($1, $2);
|
||||
|
||||
|
|
|
@ -10,7 +10,7 @@ import (
|
|||
"git.jmbit.de/jmb/scanfile/server/internal/store"
|
||||
)
|
||||
|
||||
//BasicProcessing() determines type agnostic information about the file
|
||||
// BasicProcessing() determines type agnostic information about the file
|
||||
func BasicProcessing(job sqlc.ProcessingJob) error {
|
||||
fileBytes, err := store.GetFileBytes(job.FileID.String())
|
||||
if err != nil {
|
||||
|
@ -21,6 +21,7 @@ func BasicProcessing(job sqlc.ProcessingJob) error {
|
|||
md5sum := md5.Sum(fileBytes)
|
||||
fileCmdResult, err := FileCmd(job.FileID.String())
|
||||
if err != nil {
|
||||
database.FailProcessingJob(job.ID, err)
|
||||
slog.Error("Error processing file", "file-uuid", job.FileID.String(), "error", err)
|
||||
return err
|
||||
}
|
||||
|
@ -32,9 +33,11 @@ func BasicProcessing(job sqlc.ProcessingJob) error {
|
|||
fileProperties.LibmagicMime.String = fileCmdResult.MimeType
|
||||
fileProperties.LibmagicApple.String = fileCmdResult.Apple
|
||||
fileProperties.LibmagicExtension.String = fileCmdResult.Extension
|
||||
database.InsertFileProperties(fileProperties)
|
||||
|
||||
err = database.InsertFileProperties(fileProperties)
|
||||
if err != nil {
|
||||
slog.Error("Error inserting basic file properties into database", "file-uuid", job.FileID.String(), "error", err)
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
|
|
25
server/internal/processing/basic/diec.go
Normal file
25
server/internal/processing/basic/diec.go
Normal file
|
@ -0,0 +1,25 @@
|
|||
package basic
|
||||
|
||||
import (
|
||||
"log/slog"
|
||||
"os/exec"
|
||||
|
||||
"git.jmbit.de/jmb/scanfile/server/internal/store"
|
||||
)
|
||||
|
||||
// DiecScan() runs diec -jdu on the file
|
||||
func DiecScan(fileName string) ([]byte, error) {
|
||||
var by []byte
|
||||
filepath, err := store.AbsPath(fileName)
|
||||
if err != nil {
|
||||
slog.Error("Error in DiecScan", "file-uuid", fileName, "error", err)
|
||||
return by, err
|
||||
}
|
||||
cmd := exec.Command("/usr/bin/diec", "-jdu", filepath)
|
||||
result, err := cmd.Output()
|
||||
if err != nil {
|
||||
slog.Error("Error in DiecScan", "file-uuid", fileName, "error", err)
|
||||
return by, err
|
||||
}
|
||||
return result, nil
|
||||
}
|
|
@ -8,7 +8,6 @@ import (
|
|||
"git.jmbit.de/jmb/scanfile/server/internal/store"
|
||||
)
|
||||
|
||||
|
||||
type FileCmdResult struct {
|
||||
Type string
|
||||
MimeType string
|
||||
|
@ -16,11 +15,12 @@ type FileCmdResult struct {
|
|||
Extension string
|
||||
}
|
||||
|
||||
//FileCmd() runs "/usr/bin/file" on the object. Should be replaced with libmagic bindings instead
|
||||
// FileCmd() runs "/usr/bin/file" on the object. Should be replaced with libmagic bindings instead
|
||||
func FileCmd(fileName string) (FileCmdResult, error) {
|
||||
var returnStruct FileCmdResult
|
||||
filepath, err := store.AbsPath(fileName)
|
||||
if err != nil {
|
||||
slog.Error("Error in FileCmd", "file-uuid", fileName, "error", err)
|
||||
return returnStruct, err
|
||||
}
|
||||
cmd := exec.Command("/usr/bin/file", "-b", filepath)
|
||||
|
|
|
@ -14,7 +14,8 @@ import (
|
|||
|
||||
var semaphore chan struct{}
|
||||
var swg *sync.WaitGroup
|
||||
//Used to determine if a task was started by a previous instance that stalled out or died
|
||||
|
||||
// Used to determine if a task was started by a previous instance that stalled out or died
|
||||
var startup time.Time
|
||||
|
||||
func Setup(wg *sync.WaitGroup) {
|
||||
|
@ -23,7 +24,7 @@ func Setup(wg *sync.WaitGroup) {
|
|||
}
|
||||
|
||||
// Submit() starts the analysis process for a file.
|
||||
func Submit(ctx context.Context, file pgtype.UUID ) error {
|
||||
func Submit(ctx context.Context, file pgtype.UUID) error {
|
||||
job, err := database.NewProcessingJob(ctx, file, TypeBasic)
|
||||
if err != nil {
|
||||
slog.Error("Could not submit processing job", "error", err, "file-uuid", file)
|
||||
|
@ -34,7 +35,6 @@ func Submit(ctx context.Context, file pgtype.UUID ) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
|
||||
func processJob(job sqlc.ProcessingJob) {
|
||||
|
||||
}
|
||||
|
|
|
@ -5,18 +5,23 @@ import (
|
|||
"strings"
|
||||
)
|
||||
|
||||
|
||||
const TypeBasic = "Basic"
|
||||
|
||||
// Microsoft Office Document
|
||||
const TypeMSOffice = "MSOffice"
|
||||
|
||||
// Microsoft Windows Portable Executable
|
||||
const TypePE = "PE"
|
||||
|
||||
// Linux/UNIX Executable Linkable Format
|
||||
const TypeELF = "ELF"
|
||||
|
||||
// Java Archive (JAR)
|
||||
const TypeJAR = "JAR"
|
||||
|
||||
// Archives (compressed etc.)
|
||||
const TypeArchive = "Archive"
|
||||
|
||||
// Anything not implemented (yet)
|
||||
const TypeOther = "Other"
|
||||
|
||||
|
|
|
@ -11,6 +11,89 @@ import (
|
|||
"github.com/jackc/pgx/v5/pgtype"
|
||||
)
|
||||
|
||||
const getFileProperties = `-- name: GetFileProperties :one
|
||||
SELECT id, sha256, md5, libmagic_mime, libmagic_extension, libmagic_apple FROM file_properties
|
||||
WHERE id = $1
|
||||
`
|
||||
|
||||
func (q *Queries) GetFileProperties(ctx context.Context, id pgtype.UUID) (FileProperty, error) {
|
||||
row := q.db.QueryRow(ctx, getFileProperties, id)
|
||||
var i FileProperty
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.Sha256,
|
||||
&i.Md5,
|
||||
&i.LibmagicMime,
|
||||
&i.LibmagicExtension,
|
||||
&i.LibmagicApple,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const insertFileDIEC = `-- name: InsertFileDIEC :exec
|
||||
INSERT INTO diec (
|
||||
file_id, data
|
||||
) VALUES ($1, $2)
|
||||
`
|
||||
|
||||
type InsertFileDIECParams struct {
|
||||
FileID pgtype.UUID
|
||||
Data []byte
|
||||
}
|
||||
|
||||
func (q *Queries) InsertFileDIEC(ctx context.Context, arg InsertFileDIECParams) error {
|
||||
_, err := q.db.Exec(ctx, insertFileDIEC, arg.FileID, arg.Data)
|
||||
return err
|
||||
}
|
||||
|
||||
const insertFileMsofficeMraptor = `-- name: InsertFileMsofficeMraptor :exec
|
||||
INSERT INTO msoffice_mraptor (
|
||||
file_id, data
|
||||
) VALUES ($1, $2)
|
||||
`
|
||||
|
||||
type InsertFileMsofficeMraptorParams struct {
|
||||
FileID pgtype.UUID
|
||||
Data []byte
|
||||
}
|
||||
|
||||
func (q *Queries) InsertFileMsofficeMraptor(ctx context.Context, arg InsertFileMsofficeMraptorParams) error {
|
||||
_, err := q.db.Exec(ctx, insertFileMsofficeMraptor, arg.FileID, arg.Data)
|
||||
return err
|
||||
}
|
||||
|
||||
const insertFileMsofficeOleid = `-- name: InsertFileMsofficeOleid :exec
|
||||
INSERT INTO msoffice_oleid (
|
||||
file_id, data
|
||||
) VALUES ($1, $2)
|
||||
`
|
||||
|
||||
type InsertFileMsofficeOleidParams struct {
|
||||
FileID pgtype.UUID
|
||||
Data []byte
|
||||
}
|
||||
|
||||
func (q *Queries) InsertFileMsofficeOleid(ctx context.Context, arg InsertFileMsofficeOleidParams) error {
|
||||
_, err := q.db.Exec(ctx, insertFileMsofficeOleid, arg.FileID, arg.Data)
|
||||
return err
|
||||
}
|
||||
|
||||
const insertFileMsofficeOlevba = `-- name: InsertFileMsofficeOlevba :exec
|
||||
INSERT INTO msoffice_olevba (
|
||||
file_id, data
|
||||
) VALUES ($1, $2)
|
||||
`
|
||||
|
||||
type InsertFileMsofficeOlevbaParams struct {
|
||||
FileID pgtype.UUID
|
||||
Data []byte
|
||||
}
|
||||
|
||||
func (q *Queries) InsertFileMsofficeOlevba(ctx context.Context, arg InsertFileMsofficeOlevbaParams) error {
|
||||
_, err := q.db.Exec(ctx, insertFileMsofficeOlevba, arg.FileID, arg.Data)
|
||||
return err
|
||||
}
|
||||
|
||||
const insertFileProperties = `-- name: InsertFileProperties :exec
|
||||
INSERT INTO file_properties (
|
||||
id, sha256, md5, libmagic_mime, libmagic_extension, libmagic_apple
|
||||
|
|
|
@ -33,7 +33,7 @@ func GetFileType(fileId string) (string, error) {
|
|||
return kind.MIME.Value, nil
|
||||
}
|
||||
|
||||
//Returns the MimeType for a []byte
|
||||
// Returns the MimeType for a []byte
|
||||
// We only have to pass the file header = first 261 bytes
|
||||
func GetBytesFileType(data []byte) (string, error) {
|
||||
kind, err := filetype.Match(data)
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
// templui component icon - version: main installed by templui v0.71.0
|
||||
package icon
|
||||
|
||||
// This file is auto generated
|
||||
// Using Lucide icons version 0.507.0
|
||||
var ALargeSmall = Icon("a-large-small")
|
||||
|
|
Loading…
Add table
Reference in a new issue