这是个原型,只能对PRG文件进行混淆,还未完善。
阿明的FOXSHELL看界面有 代码混淆功能,应是很好。
但我的需要,有些不同,要公开部分代码、公开部分二次开发接口,LEONID的混淆器只能
对EXE/APP处理,且BUG不小, 自主的混淆器暂时还是必须的。
1
2
*- OBFUSCATOR ----------------------------------------------------------------------------------------------------
3
*-
4
*- 功能: 针对 PRG代码|代码串的 混淆器,只混淆local定义和lParameters定义。
5
*-
6
*-
7
*- 现行的算法: 只是替换 LOCAL定义和lParameters定义中变量。这两个关键字必须写全,不能缩写。
8
*- 替换的必须是 独立的“词”,以cSep来界定。
9
*-
10
*- 未解决的问题:
11
*-
12
*- 1. 能会误将 字符串中字符 的与local定义同形的 也混了。算法上应区别 textmerge等
13
*- 字符串中,不替换
14
*- 除非: (1)在textmerge中 and <>之内;
15
*- (2)& 之后
16
*- (3)type('
')之内
17
*- (4)形如: evaluate('oObj.name') 中的oObj
18
*- (5)
19
*-
20
*- 这个算法未做,但影响不大,只是某些情况下有字符串会乱了。
21
*-
22
*- 2. 个funciton一起时. 应分开FUNCTION、分段处理的,目前是混在一起。有可能出错
23
*- 3. 换后,可能做成行长大于255。但vfp9好象没有行长限制了。
24
*- 4. 以分号续行的情况,未有考虑。
25
*- 5. 生成随机的变量名,除0o之外,应有其他选项。
26
*--------------------------------------------------------------------------------------------------------------------
27
28
29
Local cFile,cSep,cCode,aXtbl[1,2],i,j,k,cLine,cBin,nIdx,cVarDefKeyWord,cVarName,cVarCnt,nPos,cTmpCodes,cTmpLine
30
Local nIdx,cVarDefKeyWord,cVarName,cVarCnt,nPos,cTmpCodes,cTmpLine
31
32
33
*cFile = Getfile('prg')
34
*cFile = 'D:\02WORKSHOP\YXERP01\01STD\TEST\abc.PRG'
35
cFile = 'D:\02WORKSHOP\YXERP01\01STD\source\通用函数.PRG'
36
If Empty(cFile)
37
Return .F.
38
EndIf
39
40
*--
41
cCode=Filetostr(cFile)
42
cSep = Chr(13)+Chr(10)+Chr(9)+"()=+-*/&@<>.',%[] "+'"'
43
44
*-- 1. 去掉注解--------------------------------------------------
45
cTmpCodes=''
46
For i=1 to GetWordCount(cCode,Chr(13)+Chr(10))
47
cLine = Ltrim(GetWordNum(cCode,i,Chr(13)+Chr(10)) ,1,Chr(9),' ')
48
If Left( cLine,1) = '*'
49
Loop
50
EndIf
51
52
nPos = At('&'+'&',cLine)
53
If nPos > 0
54
cTmp = Substr(cLine,1,nPos-1)
55
If Occurs("'",cTmp)%2=0 and Occurs('"',cTmp)%2=0 &&不在字符串中
56
cLine = cTmp
57
EndIf
58
EndIf
59
60
cTmpCodes = cTmpCodes + Chr(13)+Chr(10) + cLine
61
Next
62
cCode = cTmpCodes
63
64
65
66
*-- 2. 取得local 及 lparameters 定义,生成对照表-------------------------------
67
cVarDefKeyWord='local'
68
nIdx = 0
69
Do While .T.
70
nIdx = nIdx + 1
71
nPos = Atc(cVarDefKeyWord,cCode,nIdx)
72
If nPos = 0
73
If cVarDefKeyWord='local'
74
cVarDefKeyWord='lparameters'
75
nIdx = 0
76
Loop
77
Else
78
Exit
79
EndIf
80
Endif
81
82
cLine = Substr(cCode,nPos-1,500)
83
cLine = Left( cLine, At(Chr(13),cLine))
84
If Not Inlist(Left(cLine,1),Chr(10),Chr(13),' ')
85
Loop
86
EndIf
87
88
cLine = Substr(cLine,Len(cVarDefKeyWord)+2)
89
90
If At('[',cLine)>0 or At('(',cLine)>0 &&array handling
91
cLine = Chrtran( cLine,'[]()', '****' )
92
cTmpLine=''
93
For i=1 to GetWordCount(cLine,'*')
94
cTmp = Alltrim(GetWordNum(cLine,i,'*'),1,Chr(9),' ')
95
If IsDigit(cTmp)
96
Loop
97
EndIf
98
cTmpLine = cTmpLine + cTmp
99
Next
100
cLine = cTmpLine
101
EndIf
102
*---
103
104
For i=1 To Getwordcount(cLine,',')
105
cVarName = Lower( Alltrim(Getwordnum(cLine,i,','),1,Chr(10),Chr(9),Chr(13),' ') )
106
cVarName = GetWordNum(cVarName,1 ,'([')
107
If Ascan(aXTbl,cVarName,1,0,1,2+4)>0
108
Loop
109
EndIf
110
111
If Vartype(aXtbl(1))<>'L'
112
Declare aXtbl( Alen(aXTbl,1)+1, 2)
113
EndIf
114
115
aXTbl(Alen(aXtbl,1),1) = cVarName
116
cBin = ''
117
For j=30 To 0 Step -1
118
cBin = cBin + Iif(Bittest(Rand()*100000000,i),'0','o')
119
Next
120
aXTbl(Alen(aXtbl,1),2) = 'o' + cBin
121
122
wait window nowait '取得变量: ' + padr(cVarName + ' ---> ' + 'o'+cBin,50)
123
Next
124
Enddo
125
126
127
*--3. 替换变量
.o0o0o0o0o0oooo000 --------------------------------------
128
For i=1 To Alen(aXTbl,1)
129
If Vartype(aXtbl(i,1))='L'
130
Loop
131
Endif
132
133
nIdx = 1
134
Do While .T.
135
nPos = Atc(aXtbl(i,1),cCode,nIdx)
136
If nPos=0
137
Exit
138
Endif
139
140
nBeforeChar = Substr(cCode,nPos-1,1)
141
nAfterChar = Substr(cCode,nPos+Len(aXtbl(i,1)),1)
142
If Not (nBeforeChar $ cSep Or Empty(nBeforeChar)) or ;
143
Not (nAfterChar $ cSep Or Empty(nAfterChar))
144
145
nIdx = nIdx + 1
146
Loop
147
Endif
148
*-----
149
cCode = Left(cCode, nPos-1) + aXTbl(i,2) + Substr(cCode,nPos + Len(aXTbl(i,1)) )
150
EndDo
151
152
wait window nowait 'obfuscating ' + Str( i / Alen(aXTbl,1)*100,5,2) +' % ' && +cVarName + ' ---> ' + 'o'+cBin
153
Next
154
155
StrToFile(cCode,'obfuscated.prg')
156
Wait clear
157
158
Modify Command obfuscated.prg
159
160
161
162
163
164

2
*- OBFUSCATOR ----------------------------------------------------------------------------------------------------3
*-4
*- 功能: 针对 PRG代码|代码串的 混淆器,只混淆local定义和lParameters定义。5
*-6
*-7
*- 现行的算法: 只是替换 LOCAL定义和lParameters定义中变量。这两个关键字必须写全,不能缩写。8
*- 替换的必须是 独立的“词”,以cSep来界定。 9
*-10
*- 未解决的问题: 11
*-12
*- 1. 能会误将 字符串中字符 的与local定义同形的 也混了。算法上应区别 textmerge等13
*- 字符串中,不替换 14
*- 除非: (1)在textmerge中 and <>之内;15
*- (2)& 之后16
*- (3)type('
')之内17
*- (4)形如: evaluate('oObj.name') 中的oObj18
*- (5)19
*-20
*- 这个算法未做,但影响不大,只是某些情况下有字符串会乱了。 21
*-22
*- 2. 个funciton一起时. 应分开FUNCTION、分段处理的,目前是混在一起。有可能出错23
*- 3. 换后,可能做成行长大于255。但vfp9好象没有行长限制了。24
*- 4. 以分号续行的情况,未有考虑。25
*- 5. 生成随机的变量名,除0o之外,应有其他选项。26
*--------------------------------------------------------------------------------------------------------------------27

28

29
Local cFile,cSep,cCode,aXtbl[1,2],i,j,k,cLine,cBin,nIdx,cVarDefKeyWord,cVarName,cVarCnt,nPos,cTmpCodes,cTmpLine30
Local nIdx,cVarDefKeyWord,cVarName,cVarCnt,nPos,cTmpCodes,cTmpLine31

32

33
*cFile = Getfile('prg')34
*cFile = 'D:\02WORKSHOP\YXERP01\01STD\TEST\abc.PRG'35
cFile = 'D:\02WORKSHOP\YXERP01\01STD\source\通用函数.PRG'36
If Empty(cFile)37
Return .F.38
EndIf39

40
*--41
cCode=Filetostr(cFile)42
cSep = Chr(13)+Chr(10)+Chr(9)+"()=+-*/&@<>.',%[] "+'"'43

44
*-- 1. 去掉注解-------------------------------------------------- 45
cTmpCodes=''46
For i=1 to GetWordCount(cCode,Chr(13)+Chr(10))47
cLine = Ltrim(GetWordNum(cCode,i,Chr(13)+Chr(10)) ,1,Chr(9),' ')48
If Left( cLine,1) = '*'49
Loop50
EndIf 51
52
nPos = At('&'+'&',cLine)53
If nPos > 054
cTmp = Substr(cLine,1,nPos-1)55
If Occurs("'",cTmp)%2=0 and Occurs('"',cTmp)%2=0 &&不在字符串中56
cLine = cTmp57
EndIf 58
EndIf 59
60
cTmpCodes = cTmpCodes + Chr(13)+Chr(10) + cLine61
Next 62
cCode = cTmpCodes63

64

65

66
*-- 2. 取得local 及 lparameters 定义,生成对照表-------------------------------67
cVarDefKeyWord='local'68
nIdx = 069
Do While .T.70
nIdx = nIdx + 171
nPos = Atc(cVarDefKeyWord,cCode,nIdx)72
If nPos = 0 73
If cVarDefKeyWord='local'74
cVarDefKeyWord='lparameters'75
nIdx = 076
Loop 77
Else78
Exit 79
EndIf 80
Endif81

82
cLine = Substr(cCode,nPos-1,500)83
cLine = Left( cLine, At(Chr(13),cLine))84
If Not Inlist(Left(cLine,1),Chr(10),Chr(13),' ')85
Loop86
EndIf87
88
cLine = Substr(cLine,Len(cVarDefKeyWord)+2)89

90
If At('[',cLine)>0 or At('(',cLine)>0 &&array handling 91
cLine = Chrtran( cLine,'[]()', '****' )92
cTmpLine=''93
For i=1 to GetWordCount(cLine,'*')94
cTmp = Alltrim(GetWordNum(cLine,i,'*'),1,Chr(9),' ')95
If IsDigit(cTmp)96
Loop97
EndIf 98
cTmpLine = cTmpLine + cTmp 99
Next100
cLine = cTmpLine 101
EndIf 102
*---103
104
For i=1 To Getwordcount(cLine,',')105
cVarName = Lower( Alltrim(Getwordnum(cLine,i,','),1,Chr(10),Chr(9),Chr(13),' ') )106
cVarName = GetWordNum(cVarName,1 ,'([')107
If Ascan(aXTbl,cVarName,1,0,1,2+4)>0108
Loop 109
EndIf 110
111
If Vartype(aXtbl(1))<>'L'112
Declare aXtbl( Alen(aXTbl,1)+1, 2)113
EndIf 114
115
aXTbl(Alen(aXtbl,1),1) = cVarName 116
cBin = ''117
For j=30 To 0 Step -1118
cBin = cBin + Iif(Bittest(Rand()*100000000,i),'0','o')119
Next120
aXTbl(Alen(aXtbl,1),2) = 'o' + cBin121
122
wait window nowait '取得变量: ' + padr(cVarName + ' ---> ' + 'o'+cBin,50)123
Next124
Enddo125

126

127
*--3. 替换变量
.o0o0o0o0o0oooo000 --------------------------------------128
For i=1 To Alen(aXTbl,1)129
If Vartype(aXtbl(i,1))='L'130
Loop131
Endif132

133
nIdx = 1134
Do While .T.135
nPos = Atc(aXtbl(i,1),cCode,nIdx)136
If nPos=0137
Exit138
Endif139

140
nBeforeChar = Substr(cCode,nPos-1,1)141
nAfterChar = Substr(cCode,nPos+Len(aXtbl(i,1)),1)142
If Not (nBeforeChar $ cSep Or Empty(nBeforeChar)) or ;143
Not (nAfterChar $ cSep Or Empty(nAfterChar))144

145
nIdx = nIdx + 1146
Loop147
Endif148
*-----149
cCode = Left(cCode, nPos-1) + aXTbl(i,2) + Substr(cCode,nPos + Len(aXTbl(i,1)) )150
EndDo151

152
wait window nowait 'obfuscating ' + Str( i / Alen(aXTbl,1)*100,5,2) +' % ' && +cVarName + ' ---> ' + 'o'+cBin153
Next154

155
StrToFile(cCode,'obfuscated.prg')156
Wait clear 157

158
Modify Command obfuscated.prg159

160

161

162

163
164

浙公网安备 33010602011771号