Skip to content

Commit af4857f

Browse files
authored
Merge pull request #308 from lunasec-io/add-jar-patcher
Add jar patcher command to log4shell cli Former-commit-id: e330778 Former-commit-id: 235934f9c62905ba413fe185445a2339a53c63da
2 parents 6db72a0 + fad8feb commit af4857f

File tree

21 files changed

+1162
-821
lines changed

21 files changed

+1162
-821
lines changed

.idea/vcs.xml

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tools/log4shell/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ payload:
99

1010
cli:
1111
touch ${LIBRARY_HASHES}
12-
go build -o ${BINARY_NAME} .
12+
CGO_ENABLED=0 GOOS=linux go build -o ${BINARY_NAME} .
1313

1414
library-hashes: cli
1515
./log4shell analyze --output ${LIBRARY_HASHES} test/vulnerable-log4j2-versions/apache test/vulnerable-log4j2-versions/target/dependency

tools/log4shell/analyze/analyze.go

Lines changed: 46 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package analyze
1616

1717
import (
18+
"archive/zip"
1819
"github.com/blang/semver/v4"
1920
"github.com/lunasec-io/lunasec/tools/log4shell/constants"
2021
"github.com/lunasec-io/lunasec/tools/log4shell/types"
@@ -96,7 +97,35 @@ func fileNameToSemver(fileNameNoExt string) string {
9697
return semverVersion
9798
}
9899

99-
func ProcessArchiveFile(reader io.Reader, filePath, fileName string) (finding *types.Finding) {
100+
func GetJndiLookupHash(zipReader *zip.Reader, filePath string) (fileHash string) {
101+
reader, err := zipReader.Open(constants.JndiLookupClasspath)
102+
if err != nil {
103+
log.Debug().
104+
Str("fieName", constants.JndiLookupClasspath).
105+
Str("path", filePath).
106+
Err(err).
107+
Msg("cannot find file in zip")
108+
return
109+
}
110+
defer reader.Close()
111+
112+
fileHash, err = util.HexEncodedSha256FromReader(reader)
113+
if err != nil {
114+
log.Debug().
115+
Str("fieName", constants.JndiLookupClasspath).
116+
Str("path", filePath).
117+
Err(err).
118+
Msg("unable to hash JndiLookup.class file")
119+
return
120+
}
121+
return
122+
}
123+
124+
func ProcessArchiveFile(zipReader *zip.Reader, reader io.Reader, filePath, fileName string) (finding *types.Finding) {
125+
var (
126+
jndiLookupFileHash string
127+
)
128+
100129
_, file := path.Split(filePath)
101130
fileNameNoExt := strings.TrimSuffix(file, path.Ext(file))
102131

@@ -128,26 +157,35 @@ func ProcessArchiveFile(reader io.Reader, filePath, fileName string) (finding *t
128157
return
129158
}
130159

131-
log.Log().
132-
Str("path", filePath).
133-
Str("fileName", fileName).
134-
Str("fileHash", fileHash).
135-
Msg("identified library version")
136-
137160
if versionCve == "" {
138161
log.Debug().
139162
Str("hash", fileHash).
140163
Str("version", semverVersion).
141164
Msg("Skipping version as it is not vulnerable to any known CVE")
142-
return nil
165+
return
143166
}
144167

168+
if versionIsInRange(fileNameNoExt, semverVersion, constants.JndiLookupPatchFileVersions) {
169+
jndiLookupFileHash = GetJndiLookupHash(zipReader, filePath)
170+
}
171+
172+
log.Log().
173+
Str("path", filePath).
174+
Str("fileName", fileName).
175+
Str("fileHash", fileHash).
176+
Str("jndiLookupFileName", constants.JndiLookupClasspath).
177+
Str("jndiLookupFileHash", jndiLookupFileHash).
178+
Msg("identified library version")
179+
145180
finding = &types.Finding{
146181
Path: filePath,
147182
FileName: fileName,
148183
Hash: fileHash,
184+
JndiLookupFileName: constants.JndiLookupClasspath,
185+
JndiLookupHash: jndiLookupFileHash,
149186
Version: semverVersion,
150187
CVE: versionCve,
188+
Severity: constants.CveSeverityLookup[versionCve],
151189
}
152190
return
153191
}

0 commit comments

Comments
 (0)