1

我想在我的 FritzBox 路由器上更改访客 WIFI 的密码。为了连接到 FritzBox,我使用 Python 中的 FritzConnection 库。一般来说,FritzConnection 可以工作:我可以从 FritzBox 读取信息,也可以启用或禁用访客 WIFI。但是,我在更改密码时遇到了麻烦。不过,我可以读取密码。

这是我到目前为止所做的:

#!/usr/bin/python3
from fritzconnection import FritzConnection
import pprint

pp = pprint.PrettyPrinter(indent=2)
fc=FritzConnection(address='192.168.2.1', password='XYZ', use_tls=True)

#switch guest WIFI off and on again - this works
state = fc.call_action('WLANConfiguration:3', 'GetInfo')
pp.pprint(state)
print("Disable WLAN")
fc.call_action('WLANConfiguration3',
               'SetEnable',
               arguments={'NewEnable':0})
state = fc.call_action('WLANConfiguration:3', 'GetInfo')
pp.pprint(state)
input("Press Enter to continue...")
print("Enable WLAN")
fc.call_action('WLANConfiguration3',
               'SetEnable',
               arguments=NewEnable=1)
state = fc.call_action('WLANConfiguration:3', 'GetInfo')
pp.pprint(state)

此代码段可关闭和打开 WIFI,效果很好。请注意,我使用了 2 种不同的参数传递方法来启用和禁用。两者都有效。

现在,在更改密码时,我首先读取安全密钥(有效),然后尝试重新设置它们(无效)。为了阅读,我执行以下操作:

securityKeys = fc.call_action('WLANConfiguration:3', 'GetSecurityKeys')
pp.pprint(securityKeys)

先写我试过:

fc.call_action('WLANConfiguration:3',
               'SetSecurityKeys',
               NewKeyPassphrase='BetterPassword')

这导致

fritzconnection.core.exceptions.FritzArgumentError: UPnPError:
errorCode: 402
errorDescription: Invalid Args

我也试过

fc.call_action('WLANConfiguration3',
               'SetSecurityKeys',
               arguments={'NewKeyPassphrase':'MeinPasswortIstBesser'})

fc.call_action('WLANConfiguration:3',
               'SetSecurityKeys',
               NewWEPKey0='',
               NewWEPKey1='',
               NewWEPKey2='',
               NewWEPKey3='',
               NewPreSharedKey='',
               NewKeyPassphrase="MeinPasswortIstBesser")

securityKeys['NewKeyPassphrase']='MeinPasswortIstBesser'
fc.call_action('WLANConfiguration3',
               'SetSecurityKeys',
               arguments=securityKeys)

但结果总是一样的。显然我对参数做错了,但是我不能说它应该是什么。使用 FritzConnection CLI 工具告诉我界面:

fritzconnection --ip-address 192.168.2.1 -A 'WLANConfiguration3' 'SetSecurityKeys'

fritzconnection v1.3.4
FRITZ!Box 7590 at http://192.168.2.1
FRITZ!OS: 7.20


Service:            WLANConfiguration3
Action:             SetSecurityKeys
Parameters:

    Name                                  direction     data type

    NewWEPKey0                            -> in         string
    NewWEPKey1                            -> in         string
    NewWEPKey2                            -> in         string
    NewWEPKey3                            -> in         string
    NewPreSharedKey                       -> in         string
    NewKeyPassphrase                      -> in         string

据我了解,我服务于该界面。

关于我在这里做错了什么的任何想法?

4

1 回答 1

0

FritzConnction 存在缺陷。可以在这里找到解决方案:

https://github.com/kbr/fritzconnection/issues/66

s:本质是FritzConnection的模板前缀太多 。特别 是s:NewWEPKey0s:NewWEPKey1、和。s:NewWEPKey2_ 从身体上取下来就可以了。s:NewWEPKey3s:NewPreSharedKeys:NewKeyPassphrases:

我做了以下肮脏的黑客攻击来验证这篇论文:在soaper.py我添加到第 226 行下面(这是在方法execute()中,以下代码片段:

    if 'NewKeyPassphrase' in body:
        print (body)
        print ('replacing......................................')
        body = body.replace('s:NewWEPKey0', 'NewWEPKey0')
        body = body.replace('s:NewWEPKey1', 'NewWEPKey1')
        body = body.replace('s:NewWEPKey2', 'NewWEPKey2')
        body = body.replace('s:NewWEPKey3', 'NewWEPKey3')
        body = body.replace('s:NewPreSharedKey', 'NewPreSharedKey')
        body = body.replace('s:NewKeyPassphrase', 'NewKeyPassphrase')
        print (body)

s:在那种情况下,这会从那个身体中移除。这工作正常,密码被更改。

所以我的脚本如下所示:

from fritzconnection import FritzConnection
import pprint

pp = pprint.PrettyPrinter(indent=2)
fc=FritzConnection(address='192.168.2.1', user='FritzConnection', password='Dr3QL78ZYG58Nn98GVsx', use_tls=True)

securityKeys = fc.call_action('WLANConfiguration:3', 'GetSecurityKeys')
pp.pprint(securityKeys)

securityKeys['NewKeyPassphrase']='MeinPasswortIstUnfassbarGut'
pp.pprint(securityKeys)
fc.call_action('WLANConfiguration3',
               'SetSecurityKeys',
               arguments=securityKeys)

securityKeys = fc.call_action('WLANConfiguration:3', 'GetSecurityKeys')
pp.pprint(securityKeys)
于 2021-07-05T15:04:53.970 回答