-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
pgsql: initial support #5700
Closed
Closed
pgsql: initial support #5700
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -66,6 +66,7 @@ pub mod ftp; | |
pub mod smb; | ||
pub mod krb; | ||
pub mod dcerpc; | ||
pub mod pgsql; | ||
|
||
pub mod ikev2; | ||
pub mod snmp; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/* Copyright (C) 2020 Open Information Security Foundation | ||
* | ||
* You can copy, redistribute or modify this Program under the terms of | ||
* the GNU General Public License version 2 as published by the Free | ||
* Software Foundation. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* version 2 along with this program; if not, write to the Free Software | ||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA | ||
* 02110-1301, USA. | ||
*/ | ||
|
||
mod parser; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
/* Copyright (C) 2020 Open Information Security Foundation | ||
* | ||
* You can copy, redistribute or modify this Program under the terms of | ||
* the GNU General Public License version 2 as published by the Free | ||
* Software Foundation. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* version 2 along with this program; if not, write to the Free Software | ||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA | ||
* 02110-1301, USA. | ||
*/ | ||
|
||
use nom::Err; | ||
use nom::error::ErrorKind; | ||
use nom::IResult; | ||
|
||
pub const PGSQL_LENGTH_SIZE: usize = 4; | ||
pub const PGSQL_PROTO_SIZE: usize = 4; | ||
|
||
// PostgreSQL Startup message packet | ||
#[derive(Debug, PartialEq)] | ||
pub struct PGSQLStartupPacket<'a> { | ||
pub length: u32, | ||
pub proto_version_major: u16, | ||
pub proto_version_minor: u16, | ||
pub data: &'a[u8], | ||
} | ||
|
||
impl<'a> PGSQLStartupPacket<'a> { | ||
pub fn is_valid(&self) -> bool { | ||
let valid: bool = match self.proto_version_major { | ||
1 | 2 | 3 => true, | ||
_ => false, | ||
}; | ||
valid | ||
} | ||
} | ||
|
||
named!(pub parse_pgsql_startup_packet<PGSQLStartupPacket>, | ||
do_parse!( | ||
len: bits!(take_bits!(32u32)) | ||
>> proto_version: bits!(tuple!( | ||
take_bits!(16u16), | ||
take_bits!(16u16))) | ||
>> data: take!(len as usize - PGSQL_LENGTH_SIZE - PGSQL_PROTO_SIZE) | ||
>> (PGSQLStartupPacket { | ||
length: len, | ||
proto_version_major: proto_version.0, | ||
proto_version_minor: proto_version.1, | ||
data: data, | ||
}) | ||
)); | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
|
||
use super::*; | ||
|
||
#[test] | ||
fn test_parse_pgsql_startup_packet() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think a good next step would be to add a test for invalid input. For example one where the length field contains a value smaller than |
||
// Startup message | ||
let buff:&[u8] = &[ | ||
/* Length */ 0x00, 0x00, 0x00, 0x52, | ||
/* Protocol Version */ 0x00, 0x03, 0x00, 0x00, | ||
/* Data */ 0x75, 0x73, 0x65, 0x72, 0x00, 0x69, 0x6e, 0x64, 0x65, 0x78, | ||
0x65, 0x72, 0x00, 0x64, 0x61, 0x74, 0x61, 0x62, 0x61, 0x73, | ||
0x65, 0x00, 0x69, 0x6e, 0x64, 0x65, 0x78, 0x65, 0x72, 0x00, | ||
0x61, 0x70, 0x70, 0x6c, 0x69, 0x63, 0x61, 0x74, 0x69, 0x6f, | ||
0x6e, 0x5f, 0x6e, 0x61, 0x6d, 0x65, 0x00, 0x70, 0x73, 0x71, | ||
0x6c, 0x00, 0x63, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x5f, 0x65, | ||
0x6e, 0x63, 0x6f, 0x64, 0x69, 0x6e, 0x67, 0x00, 0x55, 0x54, | ||
0x46, 0x38, 0x00, 0x00]; | ||
|
||
let result = parse_pgsql_startup_packet(&buff); | ||
|
||
match result { | ||
Ok((remainder, packet)) => { | ||
assert_eq!(packet.length, 82); | ||
// data.len() after taking length and protcol away | ||
assert_eq!(packet.data.len(), 74); | ||
|
||
// there should be nothing left | ||
assert_eq!(remainder.len(), 0); | ||
|
||
// this packet has protocol version 3.0, so it is valid | ||
assert_eq!(packet.is_valid(), true); | ||
} | ||
Err(nom::Err::Error((remainder, err))) => { | ||
panic!("Result should not be an error: {:?}.", err); | ||
} | ||
Err(nom::Err::Incomplete(_)) => { | ||
panic!("Result should not have been incomplete."); | ||
} | ||
_ => { | ||
panic!("Unexpected behavior!"); | ||
} | ||
} | ||
} | ||
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I always find this hard to read. Would
len as usize - (PGSQL_LENGTH_SIZE + PGSQL_PROTO_SIZE)
be the same?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll try that and see if it works! Although this will likely not be necessary anymore, following Jason's reasoning.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe it does, I've changed it and code runs, test still working.