async def Process_Cisco_LogFile_ToList(cls, queue_pc: asyncio.Queue, filename: Optional[str] = None): """ :param queue_pc: :type queue_pc: :param filename: Correct File Name (str) or None Which could raise the FileNotFound Error :return: Fresh List which stores each line of cisco firewall rules function: Process Function to Format the Cisco Log File In Cisco Log File, Need to Focus on the Line starting with "access-list" for now Version 2.x """ re_cisco_access_list_header = re.compile(r'access-list') try: with open(filename, "r") as ciscoFile: log_lineContent = ciscoFile.readline() while log_lineContent: if re.match(re_cisco_access_list_header, log_lineContent): if queue_pc.full(): await queue_pc.join() await queue_pc.put(log_lineContent) log_lineContent = ciscoFile.readline() # Put the Last label into asyncio.queue if queue_pc.full(): await queue_pc.join() await queue_pc.put("readline_complete") except IOError: config.Logger.log_fail("Error! -- Can't Find Your Cisco Config File") config.Logger.log_fail("Please Check Your Cisco Config File....") sys.exit(1)
async def Obtain_TopSec_Strategy(self, queue_pc: asyncio.Queue): # Filename is TopSec Config File Name with open(self.filename, "r") as topsecFile: # 逐行读取文本内容 log_lineContent = topsecFile.readline() while log_lineContent: entire_str = "" if re.match(self.re_topsec, log_lineContent): entire_str += log_lineContent.lstrip().replace("\n", "") log_lineContent = topsecFile.readline() while (not re.match(self.re_topsec, log_lineContent)) and log_lineContent and ( not re.match(self.re_topsec_content_stop, log_lineContent)): entire_str += log_lineContent.replace("\n", "") log_lineContent = topsecFile.readline() entire_str = entire_str.strip() + "\n" # After Process The Content of CessTop File - entire_str if queue_pc.full(): await queue_pc.join() await queue_pc.put(entire_str) else: log_lineContent = topsecFile.readline() # IF ALL Contents have processed done -> send terminate signal if queue_pc.full(): await queue_pc.join() await queue_pc.put("complete_process") return "Finished Extract Firewall Policy From Config File"
async def Process_GroupAddress_Raw_List(self, queue_groupList: asyncio.Queue): """ :param self: TopSec_Function Instance :param queue_groupList: asyncio Queue to transfer content """ try: with open(self.filename, "r") as group_info: log_lineContent = group_info.readline() while log_lineContent: entire_str = "" if re.match(self.re_topsec_group, log_lineContent): entire_str += log_lineContent.replace('\'', " ").replace("\n", "") log_lineContent = group_info.readline() while (not re.match(self.re_topsec_group_stop, log_lineContent)) and log_lineContent: entire_str += log_lineContent.replace('\'', " ").replace("\n", "") log_lineContent = group_info.readline() entire_str = entire_str.strip() + "\n" if queue_groupList.full(): await queue_groupList.join() await queue_groupList.put(entire_str) else: log_lineContent = group_info.readline() except Exception as err: print(err) exit(0) finally: if queue_groupList.full(): await queue_groupList.join() await queue_groupList.put("complete_process") return "Process_GroupAddress_Raw_List - complete"
class MessageNumberLimiter(Cog): __slots__ = "bot", "lock", "message_queue" def __init__(self, bot: Bot): self.bot = bot self.lock = Lock() self.message_queue = Queue(maxsize=25) async def delete_message_from_queue(self) -> None: message: Message = await self.message_queue.get() try: await message.delete() except Exception: pass async def update_message_queue(self, message: Message) -> None: async with self.lock: if self.message_queue.full(): await self.delete_message_from_queue() await self.message_queue.put(message) @Cog.listener() async def on_ready(self): monitoring_channel: TextChannel = self.bot.get_channel(constant.CH_LIMITED_MESSAGE_NUMBER) async for message in monitoring_channel.history(oldest_first=True): await self.update_message_queue(message) @Cog.listener() async def on_message(self, message: Message): if message.channel.id == constant.CH_LIMITED_MESSAGE_NUMBER: await self.update_message_queue(message)
def desktop_changer(changes_num: int = 5): global leave photos_queue = Queue(maxsize=changes_num) logging.basicConfig(level=logging.INFO) logging.info( "I will now go fetch %s random pictures then set them as your background", changes_num) while leave != "Yes": for j in range(changes_num): jason_data = requests.get(API_LINK).json() urllib.request.urlretrieve(jason_data['urls']['full'], "BackGround") background = Image.open("Background") random_photo = storage_path.joinpath(f'CBG{j}.jpg') background.save(random_photo) photos_queue.put_nowait(random_photo) logging.info( f"Loading background number %s" f" when i reach %s the magic will begin!", photos_queue.qsize(), changes_num) sleep(0.5) logging.info("Starting the Desktop change") sleep(1) if photos_queue.full(): while not photos_queue.empty(): logging.info("Current BG is number %s in line", photos_queue.qsize()) ctypes.windll.user32.SystemParametersInfoW( 20, 0, str(photos_queue.get_nowait()), 0) sleep(2) logging.info( "That's all for now , i shall now delete the backgrounds from your memory" ) for n in range(changes_num): os.remove(storage_path.joinpath(f"CBG{n}.jpg")) leave = input( "Would you like to quit ? Type Yes if so , Enter anything else if you wanna go again!" ) else: logging.info("Thanks for using me!") sleep(2)
class ByteInputStream(MessageInputStream, ABC): def __init__(self, maxsize=50): self.protocol: Consumer[Message] = bytes_protocol() self.queue = Queue(maxsize=maxsize) self._queue_is_full = False def continue_running(self): self.feed(b"") def queue_filled(self): pass def queue_active(self): pass def feed(self, data: bytes): for message in self.protocol.feed(data): if self.queue.full(): return self.queue.put_nowait(message) if self.queue.full(): self._queue_is_full = True self.queue_filled() return if not self.queue.full() and self._queue_is_full: self._queue_is_full = False self.queue_active() async def read(self) -> Optional[Message]: if self.queue.empty(): await self.ensure_acquired() read = (await self.queue.get()) self.continue_running() return read
async def async_feeder(dictionary: list, queue: asyncio.Queue): """ Feed your words into a queue :param dictionary: :param queue: :return: """ print("Feeding words into the pipeline") count = 0 for word in dictionary: count += 1 while queue.full(): print("Sleeping 1 seconds to allow queue to empty.") time.sleep(1) else: await queue.put(word) if count % 100000 == 0: print(f"Placed the {count}th word into the queue.")
async def Analyze_CiscoContent(cls, queue_pc: asyncio.Queue, queue_df: asyncio.Queue, each_content: Optional[str] = None): """ - Version 2.0 define this function is synchronization without async each_content is each line of config from the Cisco File - Version 2.1 Change this function nto async function: - only wait the queue to pass each line content from other coroutine - Reserve the :param each_content: each line of config file :return: Dict variable parameter - used to append the DataFrame from List - - - - """ while True: each_content = await queue_pc.get() queue_pc.task_done() if queue_pc.empty() and each_content == "readline_complete" and queue_df.empty(): await queue_df.put("readline_complete") return "Finished! - Analyse Cisco Content" strip_Content = each_content.strip().split() insert_data = dict().fromkeys(config.default_config_dict["default"].df_format, " ") # Initialize the Dictionary for key, content in zip(insert_data.keys(), strip_Content[:5]): insert_data[key] = content # Diff from TopSec, the Packet_Type in icmp if strip_Content[4] == "icmp": insert_data["Port_Type"] = "PING" # Main if strip_Content[5] == "host": insert_data["Src_Type(host/any/object-group)"] = "host" insert_data["Src_Addr"] = strip_Content[6] if strip_Content[7] == "host": insert_data["Dst_Type(host/any/object-group)"] = strip_Content[7] insert_data["Dst_Addr"] = strip_Content[8] elif strip_Content[7] == "any": insert_data["Dst_Addr"] = "any" else: insert_data["Dst_Addr"] = strip_Content[7] insert_data["Dst_Mask"] = strip_Content[8] # Src_Addr = 'any' Dst_Addr = 'any elif strip_Content[5] == "any": insert_data["Src_Addr"] = strip_Content[5] insert_data["Dst_Addr"] = strip_Content[6] # object-group elif strip_Content[5] == "object-group": insert_data["Src_Type(host/any/object-group)"] = strip_Content[5] insert_data["Src_Addr"] = strip_Content[6] if strip_Content[7] == "object-group": insert_data["Dst_Type(host/any/object-group)"] = strip_Content[7] insert_data["Dst_Addr"] = strip_Content[8] elif strip_Content[7] == "host": insert_data["Dst_Type(host/any/object-group)"] = strip_Content[7] insert_data["Dst_Addr"] = strip_Content[8] else: insert_data["Dst_Addr"] = strip_Content[7] insert_data["Dst_Mask"] = strip_Content[8] # all Src_Addr = ip / Dst_Addr else: insert_data["Src_Addr"] = strip_Content[5] insert_data["Src_Mask"] = strip_Content[6] if strip_Content[7] == "host": insert_data["Dst_Type(host/any/object-group)"] = strip_Content[7] insert_data["Dst_Addr"] = strip_Content[8] elif strip_Content[7] == "any": insert_data["Dst_Addr"] = strip_Content[7] else: insert_data["Dst_Addr"] = strip_Content[7] insert_data["Dst_Mask"] = strip_Content[8] # At Last Process with the Port_Type if 'eq' in strip_Content: insert_data["Eq"] = "eq" index = strip_Content.index("eq") if strip_Content[index + 1] == "www": insert_data["Port_Type"] = "HTTP" elif strip_Content[index + 1] == "snmptrap": insert_data["Port_Type"] = "SNMP-TRAP" else: insert_data["Port_Type"] = strip_Content[index + 1] elif strip_Content[4] == "tcp" and ("eq" not in strip_Content): insert_data["Port_Type"] = "TCPALL" # Deal With eq = "range" if 'range' in strip_Content: insert_data["Eq"] = "range" index = strip_Content.index("range") insert_data["Port_Type"] = strip_Content[index + 1] + "-" + strip_Content[index + 2] if 'log' in strip_Content: insert_data["Log"] = "log" if queue_df.full(): await queue_df.join() await queue_df.put(insert_data)
async def producer(func_ids: Iterable[int], queue: asyncio.Queue) -> None: for func_id in func_ids: await queue.put(func_id) if queue.full(): print("\nQueue is full, letting the consumer run...\n") await asyncio.sleep(1)
async def Analyze_TopSec(cls, queue_pc: asyncio.Queue, queue_df: asyncio.Queue, each_line: Optional[str] = None): while True: # Obtain the content from asyncio.queue each_line = await queue_pc.get() queue_pc.task_done() # Check IF Don't Need to Continue this loop if queue_pc.empty() and each_line == "complete_process": await queue_df.join() await queue_df.put("complete_process") return "Analyze_TopSec Function - Complete!" # Split all the content Strip_Content = each_line.replace('\n', ' ').replace('\'', ' ').strip().split() default_format = ["Command", "Category", "Insert_Word", "Type(permit/deny)", "Packet_Type"] try: # Initial return dict format input_data = dict().fromkeys(config.default_config_dict["default"].df_format, " ") # Initial Logical Processing # Default Processing in TopSec LogFile input_data[default_format[0]] = Strip_Content[0] input_data[default_format[1]] = ' '.join(Strip_Content[1:6]) input_data[default_format[3]] = Strip_Content[6] # accept/deny # Process the First Initial if Strip_Content[9] == "src": input_data["Src_Type(host/any/object-group)"] = " " input_data["Src_Addr"] = Strip_Content[10] input_data["Dst_Type(host/any/object-group)"] = " " input_data["Dst_Addr"] = Strip_Content[12] input_data["Eq"] = "eq" input_data["Port_Type"] = Strip_Content[14] if Strip_Content[-1] == "on": input_data["Log"] = "log" elif Strip_Content[9] == "slog": if Strip_Content[11] == "srcarea" and Strip_Content[13] == "dstarea": if "-" in Strip_Content[16]: input_data["Src_Addr"] = Strip_Content[16] if '/' in Strip_Content[18]: input_data["Dst_Addr"] = Strip_Content[18].split('/')[0] input_data["Dst_Mask"] = cls.exchange_maskint(Strip_Content[18].split('/')[1]) else: input_data["Dst_Addr"] = Strip_Content[18] input_data["Dst_Type(host/any/object-group)"] = "host" if Strip_Content[19] == "service": input_data["Eq"] = "eq" input_data["Port_Type"] = Strip_Content[20] if Strip_Content[-1] == "on": input_data["Log"] = "log" else: if '/' in Strip_Content[16]: input_data["Src_Addr"] = Strip_Content[16].split('/')[0] input_data["Src_Mask"] = cls.exchange_maskint(Strip_Content[16].split('/')[1]) else: input_data["Src_Addr"] = Strip_Content[16] input_data["Src_Type(host/any/object-group)"] = "host" if '/' in Strip_Content[18]: input_data["Dst_Addr"] = Strip_Content[18].split('/')[0] input_data["Dst_Mask"] = cls.exchange_maskint(Strip_Content[18].split('/')[1]) else: input_data["Dst_Addr"] = Strip_Content[18] input_data["Dst_Type(host/any/object-group)"] = "host" if Strip_Content[19] == "service": input_data["Eq"] = "eq" input_data["Port_Type"] = Strip_Content[20] if Strip_Content[-1] == "on": input_data["Log"] = "log" else: if Strip_Content[13] == "src": if '/' in Strip_Content[14]: input_data["Src_Addr"] = Strip_Content[14].split('/')[0] input_data["Src_Mask"] = cls.exchange_maskint(Strip_Content[14].split('/')[1]) else: input_data["Src_Addr"] = Strip_Content[14] input_data["Src_Type(host/any/object-group)"] = "host" input_data["Dst_Addr"] = Strip_Content[16] if Strip_Content[-1] == "on": input_data["Log"] = "log" elif Strip_Content[11] == "src": if '/' in Strip_Content[11]: input_data["Src_Addr"] = Strip_Content[11].split('/')[0] input_data["Src_Mask"] = cls.exchange_maskint(Strip_Content[11].split('/')[1]) else: input_data["Src_Addr"] = Strip_Content[11] input_data["Src_Type(host/any/object-group)"] = "host" if '/' in Strip_Content[13]: input_data["Dst_Addr"] = Strip_Content[13].split('/')[0] input_data["Dst_Mask"] = cls.exchange_maskint(Strip_Content[13].split('/')[1]) else: input_data["Dst_Addr"] = Strip_Content[13] input_data["Dst_Type(host/any/object-group)"] = "host" else: print(each_line) elif Strip_Content[9] == "srcarea" and Strip_Content[11] == "dstarea": if "-" in Strip_Content[14]: input_data["Src_Addr"] = Strip_Content[14] if '/' in Strip_Content[16]: input_data["Dst_Addr"] = Strip_Content[16].split('/')[0] input_data["Dst_Mask"] = cls.exchange_maskint(Strip_Content[16].split('/')[1]) else: input_data["Dst_Addr"] = Strip_Content[16] input_data["Dst_Type(host/any/object-group)"] = "host" if Strip_Content[17] == "service": input_data["Eq"] = "eq" input_data["Port_Type"] = Strip_Content[18] if Strip_Content[-1] == "on": input_data["Log"] = "log" else: if '/' in Strip_Content[14]: input_data["Src_Addr"] = Strip_Content[14].split('/')[0] input_data["Src_Mask"] = cls.exchange_maskint(Strip_Content[14].split('/')[1]) else: input_data["Src_Addr"] = Strip_Content[14] input_data["Src_Type(host/any/object-group)"] = "host" if '/' in Strip_Content[16]: input_data["Dst_Addr"] = Strip_Content[16].split('/')[0] input_data["Dst_Mask"] = cls.exchange_maskint(Strip_Content[16].split('/')[1]) else: input_data["Dst_Addr"] = Strip_Content[16] input_data["Dst_Type(host/any/object-group)"] = "host" if Strip_Content[17] == "service": input_data["Eq"] = "eq" input_data["Port_Type"] = Strip_Content[18] if Strip_Content[-1] == "on": input_data["Log"] = "log" elif Strip_Content[9] == "srcarea" and Strip_Content[11] == "src": if '/' in Strip_Content[12]: input_data["Src_Addr"] = Strip_Content[12].split('/')[0] input_data["Src_Mask"] = cls.exchange_maskint(Strip_Content[12].split('/')[1]) else: input_data["Src_Addr"] = Strip_Content[12] input_data["Src_Type(host/any/object-group)"] = "host" if '/' in Strip_Content[14]: input_data["Dst_Addr"] = Strip_Content[14].split('/')[0] input_data["Dst_Mask"] = cls.exchange_maskint(Strip_Content[14].split('/')[1]) else: input_data["Dst_Addr"] = Strip_Content[14] input_data["Dst_Type(host/any/object-group)"] = "host" else: print(each_line) if re.match(r'[A-Za-z]', input_data["Src_Addr"]) or '-' in input_data["Src_Addr"]: input_data["Src_Type(host/any/object-group)"] = "object-group" if re.match(r'[A-Za-z]', input_data["Dst_Addr"]) or '-' in input_data["Dst_Addr"]: input_data["Dst_Type(host/any/object-group)"] = "object-group" if 'any' == input_data["Src_Addr"]: input_data["Src_Type(host/any/object-group)"] = " " if 'any' == input_data["Dst_Addr"]: input_data["Dst_Type(host/any/object-group)"] = " " except (NameError, TypeError, RuntimeError, IndexError) as err: config.Logger.log_warning("Below Config is not Supported by this Program! Please Check...") print(each_line) finally: if queue_df.full(): await queue_df.join() await queue_df.put(input_data)