Parse success, python ctypes struct to library connected

This commit is contained in:
infidel
2023-10-25 23:51:08 +07:00
parent e420442b3d
commit cfa5264b16
8 changed files with 1836 additions and 1530 deletions

View File

@@ -1,13 +1,28 @@
#!/usr/bin/env python3
from ctypes import *
#from ctypes import *
import ctypes
import _ctypes
import os
class LogData(ctypes.Structure):
_fields_ = [
("tag", ctypes.c_char_p),
("iface_in", ctypes.c_char_p),
("iface_out", ctypes.c_char_p),
("mac", ctypes.c_char_p),
("dst_ip", ctypes.c_char_p),
("src_ip", ctypes.c_char_p),
("dst_port", ctypes.c_char_p),
("src_port", ctypes.c_char_p),
("proto", ctypes.c_char_p),
("len", ctypes.c_char_p)
]
def c_parser(log_line):
so_file = "lib/parser_lib.so"
iptablesParser = CDLL(so_file)
iptablesParser.iptablesParser.argtype = c_char_p
iptablesParser.iptablesParser.restype = c_char_p
iptablesParser.lineParser.argtype = c_char_p
iptablesParser.lineParser.restype = c_char_p
@@ -39,6 +54,61 @@ def file_pointer():
break
i = i + 1
file_pointer()
def struct_process():
path = os.getcwd()
clibrary = ctypes.CDLL(os.path.join(path, 'lib/parser_lib.so'))
#param_1=("ABC", "CDE")
clibrary.main.restype = ctypes.POINTER(LogData)
call_lib = clibrary.main()
print(call_lib.contents.src_ip.decode('utf-8'))
print(call_lib.contents.dst_ip.decode('utf-8'))
print(call_lib.contents.src_port.decode('utf-8'))
print(call_lib.contents.dst_port.decode('utf-8'))
print(call_lib.contents.proto.decode('utf-8'))
print(call_lib.contents.iface_in.decode('utf-8'))
print(call_lib.contents.iface_out.decode('utf-8'))
print(call_lib.contents.len)
def line_process():
path = os.getcwd()
log_file = "example/iptables.log"
p_file = open(os.path.join(path, log_file))
p_lines = p_file.readlines()
clibrary = ctypes.CDLL(os.path.join(path, 'lib/parser_lib.so'))
clibrary.main.restype = ctypes.POINTER(LogData)
clibrary.line_parse.restype = ctypes.POINTER(LogData)
clibrary.line_parse.argtype = ctypes.c_char_p
test_val = "HERRROOOO"
for line in p_lines:
#print(line)
parser_arg = line.encode('utf-8')
call_lib = clibrary.line_parse(parser_arg)
print("-"*30)
print("SRC ",call_lib.contents.src_ip.decode('utf-8'))
print("DST ",call_lib.contents.dst_ip.decode('utf-8'))
print("LEN ",call_lib.contents.len.decode('utf-8'))
print("IFACE_IN ",call_lib.contents.iface_in.decode('utf-8'))
print("IFACE_OUT ",call_lib.contents.iface_out.decode('utf-8'))
#print("Source ",call_lib.contents.src_port.decode('utf-8'))
#print("Source ",call_lib.contents.dst_port.decode('utf-8'))
print("PROTO ",call_lib.contents.proto.decode('utf-8'))
print()
#_ctypes.dlclose(call_lib._handle)
##clibrary.main(param_1)
#print(clibrary.main().contents.src_ip)
#print(clibrary.main().contents.dst_ip)
#file_pointer()
#struct_process()
line_process()