Skip to content
This repository was archived by the owner on Dec 10, 2025. It is now read-only.

Commit cd4d874

Browse files
Update log4j_findings.sh
- Corrected the script for handling spaces in jar names on ubuntu. - Added support for nested jars
1 parent bf5bbea commit cd4d874

1 file changed

Lines changed: 28 additions & 18 deletions

File tree

linux/log4j_findings.sh

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,22 @@ else
2121
echo "Too many parameters passed in."
2222
echo "sh ./log4j_findings.sh [base_dir] [network_filesystem_scan<true/false>]"
2323
echo "example: sh ./log4j_findings.sh /home false"
24-
echo "(default: [base_dir]=/ [network_filesystem_scan]=false)"
24+
echo "(default: [base_dir]=/ [network_filesystem_scan]=false)"
2525
exit 1
2626
fi
2727

2828
handle_war_ear_zip()
2929
{
3030
war_file=$1
31-
if jar1=`unzip -l $war_file | awk '{print $NF}'| grep -i ".jar" 2> /dev/null `;then
31+
if jar1=`zip -sf $war_file | awk '{print $NF}'| grep -i ".jar" 2> /dev/null `;then
3232
rm -rf /tmp/log4j_for_extract/
3333
mkdir /tmp/log4j_for_extract;
3434
unzip -d /tmp/log4j_for_extract/ $war_file > /dev/null
3535
fi;
3636
jars=`find /tmp/log4j_for_extract -type f -regextype posix-egrep -iregex ".+\.(jar)$" 2> /dev/null`;
3737
for i in $jars; do
38-
IFS=$'\n'
38+
IFS='
39+
'
3940
handle_jar $i $war_file
4041
done;
4142
rm -rf /tmp/log4j_for_extract/
@@ -87,21 +88,22 @@ handle_jar_with_zip()
8788
jdi="JNDI Class Not Found";
8889
fi;
8990
## Checking JNDI-Class value from jar file
90-
if test=`zip -sf $jar_file | grep -i "log4j" | grep "pom.xml"`;then
91-
IFS=$oldIFS
91+
if test=`zip -sf $jar_file | grep -i "log4j-core" | grep "pom.xml"`;then
92+
IFS=$oldIFS;
9293
echo "Source: "$test;
9394
log4j_exists=1;
9495
## Reading file pom.xml to fetch log4j version
9596
echo "JNDI-Class: "$jdi;
96-
IFS=$'\n'
97+
IFS='
98+
'
9799
if [ ! -z "$war_file" ];then
98100
p=`echo $jar_file | sed -n 's|^/tmp/log4j_for_extract/||p' `;
99101
echo 'Path= '$war_file'/'$p
100102
else
101103
echo 'Path= '$jar_file
102104
fi
103-
IFS=$oldIFS
104-
ve=`unzip -p $i $test 2> /dev/null | grep -Pzo "<artifactId>log4j</artifactId>\s*<version>.+?</version>"| cut -d ">" -f 2 | cut -d "<" -f 1 | head -2|awk 'ORS=NR%3?FS:RS'`;
105+
IFS=$oldIFS;
106+
ve=`unzip -p $jar_file $test 2> /dev/null | grep -Pzo "<artifactId>log4j</artifactId>\s*<version>.+?</version>"| cut -d ">" -f 2 | cut -d "<" -f 1 | head -2|awk 'ORS=NR%3?FS:RS'`;
105107
if [ -z "$ve" ]; then
106108
echo 'log4j Unknown';
107109
else
@@ -134,31 +136,38 @@ log4j()
134136
unzip -v 2> /dev/null 1> /dev/null;
135137
isUnZip=$?;
136138
log4j_exists=0;
137-
oldIFS=$IFS
139+
oldIFS=$IFS;
138140

139141
# Change to a network filesystem only scan if 2nd parameter is true. network filesystem scan command
140142
# does not use '!' flags
141143
if [ $NETDIR_SCAN = true ];then
142-
jars=$(find ${BASEDIR} -type f -regextype posix-egrep -iregex ".+\.(jar|war|ear|zip)$" 2> /dev/null);
144+
jars=`find ${BASEDIR} -type f -regextype posix-egrep -iregex ".+\.(jar|war|ear|zip)$" 2> /dev/null`;
143145
else
144-
jars=$(find ${BASEDIR} -type f -regextype posix-egrep -iregex ".+\.(jar|war|ear|zip)$" ! -fstype nfs ! -fstype nfs4 ! -fstype cifs ! -fstype smbfs ! -fstype gfs ! -fstype gfs2 ! -fstype safenetfs ! -fstype secfs ! -fstype gpfs ! -fstype smb2 ! -fstype vxfs ! -fstype vxodmfs ! -fstype afs -print 2>/dev/null);
146+
jars=`find ${BASEDIR} -type f -regextype posix-egrep -iregex ".+\.(jar|war|ear|zip)$" ! -fstype nfs ! -fstype nfs4 ! -fstype cifs ! -fstype smbfs ! -fstype gfs ! -fstype gfs2 ! -fstype safenetfs ! -fstype secfs ! -fstype gpfs ! -fstype smb2 ! -fstype vxfs ! -fstype vxodmfs ! -fstype afs -print 2>/dev/null`;
145147
fi
146148

147-
IFS=$'\n'
148-
for i in $jars ; do
149-
if `echo $i | grep -q ".jar"`; then
150-
handle_jar $i
149+
IFS='
150+
'
151+
for i in $jars ; do
152+
if `echo $i | grep -q ".jar"`; then
153+
if nested=`zip -sf $i | awk '{print $NF}'| grep -i ".jar" 2>/dev/null`;then
154+
handle_war_ear_zip $i
155+
else
156+
handle_jar $i
157+
fi;
151158
else
152159
if [ "$isZip" -eq 0 ] && [ "$isUnZip" -eq 0 ];then
153160
handle_war_ear_zip $i
154161
else
155162
echo "Zip/Unzip utility not present on the system, skipping processing of file: "$i >> /usr/local/qualys/cloud-agent/log4j_findings.stderr;
156163
fi
157164
fi
158-
IFS=$'\n'
165+
IFS='
166+
'
159167
done
160-
if [[ $log4j_exists -eq 0 ]]; then
161-
echo "No log4j jars found on the system for base directory , exiting now.";
168+
IFS=$oldIFS;
169+
if [ $log4j_exists -eq 0 ]; then
170+
echo "No log4j jars found on the system , exiting now.";
162171
fi;
163172
echo "Run status : Success" >> /usr/local/qualys/cloud-agent/log4j_findings.stderr;
164173
};
@@ -174,3 +183,4 @@ else
174183
rm -rf /usr/local/qualys/cloud-agent/log4j_findings.stdout;
175184
echo "Flag is disabled, skipping command execution" > /usr/local/qualys/cloud-agent/log4j_findings.stderr;
176185
fi;
186+

0 commit comments

Comments
 (0)