mirror of
https://github.com/zeek/zeek.git
synced 2025-10-12 03:28:19 +00:00
Refactoring of Base64 functions.
Base64Converter now uses a connection directly, instead of an analyzer redirecting to the underlying connection for reporting to Weird. The new built-in functions en-/decode_base64_intern make use of this to send encoding-errors to Weird instead of Reporter. According to the documentation, using the empty string as alphabet in the built-in functions, will use the default alphabet. Therefore the built-in functions can now use default arguments and en-/decode_base64_custom is deprecated. The tests have been updated accordingly.
This commit is contained in:
parent
4c2aa804e1
commit
8f3ded5e2d
8 changed files with 136 additions and 28 deletions
|
@ -82,7 +82,7 @@ int* Base64Converter::InitBase64Table(const string& alphabet)
|
||||||
return base64_table;
|
return base64_table;
|
||||||
}
|
}
|
||||||
|
|
||||||
Base64Converter::Base64Converter(analyzer::Analyzer* arg_analyzer, const string& arg_alphabet)
|
Base64Converter::Base64Converter(Connection* arg_conn, const string& arg_alphabet)
|
||||||
{
|
{
|
||||||
if ( arg_alphabet.size() > 0 )
|
if ( arg_alphabet.size() > 0 )
|
||||||
{
|
{
|
||||||
|
@ -98,7 +98,7 @@ Base64Converter::Base64Converter(analyzer::Analyzer* arg_analyzer, const string&
|
||||||
base64_group_next = 0;
|
base64_group_next = 0;
|
||||||
base64_padding = base64_after_padding = 0;
|
base64_padding = base64_after_padding = 0;
|
||||||
errored = 0;
|
errored = 0;
|
||||||
analyzer = arg_analyzer;
|
conn = arg_conn;
|
||||||
}
|
}
|
||||||
|
|
||||||
Base64Converter::~Base64Converter()
|
Base64Converter::~Base64Converter()
|
||||||
|
@ -216,9 +216,9 @@ int Base64Converter::Done(int* pblen, char** pbuf)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
BroString* decode_base64(const BroString* s, const BroString* a)
|
BroString* decode_base64(const BroString* s, const BroString* a, Connection* conn)
|
||||||
{
|
{
|
||||||
if ( a && a->Len() != 64 )
|
if ( a && a->Len() != 0 && a->Len() != 64 )
|
||||||
{
|
{
|
||||||
reporter->Error("base64 decoding alphabet is not 64 characters: %s",
|
reporter->Error("base64 decoding alphabet is not 64 characters: %s",
|
||||||
a->CheckString());
|
a->CheckString());
|
||||||
|
@ -229,7 +229,7 @@ BroString* decode_base64(const BroString* s, const BroString* a)
|
||||||
int rlen2, rlen = buf_len;
|
int rlen2, rlen = buf_len;
|
||||||
char* rbuf2, *rbuf = new char[rlen];
|
char* rbuf2, *rbuf = new char[rlen];
|
||||||
|
|
||||||
Base64Converter dec(0, a ? a->CheckString() : "");
|
Base64Converter dec(conn, a ? a->CheckString() : "");
|
||||||
if ( dec.Decode(s->Len(), (const char*) s->Bytes(), &rlen, &rbuf) == -1 )
|
if ( dec.Decode(s->Len(), (const char*) s->Bytes(), &rlen, &rbuf) == -1 )
|
||||||
goto err;
|
goto err;
|
||||||
|
|
||||||
|
@ -248,9 +248,9 @@ err:
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
BroString* encode_base64(const BroString* s, const BroString* a)
|
BroString* encode_base64(const BroString* s, const BroString* a, Connection* conn)
|
||||||
{
|
{
|
||||||
if ( a && a->Len() != 64 )
|
if ( a && a->Len() != 0 && a->Len() != 64 )
|
||||||
{
|
{
|
||||||
reporter->Error("base64 alphabet is not 64 characters: %s",
|
reporter->Error("base64 alphabet is not 64 characters: %s",
|
||||||
a->CheckString());
|
a->CheckString());
|
||||||
|
@ -259,7 +259,7 @@ BroString* encode_base64(const BroString* s, const BroString* a)
|
||||||
|
|
||||||
char* outbuf = 0;
|
char* outbuf = 0;
|
||||||
int outlen = 0;
|
int outlen = 0;
|
||||||
Base64Converter enc(0, a ? a->CheckString() : "");
|
Base64Converter enc(conn, a ? a->CheckString() : "");
|
||||||
enc.Encode(s->Len(), (const unsigned char*) s->Bytes(), &outlen, &outbuf);
|
enc.Encode(s->Len(), (const unsigned char*) s->Bytes(), &outlen, &outbuf);
|
||||||
|
|
||||||
return new BroString(1, (u_char*)outbuf, outlen);
|
return new BroString(1, (u_char*)outbuf, outlen);
|
||||||
|
|
19
src/Base64.h
19
src/Base64.h
|
@ -8,15 +8,16 @@
|
||||||
#include "util.h"
|
#include "util.h"
|
||||||
#include "BroString.h"
|
#include "BroString.h"
|
||||||
#include "Reporter.h"
|
#include "Reporter.h"
|
||||||
#include "analyzer/Analyzer.h"
|
#include "Conn.h"
|
||||||
|
|
||||||
// Maybe we should have a base class for generic decoders?
|
// Maybe we should have a base class for generic decoders?
|
||||||
class Base64Converter {
|
class Base64Converter {
|
||||||
public:
|
public:
|
||||||
// <analyzer> is used for error reporting, and it should be zero when
|
// <conn> is used for error reporting. If it is set to zero, e.g. done by the
|
||||||
// the decoder is called by the built-in function decode_base64() or encode_base64().
|
// built-in functions decode_base64() and encode_base64(), encoding-errors will
|
||||||
|
// go to Reporter instead of Weird. Usage-errors go to Reporter in any case.
|
||||||
// Empty alphabet indicates the default base64 alphabet.
|
// Empty alphabet indicates the default base64 alphabet.
|
||||||
Base64Converter(analyzer::Analyzer* analyzer, const string& alphabet = "");
|
Base64Converter(Connection* conn, const string& alphabet = "");
|
||||||
~Base64Converter();
|
~Base64Converter();
|
||||||
|
|
||||||
// A note on Decode():
|
// A note on Decode():
|
||||||
|
@ -42,8 +43,8 @@ public:
|
||||||
void IllegalEncoding(const char* msg)
|
void IllegalEncoding(const char* msg)
|
||||||
{
|
{
|
||||||
// strncpy(error_msg, msg, sizeof(error_msg));
|
// strncpy(error_msg, msg, sizeof(error_msg));
|
||||||
if ( analyzer )
|
if ( conn )
|
||||||
analyzer->Weird("base64_illegal_encoding", msg);
|
conn->Weird("base64_illegal_encoding", msg);
|
||||||
else
|
else
|
||||||
reporter->Error("%s", msg);
|
reporter->Error("%s", msg);
|
||||||
}
|
}
|
||||||
|
@ -63,11 +64,11 @@ protected:
|
||||||
int base64_after_padding;
|
int base64_after_padding;
|
||||||
int* base64_table;
|
int* base64_table;
|
||||||
int errored; // if true, we encountered an error - skip further processing
|
int errored; // if true, we encountered an error - skip further processing
|
||||||
analyzer::Analyzer* analyzer;
|
Connection* conn;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
BroString* decode_base64(const BroString* s, const BroString* a = 0);
|
BroString* decode_base64(const BroString* s, const BroString* a = 0, Connection* conn = 0);
|
||||||
BroString* encode_base64(const BroString* s, const BroString* a = 0);
|
BroString* encode_base64(const BroString* s, const BroString* a = 0, Connection* conn = 0);
|
||||||
|
|
||||||
#endif /* base64_h */
|
#endif /* base64_h */
|
||||||
|
|
|
@ -1116,13 +1116,21 @@ void MIME_Entity::DecodeBase64(int len, const char* data)
|
||||||
|
|
||||||
void MIME_Entity::StartDecodeBase64()
|
void MIME_Entity::StartDecodeBase64()
|
||||||
{
|
{
|
||||||
|
analyzer::Analyzer* analyzer = message->GetAnalyzer();
|
||||||
|
Connection* conn = 0;
|
||||||
|
|
||||||
if ( base64_decoder )
|
if ( base64_decoder )
|
||||||
{
|
{
|
||||||
reporter->InternalWarning("previous MIME Base64 decoder not released");
|
reporter->InternalWarning("previous MIME Base64 decoder not released");
|
||||||
delete base64_decoder;
|
delete base64_decoder;
|
||||||
}
|
}
|
||||||
|
|
||||||
base64_decoder = new Base64Converter(message->GetAnalyzer());
|
if( analyzer )
|
||||||
|
conn = analyzer->Conn();
|
||||||
|
else
|
||||||
|
reporter->InternalWarning("no analyzer associated with MIME message");
|
||||||
|
|
||||||
|
base64_decoder = new Base64Converter(conn);
|
||||||
}
|
}
|
||||||
|
|
||||||
void MIME_Entity::FinishDecodeBase64()
|
void MIME_Entity::FinishDecodeBase64()
|
||||||
|
|
94
src/bro.bif
94
src/bro.bif
|
@ -2723,14 +2723,51 @@ function hexstr_to_bytestring%(hexstr: string%): string
|
||||||
|
|
||||||
## Encodes a Base64-encoded string.
|
## Encodes a Base64-encoded string.
|
||||||
##
|
##
|
||||||
## s: The string to encode
|
## s: The string to encode.
|
||||||
|
##
|
||||||
|
## a: The custom alphabet. The empty string indicates the default alphabet. The
|
||||||
|
## length of *a* must be 64. For example, a custom alphabet could be
|
||||||
|
## ``"!#$%&/(),-.:;<>@[]^ `_{|}~abcdefghijklmnopqrstuvwxyz0123456789+?"``.
|
||||||
##
|
##
|
||||||
## Returns: The encoded version of *s*.
|
## Returns: The encoded version of *s*.
|
||||||
##
|
##
|
||||||
## .. bro:see:: encode_base64_custom decode_base64
|
## .. bro:see:: encode_base64_intern decode_base64
|
||||||
function encode_base64%(s: string%): string
|
function encode_base64%(s: string, a: string &default=""%): string
|
||||||
%{
|
%{
|
||||||
BroString* t = encode_base64(s->AsString());
|
BroString* t = encode_base64(s->AsString(), a->AsString());
|
||||||
|
if ( t )
|
||||||
|
return new StringVal(t);
|
||||||
|
else
|
||||||
|
{
|
||||||
|
reporter->Error("error in encoding string %s", s->CheckString());
|
||||||
|
return new StringVal("");
|
||||||
|
}
|
||||||
|
%}
|
||||||
|
|
||||||
|
## Encodes a Base64-encoded string.
|
||||||
|
##
|
||||||
|
## cid: The connection identifier, identifiying the connection which is used to
|
||||||
|
## handle encoding-errors (errors will go to Weird).
|
||||||
|
##
|
||||||
|
## s: The string to encode.
|
||||||
|
##
|
||||||
|
## a: The custom alphabet. The empty string indicates the default alphabet. The
|
||||||
|
## length of *a* must be 64. For example, a custom alphabet could be
|
||||||
|
## ``"!#$%&/(),-.:;<>@[]^ `_{|}~abcdefghijklmnopqrstuvwxyz0123456789+?"``.
|
||||||
|
##
|
||||||
|
## Returns: The encoded version of *s*.
|
||||||
|
##
|
||||||
|
## .. bro:see:: encode_base64 decode_base64_intern
|
||||||
|
function encode_base64_intern%(cid: conn_id, s: string, a: string &default=""%): string
|
||||||
|
%{
|
||||||
|
Connection* conn = sessions->FindConnection(cid);
|
||||||
|
if ( ! conn )
|
||||||
|
{
|
||||||
|
builtin_error("connection ID not a known connection", cid);
|
||||||
|
return new StringVal("");
|
||||||
|
}
|
||||||
|
|
||||||
|
BroString* t = encode_base64(s->AsString(), a->AsString(), conn);
|
||||||
if ( t )
|
if ( t )
|
||||||
return new StringVal(t);
|
return new StringVal(t);
|
||||||
else
|
else
|
||||||
|
@ -2742,7 +2779,7 @@ function encode_base64%(s: string%): string
|
||||||
|
|
||||||
## Encodes a Base64-encoded string with a custom alphabet.
|
## Encodes a Base64-encoded string with a custom alphabet.
|
||||||
##
|
##
|
||||||
## s: The string to encode
|
## s: The string to encode.
|
||||||
##
|
##
|
||||||
## a: The custom alphabet. The empty string indicates the default alphabet. The
|
## a: The custom alphabet. The empty string indicates the default alphabet. The
|
||||||
## length of *a* must be 64. For example, a custom alphabet could be
|
## length of *a* must be 64. For example, a custom alphabet could be
|
||||||
|
@ -2751,7 +2788,7 @@ function encode_base64%(s: string%): string
|
||||||
## Returns: The encoded version of *s*.
|
## Returns: The encoded version of *s*.
|
||||||
##
|
##
|
||||||
## .. bro:see:: encode_base64 decode_base64_custom
|
## .. bro:see:: encode_base64 decode_base64_custom
|
||||||
function encode_base64_custom%(s: string, a: string%): string
|
function encode_base64_custom%(s: string, a: string%): string &deprecated
|
||||||
%{
|
%{
|
||||||
BroString* t = encode_base64(s->AsString(), a->AsString());
|
BroString* t = encode_base64(s->AsString(), a->AsString());
|
||||||
if ( t )
|
if ( t )
|
||||||
|
@ -2767,12 +2804,49 @@ function encode_base64_custom%(s: string, a: string%): string
|
||||||
##
|
##
|
||||||
## s: The Base64-encoded string.
|
## s: The Base64-encoded string.
|
||||||
##
|
##
|
||||||
|
## a: The custom alphabet. The empty string indicates the default alphabet. The
|
||||||
|
## length of *a* must be 64. For example, a custom alphabet could be
|
||||||
|
## ``"!#$%&/(),-.:;<>@[]^ `_{|}~abcdefghijklmnopqrstuvwxyz0123456789+?"``.
|
||||||
|
##
|
||||||
## Returns: The decoded version of *s*.
|
## Returns: The decoded version of *s*.
|
||||||
##
|
##
|
||||||
## .. bro:see:: decode_base64_custom encode_base64
|
## .. bro:see:: decode_base64_intern encode_base64
|
||||||
function decode_base64%(s: string%): string
|
function decode_base64%(s: string, a: string &default=""%): string
|
||||||
%{
|
%{
|
||||||
BroString* t = decode_base64(s->AsString());
|
BroString* t = decode_base64(s->AsString(), a->AsString());
|
||||||
|
if ( t )
|
||||||
|
return new StringVal(t);
|
||||||
|
else
|
||||||
|
{
|
||||||
|
reporter->Error("error in decoding string %s", s->CheckString());
|
||||||
|
return new StringVal("");
|
||||||
|
}
|
||||||
|
%}
|
||||||
|
|
||||||
|
## Decodes a Base64-encoded string.
|
||||||
|
##
|
||||||
|
## cid: The connection identifier, identifiying the connection which is used to
|
||||||
|
## handle encoding-errors (errors will go to Weird).
|
||||||
|
##
|
||||||
|
## s: The Base64-encoded string.
|
||||||
|
##
|
||||||
|
## a: The custom alphabet. The empty string indicates the default alphabet. The
|
||||||
|
## length of *a* must be 64. For example, a custom alphabet could be
|
||||||
|
## ``"!#$%&/(),-.:;<>@[]^ `_{|}~abcdefghijklmnopqrstuvwxyz0123456789+?"``.
|
||||||
|
##
|
||||||
|
## Returns: The decoded version of *s*.
|
||||||
|
##
|
||||||
|
## .. bro:see:: decode_base64 encode_base64_intern
|
||||||
|
function decode_base64_intern%(cid: conn_id, s: string, a: string &default=""%): string
|
||||||
|
%{
|
||||||
|
Connection* conn = sessions->FindConnection(cid);
|
||||||
|
if ( ! conn )
|
||||||
|
{
|
||||||
|
builtin_error("connection ID not a known connection", cid);
|
||||||
|
return new StringVal("");
|
||||||
|
}
|
||||||
|
|
||||||
|
BroString* t = decode_base64(s->AsString(), a->AsString(), conn);
|
||||||
if ( t )
|
if ( t )
|
||||||
return new StringVal(t);
|
return new StringVal(t);
|
||||||
else
|
else
|
||||||
|
@ -2793,7 +2867,7 @@ function decode_base64%(s: string%): string
|
||||||
## Returns: The decoded version of *s*.
|
## Returns: The decoded version of *s*.
|
||||||
##
|
##
|
||||||
## .. bro:see:: decode_base64 encode_base64_custom
|
## .. bro:see:: decode_base64 encode_base64_custom
|
||||||
function decode_base64_custom%(s: string, a: string%): string
|
function decode_base64_custom%(s: string, a: string%): string &deprecated
|
||||||
%{
|
%{
|
||||||
BroString* t = decode_base64(s->AsString(), a->AsString());
|
BroString* t = decode_base64(s->AsString(), a->AsString());
|
||||||
if ( t )
|
if ( t )
|
||||||
|
|
|
@ -4,3 +4,11 @@ bro
|
||||||
bro
|
bro
|
||||||
bro
|
bro
|
||||||
bro
|
bro
|
||||||
|
bro
|
||||||
|
bro
|
||||||
|
bro
|
||||||
|
bro
|
||||||
|
bro
|
||||||
|
bro
|
||||||
|
bro
|
||||||
|
bro
|
||||||
|
|
|
@ -1,5 +1,9 @@
|
||||||
YnJv
|
YnJv
|
||||||
YnJv
|
YnJv
|
||||||
|
YnJv
|
||||||
|
}n-v
|
||||||
|
YnJv
|
||||||
|
YnJv
|
||||||
}n-v
|
}n-v
|
||||||
cGFkZGluZw==
|
cGFkZGluZw==
|
||||||
cGFkZGluZzE=
|
cGFkZGluZzE=
|
||||||
|
|
|
@ -6,9 +6,17 @@ global default_alphabet: string = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrs
|
||||||
global my_alphabet: string = "!#$%&/(),-.:;<>@[]^ `_{|}~abcdefghijklmnopqrstuvwxyz0123456789+?";
|
global my_alphabet: string = "!#$%&/(),-.:;<>@[]^ `_{|}~abcdefghijklmnopqrstuvwxyz0123456789+?";
|
||||||
|
|
||||||
print decode_base64("YnJv");
|
print decode_base64("YnJv");
|
||||||
|
print decode_base64("YnJv", default_alphabet);
|
||||||
|
print decode_base64("YnJv", ""); # should use default alpabet
|
||||||
|
print decode_base64("}n-v", my_alphabet);
|
||||||
print decode_base64_custom("YnJv", default_alphabet);
|
print decode_base64_custom("YnJv", default_alphabet);
|
||||||
|
print decode_base64_custom("YnJv", ""); # should use default alpabet
|
||||||
print decode_base64_custom("}n-v", my_alphabet);
|
print decode_base64_custom("}n-v", my_alphabet);
|
||||||
|
|
||||||
print decode_base64("YnJv");
|
print decode_base64("YnJv");
|
||||||
|
print decode_base64("YnJv", default_alphabet);
|
||||||
|
print decode_base64("YnJv", ""); # should use default alpabet
|
||||||
|
print decode_base64("}n-v", my_alphabet);
|
||||||
print decode_base64_custom("YnJv", default_alphabet);
|
print decode_base64_custom("YnJv", default_alphabet);
|
||||||
|
print decode_base64_custom("YnJv", ""); # should use default alpabet
|
||||||
print decode_base64_custom("}n-v", my_alphabet);
|
print decode_base64_custom("}n-v", my_alphabet);
|
||||||
|
|
|
@ -6,7 +6,12 @@ global default_alphabet: string = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrs
|
||||||
global my_alphabet: string = "!#$%&/(),-.:;<>@[]^ `_{|}~abcdefghijklmnopqrstuvwxyz0123456789+?";
|
global my_alphabet: string = "!#$%&/(),-.:;<>@[]^ `_{|}~abcdefghijklmnopqrstuvwxyz0123456789+?";
|
||||||
|
|
||||||
print encode_base64("bro");
|
print encode_base64("bro");
|
||||||
|
print encode_base64("bro", default_alphabet);
|
||||||
|
print encode_base64("bro", ""); # should use default alpabet
|
||||||
|
print encode_base64("bro", my_alphabet);
|
||||||
|
|
||||||
print encode_base64_custom("bro", default_alphabet);
|
print encode_base64_custom("bro", default_alphabet);
|
||||||
|
print encode_base64_custom("bro", ""); # should use default alpabet
|
||||||
print encode_base64_custom("bro", my_alphabet);
|
print encode_base64_custom("bro", my_alphabet);
|
||||||
|
|
||||||
print encode_base64("padding");
|
print encode_base64("padding");
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue