Nws pib muab Yealink T19 + dynamic chaw nyob phau ntawv

Thaum kuv los ua haujlwm rau lub tuam txhab no, kuv twb muaj qee qhov chaw khaws ntaub ntawv ntawm IP khoom siv, ntau lub servers nrog lub hnub qub thiab thaj ua rau hauv daim ntawv ntawm FreeBPX. Tsis tas li ntawd, ib qho analogue PBX Samsung IDCS500 ua hauj lwm nyob rau hauv parallel thiab, feem ntau, yog lub ntsiab kev sib txuas lus system nyob rau hauv lub tuam txhab; IP xov tooj ua hauj lwm tsuas yog rau cov muag khoom department. Thiab txhua yam yuav tau txuas ntxiv ua noj zoo li no, tab sis muaj ib hnub zoo ib qho kev txiav txim kom hloov txhua tus neeg mus rau IP xov tooj, cov hnub kawg tau pom zoo, cov khoom siv tau yuav, thiab cov phiaj xwm hloov chaw lag luam mus rau xyoo pua 21st tau pib ua tiav.
Thawj qhov uas pib txhawj xeeb hauv qhov xwm txheej zoo li no yog cov xov tooj loj zuj zus uas yuav tsum tau tswj hwm qee yam, qhov thib ob uas txhawj xeeb heev yog phau ntawv xov tooj. Yog tias Endpoint Manager tuaj yeem pab peb nrog thawj tus (uas, los ntawm txoj kev, raug txiav tawm ntawm qhov tseeb versions of FreePBX), ces qee cov lus nug tshwm sim nrog phau ntawv:

  • Ua ntej, yuav ua li cas kom ntseeg tau nws qhov tseeb thaum qhov chaw / fluidity ntawm cov neeg siv hloov pauv tas li?
  • Qhov thib ob, yuav ua li cas kom depersonalize xov tooj tag nrho. Thiab tsis sau npe rau txhua lub sijhawm?

Qhov teeb meem tau nthuav dav, qhov kev daws teeb meem tsis siv sijhawm ntev los txog. Tam sim no kuv yuav muab tag nrho cov npe, thiab tom qab ntawd peb yuav saib nws hauv kev txiav txim.

from scapy.all import sniff
from scapy.layers.inet import IP
import mysql.connector
import ldap
import getpass
import tftpy
import requests
import os
import time
from string import replace

def conn_ldap(login):
    ad = ldap.initialize('ldap://***.local')
    ad.simple_bind_s('voip@***.local', 'password')
    basedn = 'OU=IT,DC=***,DC=LOCAL'
    basedn_user = 'OU=***,OU=***,DC=***,DC=LOCAL'
    scope = ldap.SCOPE_SUBTREE
    filterexp = "(&(sAMAccountName=" + login + ")(ObjectClass=person))"
    filterexp2 = "(&(ObjectClass=organizationUnit))"
    attrlist = ['cn']
    attrlist2 = ['OU']
    search = ad.search_s(basedn, scope, filterexp, attrlist)
    adname = search[0][1]['cn'][0].decode('utf-8')
    if adname == ' ':
        search = ad.search_s(basedn_user, scope, filterexp2, attrlist2)
        for i in range(1, len(search)+1):
            group = search[i][1]['ou'][0]
            basedn_user2 = 'OU='+group+','+basedn_user
            search = ad.search_s(basedn_user2, scope, filterexp, attrlist)
            adname = search[0][1]['cn'][0].decode('utf-8')
            if adname != ' ':
                return adname
        adname = search[0][1]['cn'][0].decode('utf-8')
    ad.unbind_s()
    return adname


def tftp_file_change(config,place,adname,current_account,current_account_password):

    client = tftpy.TftpClient("192.168.0.3", 69)
    client.download('template.cfg', place)
    fileread = open(place, 'r')
    line = fileread.readlines()
    fileread.close()
    line[5] = (('account.1.label = ').encode('utf-8') + adname.encode('utf-8') + 'n')
    line[2] = (('account.1.auth_name = ').encode('utf-8') + current_account.encode('utf-8') + 'n')
    line[3] = (('account.1.display_name = ').encode('utf-8') + current_account.encode('utf-8') + 'n')
    line[6] = (('account.1.password = ').encode('utf-8') + current_account_password[0][0] + 'n')
    filewrite = open(place, 'w')
    for i in line:
      filewrite.write(i)
    filewrite.close()
    print place
    print config
    client.upload(config,place)


def get_phone_inform(ipaddr):
    fileconf = requests.get('http://admin:admin@'+ipaddr+'/servlet?phonecfg=get[&accounts=1]')
    conf = fileconf.text.split('|')
    current_account = conf[2]
    return current_account


def sniff_frame():
    pcapf = sniff(count=1, timeout=70, filter="dst host 192.168.0.3 and port 5060")
    if len(pcapf) == 0:
        exit()
    frame = pcapf[0]
    macaddr = frame.src
    print macaddr[:8]
    if macaddr[:8] != '80:5e:c0':
        exit()
    ipaddr = frame[0][IP].src
    return macaddr, ipaddr


def conn_mysql(query,fquery,macaddr,qwery2):
    connect = mysql.connector.connect(host='192.168.0.3', database='voip', user='voip_wr', password='***')
    cursor = connect.cursor()
    cursor.execute(fquery)
    state = cursor.fetchall()
    state = bool(state[0][0])
    if state == True:
        cursor.execute(qwery2)
        connect.commit()
        connect.close()
    else:
        cursor.execute(query)
        connect.commit()
        connect.close()


def check_account(current_account):
    connect = mysql.connector.connect(host='192.168.0.3', database='asterisk', user='voip_wr', password='***')
    cursor = connect.cursor()
    qwery = 'select data from sip where id=' + current_account + ' and keyword="secret";'
    cursor.execute(qwery)
    password = cursor.fetchall()
    if password == ' ':
        exit()
    else:
        return password


if __name__ == '__main__':
    macaddr, ipaddr = sniff_frame()
    current_account = get_phone_inform(ipaddr)
    current_account_password = check_account(current_account)
    macaddr = macaddr.replace(':', '')
    ipaddr = ipaddr.decode('utf-8')
    adname = conn_ldap(getpass.getuser())
    query = 'INSERT INTO station (mac, ip, name, number) VALUES (' + '"' + macaddr + '",' + '"' + ipaddr + '",' + '"' + adname + '",' + '"' + get_phone_inform(ipaddr) + '"' + ')'
    qwery2 = 'UPDATE station SET ip=' + '"' + ipaddr + '"' + ', name=' + '"' + adname + '"' + ', number=' + '"' + get_phone_inform(ipaddr) + '"' + ' WHERE mac=' + '"' + macaddr + '"'
    fquery = 'SELECT EXISTS(SELECT mac FROM voip.station WHERE mac=' + '"' + macaddr + '")'
    query = query.encode('utf-8')
    fquery = fquery.encode('utf-8')
    config = macaddr + '.cfg'
    place = os.path.expanduser("~") + "" + "AppDataLocal" + config
    conn_mysql(query,fquery,macaddr,qwery2)
    tftp_file_change(config,place,adname,current_account,current_account_password)
    requests.get('http://admin:admin@'+ipaddr+'/cgi-bin/ConfigManApp.com?key=AutoP')
    requests.get('http://admin:admin@'+ipaddr+'/cgi-bin/ConfigManApp.com?key=Reboot')

Qhov kev zov me nyuam khiav ntawm tus neeg siv lub computer thiab ua haujlwm tau hais tias lub khoos phis tawj txuas nrog lub network ntawm lub xov tooj, txij li Yealink T19 tsis tuaj yeem ua haujlwm raws li lub rooj vag.

Ua ntej, peb yuav tsum nkag siab tias nws txuas nrog? thiab dab tsi mac thiab ip peb lub xov tooj muaj.

def sniff_frame():
    pcapf = sniff(count=1, timeout=70, filter="dst host 192.168.0.3 and port 5060")
    if len(pcapf) == 0:
        exit()
    frame = pcapf[0]
    macaddr = frame.src
    print macaddr[:8]
    if macaddr[:8] != '80:5e:c0':
        exit()
    ipaddr = frame[0][IP].src
    return macaddr, ipaddr

Ntawm no peb siv sniff muaj nuj nqi los ntawm lub moj khaum scapy, nrog rau nws cov kev pab peb tau txais ib tug predetermined udp pob ntawv, tos 70 vib nas this thiab yog hais tias peb tsis ntes dab tsi, peb tawm.

count=1, timeout=70, filter="dst host 192.168.0.3 and port 5060"

Tom ntej no, peb xyuas kom meej tias cov cuab yeej yog Yealink thiab xa rov qab qhov tsim nyog qhov tseem ceeb (ip thiab mac).

Siv qhov kev thov tshwj xeeb, peb pom cov nyiaj tam sim no hauv xov tooj. Txhawm rau ua qhov no, qhov kev teeb tsa tam sim no rub tawm los ntawm lub xov tooj thiab parsed.

def get_phone_inform(ipaddr):
    fileconf = requests.get('http://admin:admin@'+ipaddr+'/servlet?phonecfg=get[&accounts=1]')
    conf = fileconf.text.split('|')
    current_account = conf[2]
    return current_account

Nrhiav tus password rau tus account no. Ua li no, peb tig mus rau lub asterisk.sip lub rooj thiab cov ntaub ntawv teb hauv nws.

def check_account(current_account):
    connect = mysql.connector.connect(host='192.168.0.3', database='asterisk', user='voip_wr', password='***')
    cursor = connect.cursor()
    qwery = 'select data from sip where id=' + current_account + ' and keyword="secret";'
    cursor.execute(qwery)
    password = cursor.fetchall()
    if password == ' ':
        exit()
    else:
        return password

