forked from DidierStevens/DidierStevensSuite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathXORSelection.1sc
124 lines (112 loc) · 3.62 KB
/
XORSelection.1sc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
//------------------------------------------------
//--- 010 Editor Script File
//
// File: XORSelection.1sc
// Authors: Didier Stevens
// Version: 4.0
// Purpose: Xors the current selection with a set of bytes,
// which causes the bytes to be encoded so they
// cannot be read by a human. Run the Xor again on
// the encoded selection to decode the data.
// Category: Binary
// History:
// 4.0 2018-03-25 D Stevens: refactoring to handle keys with null bytes and hex keys with whitespace
// 3.1 2016-02-10 SweetScape Software: Updated header for repository submission.
// 3.0 2014-12-11 D Stevens: Added hex processing; refactoring
// 1.0 2009-05-26 D Stevens: start
//
// Source code put in public domain by Didier Stevens, no Copyright
// https://DidierStevens.com
// Use at your own risk
//------------------------------------------------
#define TITLE "XORSelection"
int HexDigitToInt(int digit)
{
if (digit >= '0' && digit <= '9')
return digit - '0';
if (digit >= 'a' && digit <= 'f')
return digit - 'a' + 10;
if (digit >= 'A' && digit <= 'F')
return digit - 'A' + 10;
return -1;
}
string RemoveWhitespace(string sInput)
{
int iIter;
string sResult = "";
for (iIter = 0; iIter < Strlen(sInput); iIter++)
if (sInput[iIter] != ' ' && sInput[iIter] != '\n' && sInput[iIter] != '\r' && sInput[iIter] != '\t')
sResult = sResult + sInput[iIter];
return sResult;
}
void Main(void)
{
int iIter, iStart, iSize, iKeyLength;
string sKey;
// Check that a file is open
if(FileCount() == 0)
{
MessageBox(idOk, TITLE, "XORSelection can only be executed when a file is loaded.");
return;
}
// Initializes the variables
iSize = GetSelSize();
iStart = GetSelStart();
// Check that bytes were selected
if(iSize == 0)
{
iSize = FileSize();
iStart = 0;
}
sKey = InputString(TITLE, "Input the XOR key (start with 0x for hex)", "");
if (sKey == "")
{
MessageBox(idOk, TITLE, "Please enter a valid key.");
return;
}
else if (SubStr(sKey, 0, 2) == "0x")
{
string sHex = RemoveWhitespace(SubStr(sKey, 2));
int iIter2;
int iHexDigit1;
int iHexDigit2;
if (Strlen(sHex) % 2 != 0)
{
MessageBox(idOk, TITLE, "Please enter a valid hex value (uneven digits).");
return;
}
iKeyLength = Strlen(sHex) / 2;
if (iKeyLength == 0)
{
MessageBox(idOk, TITLE, "Please enter a valid hex value (no digits).");
return;
}
uchar aucKey[iKeyLength];
for (iIter2 = 0; iIter2 < Strlen(sHex); iIter2 += 2)
{
iHexDigit1 = HexDigitToInt(sHex[iIter2]);
if (iHexDigit1 == -1)
{
MessageBox(idOk, TITLE, "Please enter a valid hex value: digit %c is not hexadecimal.", sHex[iIter2]);
return;
}
iHexDigit2 = HexDigitToInt(sHex[iIter2 + 1]);
if (iHexDigit2 == -1)
{
MessageBox(idOk, TITLE, "Please enter a valid hex value: digit %c is not hexadecimal.", sHex[iIter2 + 1]);
return;
}
aucKey[iIter2 / 2] = iHexDigit1 * 0x10 + iHexDigit2;
}
}
else
{
string aucKey = sKey;
iKeyLength = Strlen(sKey);
}
// Modify the selection
for (iIter = 0; iIter < iSize; iIter++)
// Modify the current byte
WriteUByte(iStart + iIter, ReadUByte(iStart + iIter) ^ aucKey[iIter % iKeyLength]);
}
Main();