Skip to content
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

Port to Python 3 #6

Open
wants to merge 23 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
0a3c006
First pass on porting nidsmodule to python 3
comrumino Sep 4, 2019
961d255
Ported Example from 2 to 3
comrumino Sep 4, 2019
cb08ec5
Added .clang-format to make style in nidsmodule.c consistent
comrumino Sep 22, 2019
f194a2d
Ran clang-format and cleaned up docstrings
comrumino Sep 22, 2019
283d548
Fix incorrect sed change due to docstring macro and change in multili…
comrumino Sep 22, 2019
6d51fef
Cleanup macros for long/int unification in the C-API for python 3
comrumino Sep 22, 2019
c0697f1
Removed some unused macros copied when trying to get a working compil…
comrumino Sep 22, 2019
0a48bc7
Replaced PyString_FromStringAndSize with PyBytes_FromStringAndSize. A…
comrumino Sep 22, 2019
9d858d9
Remove absurd redefinition of Bytes to Unicode that was copied of Git…
comrumino Sep 22, 2019
59c4fad
Remove more nonsensical define macros: PyString_FromFormat and PyStri…
comrumino Sep 22, 2019
d107361
Removed macro for PyString_Check since PyErr_SetString requires that …
comrumino Sep 22, 2019
f23e733
Remove last remaing conversion macros and cleaned seemingly ineffectu…
comrumino Sep 22, 2019
97fb2d6
Change line length limit from 80 to 120 since I do not have IBM punch…
comrumino Sep 22, 2019
0cb75c3
Ran clang-format after change of 80 to 120 char per line
comrumino Sep 22, 2019
0b55952
Removed comment block on resources for C-API and C porting after book…
comrumino Sep 22, 2019
f43e198
Changed indent width to 4 in clang-format
comrumino Sep 22, 2019
fe16ea3
Ran clang-format with new indent width of 4
comrumino Sep 22, 2019
2d38aee
Made pynids_chksum_ctl__doc__ consistent with changes to other doc st…
comrumino Sep 22, 2019
8deba13
Cleaned up PyTypeObject structs and fixed type warnings
comrumino Sep 23, 2019
4ea4506
Fixed warning about u_char for urgdata
comrumino Sep 23, 2019
b1c87a0
Replaced missed PyString_*
comrumino Sep 23, 2019
b6b6d04
Fixed addr type check in _parse_chksum_tuple
comrumino Sep 25, 2019
6c271b2
Changed nidsmodule to nids in setup.py to fix import
comrumino Sep 25, 2019
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
121 changes: 121 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
---
Language: Cpp
# BasedOnStyle: LLVM
AccessModifierOffset: -2
AlignAfterOpenBracket: Align
AlignConsecutiveAssignments: false
AlignConsecutiveDeclarations: false
AlignEscapedNewlines: Right
AlignOperands: true
AlignTrailingComments: true
AllowAllParametersOfDeclarationOnNextLine: true
AllowShortBlocksOnASingleLine: false
AllowShortCaseLabelsOnASingleLine: false
AllowShortFunctionsOnASingleLine: All
AllowShortIfStatementsOnASingleLine: false
AllowShortLoopsOnASingleLine: false
AlwaysBreakAfterDefinitionReturnType: None
AlwaysBreakAfterReturnType: None
AlwaysBreakBeforeMultilineStrings: false
AlwaysBreakTemplateDeclarations: MultiLine
BinPackArguments: true
BinPackParameters: true
BraceWrapping:
AfterClass: false
AfterControlStatement: false
AfterEnum: false
AfterFunction: false
AfterNamespace: false
AfterObjCDeclaration: false
AfterStruct: false
AfterUnion: false
AfterExternBlock: false
BeforeCatch: false
BeforeElse: false
IndentBraces: false
SplitEmptyFunction: true
SplitEmptyRecord: true
SplitEmptyNamespace: true
BreakBeforeBinaryOperators: None
BreakBeforeBraces: Attach
BreakBeforeInheritanceComma: false
BreakInheritanceList: BeforeColon
BreakBeforeTernaryOperators: true
BreakConstructorInitializersBeforeComma: false
BreakConstructorInitializers: BeforeColon
BreakAfterJavaFieldAnnotations: false
BreakStringLiterals: true
ColumnLimit: 120
CommentPragmas: '^ IWYU pragma:'
CompactNamespaces: false
ConstructorInitializerAllOnOneLineOrOnePerLine: false
ConstructorInitializerIndentWidth: 4
ContinuationIndentWidth: 4
Cpp11BracedListStyle: true
DerivePointerAlignment: false
DisableFormat: false
ExperimentalAutoDetectBinPacking: false
FixNamespaceComments: true
ForEachMacros:
- foreach
- Q_FOREACH
- BOOST_FOREACH
IncludeBlocks: Preserve
IncludeCategories:
- Regex: '^"(llvm|llvm-c|clang|clang-c)/'
Priority: 2
- Regex: '^(<|"(gtest|gmock|isl|json)/)'
Priority: 3
- Regex: '.*'
Priority: 1
IncludeIsMainRegex: '(Test)?$'
IndentCaseLabels: false
IndentPPDirectives: None
IndentWidth: 4
IndentWrappedFunctionNames: false
JavaScriptQuotes: Leave
JavaScriptWrapImports: true
KeepEmptyLinesAtTheStartOfBlocks: true
MacroBlockBegin: ''
MacroBlockEnd: ''
MaxEmptyLinesToKeep: 1
NamespaceIndentation: None
ObjCBinPackProtocolList: Auto
ObjCBlockIndentWidth: 2
ObjCSpaceAfterProperty: false
ObjCSpaceBeforeProtocolList: true
PenaltyBreakAssignment: 2
PenaltyBreakBeforeFirstCallParameter: 19
PenaltyBreakComment: 300
PenaltyBreakFirstLessLess: 120
PenaltyBreakString: 1000
PenaltyBreakTemplateDeclaration: 10
PenaltyExcessCharacter: 1000000
PenaltyReturnTypeOnItsOwnLine: 60
PointerAlignment: Right
ReflowComments: true
SortIncludes: true
SortUsingDeclarations: true
SpaceAfterCStyleCast: false
SpaceAfterTemplateKeyword: true
SpaceBeforeAssignmentOperators: true
SpaceBeforeCpp11BracedList: false
SpaceBeforeCtorInitializerColon: true
SpaceBeforeInheritanceColon: true
SpaceBeforeParens: ControlStatements
SpaceBeforeRangeBasedForLoopColon: true
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 1
SpacesInAngles: false
SpacesInContainerLiterals: true
SpacesInCStyleCastParentheses: false
SpacesInParentheses: false
SpacesInSquareBrackets: false
Standard: Cpp11
StatementMacros:
- Q_UNUSED
- QT_REQUIRE_VERSION
TabWidth: 8
UseTab: Never
...