Zoo, rau theem kawg peb txuas rau ldap AD thiab siv sAMAccountName tau los ntawm kev ua haujlwm getpass.getuser() coj tus cn ntawm tus neeg siv tam sim no (uas feem ntau muaj tus neeg siv lub npe tag nrho).

def conn_ldap(login):
    ad = ldap.initialize('ldap://***.local')
    ad.simple_bind_s('voip@***.local', 'password')
    basedn = 'OU=***,DC=***,DC=LOCAL'
    basedn_user = 'OU=***,OU=***,DC=***,DC=LOCAL'
    scope = ldap.SCOPE_SUBTREE
    filterexp = "(&(sAMAccountName=" + login + ")(ObjectClass=person))"
    filterexp2 = "(&(ObjectClass=organizationUnit))"
    attrlist = ['cn']
    attrlist2 = ['OU']
    search = ad.search_s(basedn, scope, filterexp, attrlist)
    adname = search[0][1]['cn'][0].decode('utf-8')
    if adname == ' ':
        search = ad.search_s(basedn_user, scope, filterexp2, attrlist2)
        for i in range(1, len(search)+1):
            group = search[i][1]['ou'][0]
            basedn_user2 = 'OU='+group+','+basedn_user
            search = ad.search_s(basedn_user2, scope, filterexp, attrlist)
            adname = search[0][1]['cn'][0].decode('utf-8')
            if adname != ' ':
                return adname
        adname = search[0][1]['cn'][0].decode('utf-8')
    ad.unbind_s()
    return adname

Peb txuas mus rau lub rooj tsim ua ntej hauv cov ntaub ntawv (Kuv tsim nws muaj) thiab nkag mus rau txhua yam uas peb tau kawm, xws li: ip, mac, username.

def conn_mysql(query,fquery,macaddr,qwery2):
    connect = mysql.connector.connect(host='192.168.0.3', database='voip', user='voip_wr', password='***')
    cursor = connect.cursor()
    cursor.execute(fquery)
    state = cursor.fetchall()
    state = bool(state[0][0])
    if state == True:
        cursor.execute(qwery2)
        connect.commit()
        connect.close()
    else:
        cursor.execute(query)
        connect.commit()
        connect.close()

Peb tuaj yeem tso tseg ntawm no, vim tias peb twb tau tsim phau ntawv chaw nyob dynamic, koj tuaj yeem nug, tab sis kuv tau mus ntxiv thiab ntxiv qhov pib muab khoom siv ntawm no.

Txhawm rau ua qhov no, lub template configuration yog rub tawm los ntawm pre-configured tftp server, rau hauv uas peb hloov peb thiab txuag nws li mac.cfg. Ntawd yog, rau Yealink muaj ob hom kev teeb tsa, ib qho yog thoob ntiaj teb, thiab qhov thib ob siv rau lub xov tooj tshwj xeeb thiab yuav tsum yog ntawm daim ntawv mac_phone.cfg

Tom qab tag nrho cov kev hloov pauv hauv cov ntaub ntawv thiab txuag nws rov qab mus rau tftp server, peb muab cov lus txib rau lub xov tooj kom muab thiab rov pib lub cuab yeej.

def tftp_file_change(config,place,adname,current_account,current_account_password):

    client = tftpy.TftpClient("192.168.0.3", 69)
    client.download('template.cfg', place)
    fileread = open(place, 'r')
    line = fileread.readlines()
    fileread.close()
    line[5] = (('account.1.label = ').encode('utf-8') + adname.encode('utf-8') + 'n')
    line[2] = (('account.1.auth_name = ').encode('utf-8') + current_account.encode('utf-8') + 'n')
    line[3] = (('account.1.display_name = ').encode('utf-8') + current_account.encode('utf-8') + 'n')
    line[6] = (('account.1.password = ').encode('utf-8') + current_account_password[0][0] + 'n')
    filewrite = open(place, 'w')
    for i in line:
      filewrite.write(i)
    filewrite.close()
    print place
    print config
    client.upload(config,place)

requests.get('http://admin:admin@'+ipaddr+'/cgi-bin/ConfigManApp.com?key=AutoP')
requests.get('http://admin:admin@'+ipaddr+'/cgi-bin/ConfigManApp.com?key=Reboot')

Tom qab rebooting lub cuab yeej, peb tau txais peb lub npe tag nrho ntawm lub xov tooj screen + ib txwm ua kom raug sau cov ntawv nyob hauv daim ntawv ntawm cov ntaub ntawv, tom qab ntawd txhua yam uas tseem tshuav yog ntxiv XML thiab PHP me ntsis kom pom cov ntsiab lus dynamically. Muaj ntau qhov piv txwv zoo li no, txawm tias YEALINK nws tus kheej muaj lawv.

PS: Rau kev ua kom muaj zog ntau dua, koj tuaj yeem txav cov chaw tseem ceeb (kuj hloov pauv) mus rau hauv cov ntaub ntawv cais.

Tau qhov twg los: www.hab.com

Ntxiv ib saib