mongdb遭遇勒索,用備份進行資料恢復

cn_king發表於2023-01-29

mongdb遭遇勒索,用備份進行資料恢復

1、背景

某臺MongoDB伺服器,沒有配置使用者名稱密碼,放到公網不到一天,遭到刪庫勒索

image

All your data is a backed up. You must pay 0.05 BTC to 1K4DdqZ7sWUkhtfRqAGi7VcRck3itcNH17 48 hours for recover it. After 48 hours expiration we will sell all your data on dark markets and the database dump will be dropped from our server!You can buy bitcoin here, https://localbitcoins.com or https://buy.moonpay.io/ After paying write to us in the mail with your DB IP: rambler+1kgup@onionmail.org and you will receive a link to download your database dump.

2、查詢是否有備份,發現存在JSON格式資料備份

[
  {
    "_id": "5d17452d5548b118d86ee882",
    "A_id": "636974317259022022",
    "ArtCard_MaxSize": "0",
    "AuthorName": "xxx作者",
    "Create_time": "29/6/2019 11:02:05.902",
    "Email": "aaaa@163.com",
    "EndDate": "21/12/2025 16:00:00",
    "Goods_footer_img": "/upload/xxx.jpg",
    "Goods_header_img": "/upload/xxx.jpg",
    "StartDate": "21/12/2019 16:00:00",
    "Userhead": "/upload/xxx.jpg",
    "Username": "aaaa",
  },
 {
    "_id": "5f4b40ac7341f020cb5ebfa8",
    "A_id": "637343928761936160",
    "ArtCard_MaxSize": "0",
    "AuthorName": "xxx作者",
    "Create_time": "29/6/2019 11:02:05.902",
    "Email": "aaaa@163.com",
    "EndDate": "21/12/2025 16:00:00",
    "Goods_footer_img": "/upload/xxx.jpg",
    "Goods_header_img": "/upload/xxx.jpg",
    "StartDate": "21/12/2019 16:00:00",
    "Userhead": "/upload/xxx.jpg",
    "Username": "aaaa",
  },

3.、嘗試用“mongoimport”匯入資料

mongoimport  --uri="mongodb://user:user_pwd@127.0.0.1:27017/DBManage" -c "userInfo"  --file="C:\userInfo.json"

報錯如下:

Failed: error unmarshaling bytes on document #0: JSON decoder out of sync

原因為,匯入JSON為陣列,需要新增引數“mongoimport --jsonArray”

再次匯入:

mongoimport  --uri="mongodb://user:user_pwd@127.0.0.1:27017/DBManage" -c "userInfo" --jsonArray  --file="C:\userInfo.json"

匯入成功,但發現程式不能跑,檢視資料,發現日期格式並未匯入成預期格式:

預期的格式
{
    "A_id": NumberLong(636974317259022022),
    "Create_time" : ISODate("2019-06-24T16:27:14.537Z"),
    "StartDate" : ISODate("2019-06-24T16:00:00.537Z"),
    "EndDate" : ISODate("2019-06-24T16:00:00.537Z"),
}

資料庫裡的格式
{
    "A_id": "636974317259022022",
    "Create_time" : "24/6/2019 16:27:14.537",
    "StartDate": "21/12/2019 16:00:00",
    "EndDate": "21/12/2025 16:00:00",
}

4、PyCharm開起來,修正資料格式【Date/Long/Array】,並插入資料庫

import json
import datetime
import pymongo

#備份JSON檔案所在目錄
basePath = "C:\JSON"

myclient = pymongo.MongoClient("mongodb://127.0.0.1:27017")
mydb = myclient["DBManage"]
tabAdmin_userInfo = mydb["userInfo_Test"]

strJson = ""
with open(basePath + '\\userInfo.json', encoding='utf8') as file:
    strJson = file.read()

str_json = str(strJson)

jsonList = json.loads(str_json)


newJsonList = []
for ite in jsonList:
    for ke in ite.keys():
        # String轉成ISO Date
        if ke in ["Create_time", "StartDate", "EndDate","Sale_time"] and ite[ke] is not None:
            dateTimeValue = ite[ke]
            dateFor = None
            # "24/6/2019 16:27:14.537"  格式轉換成 ISODate("2019-06-24T16:27:14.537Z")
            if "." in dateTimeValue:
                dateFor = datetime.datetime.strptime(dateTimeValue, '%d/%m/%Y %H:%M:%S.%f')
            else:
                dateFor = datetime.datetime.strptime(dateTimeValue, '%d/%m/%Y %H:%M:%S')
            print(dateTimeValue, dateFor, dateFor.time())
            ite[ke] =dateFor

        # String轉成Long ,python3 後long和int 都為int()轉換
        if ke in ["A_id", "C_id", "New_id", "P_id","Join_userId"]:
            ite[ke] = int( ite[ke])

        # String轉成ISO Date
        if ke in ["UserType"]:
            #"636974317259022022" 轉換成 NumberLong(636974317259022022)
            ite[ke]=int(ite[ke])

        # String轉成物件
        if ke in ["Goods_Detail_image"]:
            ite[ke]=json.loads(ite[ke])

    print(ite)
    #按條插入資料庫
    insREs = tabAdmin_userInfo.insert_one(ite)
    print(insREs)

檢視userInfo_Test表資料,並和userInfo 表資料對比,userInfo_Test 表資料達到預期要求;

刪除userInfo表,將userInfo_Test表重新命名為userInfo,測試應用

應用正常執行,此次應急完成

相關文章