42 changes: 23 additions & 19 deletions Example → Example.py
Original file line number Diff line number Diff line change
@@ -1,38 +1,41 @@
#! /usr/bin/env python2
#! /usr/bin/env python

# pynids Example
# $Id: Example,v 1.3 2005/01/27 04:53:45 mjp Exp $

import os, pwd
import os
import pwd
import sys
import nids

NOTROOT = "nobody" # edit to taste
end_states = (nids.NIDS_CLOSE, nids.NIDS_TIMEOUT, nids.NIDS_RESET)


def handleTcpStream(tcp):
print "tcps -", str(tcp.addr), " state:", tcp.nids_state
print("tcps -", str(tcp.addr), " state:", tcp.nids_state)
if tcp.nids_state == nids.NIDS_JUST_EST:
# new to us, but do we care?
((src, sport), (dst, dport)) = tcp.addr
print tcp.addr
print(tcp.addr)
if dport in (80, 8000, 8080):
print "collecting..."
print("collecting...")
tcp.client.collect = 1
tcp.server.collect = 1
elif tcp.nids_state == nids.NIDS_DATA:
# keep all of the stream's new data
tcp.discard(0)
tcp.discard(0)
elif tcp.nids_state in end_states:
print "addr:", tcp.addr
print "To server:"
print tcp.server.data[:tcp.server.count] # WARNING - may be binary
print "To client:"
print tcp.client.data[:tcp.client.count] # WARNING - as above
print("addr:", tcp.addr)
print("To server:")
print(tcp.server.data[:tcp.server.count]) # WARNING - may be binary
print("To client:")
print(tcp.client.data[:tcp.client.count]) # WARNING - as above


def main():

#nids.param("pcap_filter", "tcp") # bpf restrict to TCP only, note
# nids.param("pcap_filter", "tcp") # bpf restrict to TCP only, note
# libnids caution about fragments

nids.param("scan_num_hosts", 0) # disable portscan detection
Expand All @@ -45,24 +48,25 @@ def main():
nids.init()

(uid, gid) = pwd.getpwnam(NOTROOT)[2:4]
os.setgroups([gid,])
os.setgroups([gid, ])
os.setgid(gid)
os.setuid(uid)
if 0 in [os.getuid(), os.getgid()] + list(os.getgroups()):
print "error - drop root, please!"
print("error - drop root, please!")
sys.exit(1)

nids.register_tcp(handleTcpStream)
print "pid", os.getpid()
print("pid", os.getpid())

# Loop forever (network device), or until EOF (pcap file)
# Note that an exception in the callback will break the loop!
try:
nids.run()
except nids.error, e:
print "nids/pcap error:", e
except Exception, e:
print "misc. exception (runtime error in user callback?):", e
except nids.error as e:
print("nids/pcap error:", e)
except Exception as e:
print("misc. exception (runtime error in user callback?):", e)


if __name__ == '__main__':
main()
Loading