Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

Python 

Removing parts of the code

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.
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.

Don't fall for a scam, use a middleman!
[Safe Trading Guide] Middleman Services
(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?
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.
(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!


Possibly Related Threads...
Thread / Author Replies Views Last Post
[.NET] by ninen2001Jul 27, 2015 10:06 AM 13 292 Jul 27, 2015 10:06 AM
Last Post: ninen2001
[.NET] by xSilentNov 10, 2013 11:26 AM 121 4,964 Nov 10, 2013 11:26 AM
Last Post: iMalware
[C] by UKN0WNNov 3, 2013 09:16 PM 2 103 Nov 3, 2013 09:16 PM
Last Post: UKN0WN