Last active
August 29, 2015 14:05
-
-
Save John-Lin/9408ab716df57dbe32ca to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| import os | |
| import sys | |
| import time | |
| import socket | |
| import logging | |
| logging.basicConfig(level=logging.INFO) | |
| logger = logging.getLogger(__name__) | |
| SOCKFILE = "/tmp/snort_alert" | |
| BUFSIZE = 65863 | |
| IP = '127.0.0.1' | |
| PORT = 51234 | |
| # TODO: TLS/SSL wrapper for socket | |
| class SnortListener(): | |
| def __init__(self): | |
| self.unsock = None | |
| self.nwsock = None | |
| def start_send(self): | |
| self.nwsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
| try: | |
| self.nwsock.connect((IP, PORT)) | |
| except Exception, e: | |
| logger.info("Network socket connection error: %s" % e) | |
| sys.exit(1) | |
| def start_recv(self): | |
| if os.path.exists(SOCKFILE): | |
| os.unlink(SOCKFILE) | |
| self.unsock = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM) | |
| self.unsock.bind(SOCKFILE) | |
| logger.info("Unix Domain Socket listening...") | |
| self.recv_loop() | |
| def recv_loop(self): | |
| logger.info("Start the network socket client....") | |
| self.start_send() | |
| while True: | |
| data = self.unsock.recv(BUFSIZE) | |
| time.sleep(0.5) | |
| if data: | |
| logger.debug("Send {0} bytes of data.".format | |
| (sys.getsizeof(data))) | |
| # data == 65900 byte | |
| self.tcp_send(data) | |
| else: | |
| pass | |
| def tcp_send(self, data): | |
| self.nwsock.sendall(data) | |
| logger.info("Send the alert messages to Ryu.") | |
| if __name__ == '__main__': | |
| server = SnortListener() | |
| server.start_recv() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment