Hack Forums
[Python] Removing parts of the code - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [Python] Removing parts of the code (/showthread.php?tid=6260304)



Removing parts of the code - jasonnix - Jan 6, 2024

Hello,
I found this article. The following code checks both the username and the MAC address:
Code
#!/usr/bin/python3

import re
import sys


db_file = '/etc/openvpn/db.txt'
log_file = '/var/log/openvpn/openvpn.log'
regex_mac = 'IV_HWADDR=(.*)'
regex_username = 'depth=0, CN=(.*)'
login = False

logs_username_mac_list = []


with open(log_file, 'r') as log:
    lines = log.readlines()
    # read only latest 100 lines
    last_50_lines = lines[-50:]
    # iterate latest 50 lines
    for line in last_50_lines:


        match_mac = re.search(regex_mac, line)
        if match_mac:
            log_mac = match_mac.group(1)
            print(log_mac)
            # on match, add it to list
            logs_username_mac_list.append(log_mac)

        match_username = re.search(regex_username, line)
        if match_username:
            log_username = match_username.group(1)
            print(log_username)
            logs_username_mac_list.append(log_username)

# fetch username and mac address from database
with open(db_file, 'r') as db:
    for line in db.readlines():
      
        splitter = line.split('-', 1)
        # 0 index is username, removing newline
        db_username = (splitter[0]).rstrip("\n")
        print(db_username)
        # 1 index is mac, removing newline
        db_mac = (splitter[1]).rstrip("\n")
        print(db_mac)
        if db_username in logs_username_mac_list:
            user_index = logs_username_mac_list.index(db_username) - 1
            print(user_index)
            mac_index = logs_username_mac_list[user_index]
            print(user_index)
            # if log mac matches db_mac
            if mac_index == db_mac:
                print("true")
                login = True
                print(login)

if login:
    sys.exit(0)
else:
    sys.exit(1)
I just want the mac address to be checked. I changed the code as follows:
Code
#!/usr/bin/python3

import re
import sys


db_file = '/etc/openvpn/db.txt'
log_file = '/var/log/openvpn/openvpn.log'
regex_mac = 'IV_HWADDR=(.*)'
login = False

logs_mac_list = []


with open(log_file, 'r') as log:
    lines = log.readlines()
    # read only latest 100 lines
    last_50_lines = lines[-50:]
    # iterate latest 50 lines
    for line in last_50_lines:


        match_mac = re.search(regex_mac, line)
        if match_mac:
            log_mac = match_mac.group(1)
            print(log_mac)
            # on match, add it to list
            logs_mac_list.append(log_mac)

# fetch username and mac address from database
with open(db_file, 'r') as db:
    for line in db.readlines():
      
        splitter = line.split('-', 1)
        # 1 index is mac, removing newline
        db_mac = (splitter[1]).rstrip("\n")
        print(db_mac)
        mac_index = logs_mac_list[user_index]
        print(user_index)
        # if log mac matches db_mac
        if mac_index == db_mac:
            print("true")
            login = True
            print(login)

if login:
    sys.exit(0)
else:
    sys.exit(1)
Is it OK?

Thank you.


RE: Removing parts of the code - Charlie Sheen - Jan 6, 2024

The comment says "read only latest 100 lines", but the code actually reads the last 50 lines (last_50_lines = lines[-50:]). You should correct the comment

Also

The variable user_index is used in the line mac_index = logs_mac_list[user_index] but is not initialized or defined anywhere in the script. This will cause a NameError at runtime. You need to define user_index or revise the logic to correctly index logs_mac_list.


RE: Removing parts of the code - jasonnix - Jan 6, 2024

(Jan 6, 2024 04:29 AM)Charlie Sheen Wrote:  The comment says "read only latest 100 lines", but the code actually reads the last 50 lines (last_50_lines = lines[-50:]). You should correct the comment

Also

The variable user_index is used in the line mac_index = logs_mac_list[user_index] but is not initialized or defined anywhere in the script. This will cause a NameError at runtime. You need to define user_index or revise the logic to correctly index logs_mac_list.

Hello,
Thank you so much for your reply.
Can you change the code for MAC address?


RE: Removing parts of the code - snoopy717 - Jan 8, 2024

This help u

Code
with open(log_file, 'r') as log:
    lines = log.readlines()
    # read only latest 100 lines
    last_50_lines = lines[-50:]
    # iterate latest 50 lines
    for line in last_50_lines:


        match_mac = re.search(regex_mac, line)
        if match_mac:
            log_mac = match_mac.group(1)
            print(log_mac)
            # on match, add it to list
            logs_username_mac_list.append(log_mac)

with open(db_file, 'r') as file:
        # Read the content of the file
        file_content = file.read()

for line in logs_username_mac_list:
     if line in file_content:
         print(db_username)

if you have any question, PM me.


RE: Removing parts of the code - jasonnix - Jan 19, 2024

(Jan 8, 2024 05:24 AM)snoopy717 Wrote:  This help u

Code
with open(log_file, 'r') as log:
    lines = log.readlines()
    # read only latest 100 lines
    last_50_lines = lines[-50:]
    # iterate latest 50 lines
    for line in last_50_lines:


        match_mac = re.search(regex_mac, line)
        if match_mac:
            log_mac = match_mac.group(1)
            print(log_mac)
            # on match, add it to list
            logs_username_mac_list.append(log_mac)

with open(db_file, 'r') as file:
        # Read the content of the file
        file_content = file.read()

for line in logs_username_mac_list:
     if line in file_content:
         print(db_username)

if you have any question, PM me.

Hello,
Thank you so much for your reply.
If you read the original code, you will see the following lines that are related to allowing the user to log in:
Code
...
if mac_index == db_mac:
                print("true")
                login = True
                print(login)

if login:
    sys.exit(0)
else:
    sys.exit(1)
I think you forgot it!