source

잘못된 바이트 수 길이로 인해 손상된 직렬화된 문자열을 복구하려면 어떻게 해야 합니까?

nicesource 2022. 12. 24. 17:23
반응형

잘못된 바이트 수 길이로 인해 손상된 직렬화된 문자열을 복구하려면 어떻게 해야 합니까?

이미지 업로드 플러그인과 함께 Hotaru CMS를 사용하고 있는데, 이미지를 게시물에 첨부하려고 하면 이 오류가 발생하며, 그렇지 않으면 오류가 없습니다.

unserialize() [function.unserialize] :오프셋 오류

문제의 코드(오류 포인트는 **)입니다.

/**
     * Retrieve submission step data
     *
     * @param $key - empty when setting
     * @return bool
     */
    public function loadSubmitData($h, $key = '')
    {
        // delete everything in this table older than 30 minutes:
        $this->deleteTempData($h->db);

        if (!$key) { return false; }

        $cleanKey = preg_replace('/[^a-z0-9]+/','',$key);
        if (strcmp($key,$cleanKey) != 0) {
            return false;
        } else {
            $sql = "SELECT tempdata_value FROM " . TABLE_TEMPDATA . " WHERE tempdata_key = %s ORDER BY tempdata_updatedts DESC LIMIT 1";
            $submitted_data = $h->db->get_var($h->db->prepare($sql, $key));
            **if ($submitted_data) { return unserialize($submitted_data); } else { return false; }** 
        }
    }

테이블의 데이터입니다만, 엔드 비트에 이미지 정보가 기재되어 있습니다.저는 PHP 전문가가 아니기 때문에, 여러분은 어떻게 생각하십니까?

tempdata_value:

a:10:{s:16:"submit_editorial";b:0;s:15:"submit_orig_url";s:13:"www.bbc.co.uk";s:12:"submit_title";s:14:"No title found";s:14:"submit_content";s:12:"dnfsdkfjdfdf";s:15:"submit_category";i:2;s:11:"submit_tags";s:3:"bbc";s:9:"submit_id";b:0;s:16:"submit_subscribe";i:0;s:15:"submit_comments";s:4:"open";s:5:"image";s:19:"C:fakepath100.jpg";}

편집: 직렬화 비트를 찾은 것 같습니다...

/**
     * Save submission step data
     *
     * @return bool
     */
    public function saveSubmitData($h)
    {
        // delete everything in this table older than 30 minutes:
        $this->deleteTempData($h->db);

        $sid = preg_replace('/[^a-z0-9]+/i', '', session_id());
        $key = md5(microtime() . $sid . rand());
        $sql = "INSERT INTO " . TABLE_TEMPDATA . " (tempdata_key, tempdata_value, tempdata_updateby) VALUES (%s,%s, %d)";
        $h->db->query($h->db->prepare($sql, $key, serialize($h->vars['submitted_data']), $h->currentUser->id));
        return $key;
    }

unserialize() [function.unserialize]: Error at offset이 되다invalid serialization data가 무효이기

퀵픽스

수 있는 건 할 수 있어요.recalculating the length

현재 직렬화된 데이터

$data = 'a:10:{s:16:"submit_editorial";b:0;s:15:"submit_orig_url";s:13:"www.bbc.co.uk";s:12:"submit_title";s:14:"No title found";s:14:"submit_content";s:12:"dnfsdkfjdfdf";s:15:"submit_category";i:2;s:11:"submit_tags";s:3:"bbc";s:9:"submit_id";b:0;s:16:"submit_subscribe";i:0;s:15:"submit_comments";s:4:"open";s:5:"image";s:19:"C:fakepath100.jpg";}';

재계산하지 않은 예

var_dump(unserialize($data));

산출량

Notice: unserialize() [function.unserialize]: Error at offset 337 of 338 bytes

재계산

$data = preg_replace('!s:(\d+):"(.*?)";!e', "'s:'.strlen('$2').':\"$2\";'", $data);
var_dump(unserialize($data));

산출량

array
  'submit_editorial' => boolean false
  'submit_orig_url' => string 'www.bbc.co.uk' (length=13)
  'submit_title' => string 'No title found' (length=14)
  'submit_content' => string 'dnfsdkfjdfdf' (length=12)
  'submit_category' => int 2
  'submit_tags' => string 'bbc' (length=3)
  'submit_id' => boolean false
  'submit_subscribe' => int 0
  'submit_comments' => string 'open' (length=4)
  'image' => string 'C:fakepath100.jpg' (length=17)

권장사항 ..i

이런 종류의 빠른 수정을 사용하는 대신... 질문을 업데이트 할 것을 권장합니다.

  • 데이터 시리얼화 방법

  • 절약 방법..

================================ 편집 1 ===============================

에러

는 큰따옴표 생성되었습니다." 작은따옴표 " " " 를 사용합니다.' 때문에C:\fakepath\100.pngC:fakepath100.jpg

오류를 수정하려면

필요가 $h->vars['submitted_data']] ( Quiet From)을해 주세요.')

교체하다

 $h->vars['submitted_data']['image'] = "C:\fakepath\100.png" ;

와 함께

 $h->vars['submitted_data']['image'] = 'C:\fakepath\100.png' ;

추가 필터

serialize를 호출하기 전에 이 간단한 필터를 추가할 수도 있습니다.

function satitize(&$value, $key)
{
    $value = addslashes($value);
}

array_walk($h->vars['submitted_data'], "satitize");

UTF Characters가 있는 경우,

 $h->vars['submitted_data'] = array_map("utf8_encode",$h->vars['submitted_data']);

향후 시리얼화된 데이터의 문제를 검출하는 방법

  findSerializeError ( $data1 ) ;

산출량

Diffrence 9 != 7
    -> ORD number 57 != 55
    -> Line Number = 315
    -> Section Data1  = pen";s:5:"image";s:19:"C:fakepath100.jpg
    -> Section Data2  = pen";s:5:"image";s:17:"C:fakepath100.jpg
                                            ^------- The Error (Element Length)

findSerializeError ★★

function findSerializeError($data1) {
    echo "<pre>";
    $data2 = preg_replace ( '!s:(\d+):"(.*?)";!e', "'s:'.strlen('$2').':\"$2\";'",$data1 );
    $max = (strlen ( $data1 ) > strlen ( $data2 )) ? strlen ( $data1 ) : strlen ( $data2 );

    echo $data1 . PHP_EOL;
    echo $data2 . PHP_EOL;

    for($i = 0; $i < $max; $i ++) {

        if (@$data1 {$i} !== @$data2 {$i}) {

            echo "Diffrence ", @$data1 {$i}, " != ", @$data2 {$i}, PHP_EOL;
            echo "\t-> ORD number ", ord ( @$data1 {$i} ), " != ", ord ( @$data2 {$i} ), PHP_EOL;
            echo "\t-> Line Number = $i" . PHP_EOL;

            $start = ($i - 20);
            $start = ($start < 0) ? 0 : $start;
            $length = 40;

            $point = $max - $i;
            if ($point < 20) {
                $rlength = 1;
                $rpoint = - $point;
            } else {
                $rpoint = $length - 20;
                $rlength = 1;
            }

            echo "\t-> Section Data1  = ", substr_replace ( substr ( $data1, $start, $length ), "<b style=\"color:green\">{$data1 {$i}}</b>", $rpoint, $rlength ), PHP_EOL;
            echo "\t-> Section Data2  = ", substr_replace ( substr ( $data2, $start, $length ), "<b style=\"color:red\">{$data2 {$i}}</b>", $rpoint, $rlength ), PHP_EOL;
        }

    }

}

데이터베이스에 저장하는 더 나은 방법

$toDatabse = base64_encode(serialize($data));  // Save to database
$fromDatabase = unserialize(base64_decode($data)); //Getting Save Format 

코멘트를 할 만한 평판이 없기 때문에, 위의 「정답」을 사용하고 있는 분에게 보여 주셨으면 합니다.

php 5.5 이후 preg_replace()의 /e 수식자는 완전히 폐지되어 위의 preg_match가 에러 아웃됩니다.php 문서에서는 preg_match_callback을 대신 사용할 것을 권장합니다.

상기 preg_match 제안 대신 다음 솔루션을 찾으십시오.

$fixed_data = preg_replace_callback ( '!s:(\d+):"(.*?)";!', function($match) {      
    return ($match[1] == strlen($match[2])) ? $match[0] : 's:' . strlen($match[2]) . ':"' . $match[2] . '";';
},$bad_data );

이유가 unserialize()데이터베이스에 잘못 직렬화된 데이터를 넣었기 때문에 실패했습니다. 여기서 공식 설명을 참조하십시오.부터serialize()이진 데이터를 반환하고 php 변수는 인코딩 메서드에 상관하지 않으므로 TEXT에 VARCHAR()을 입력하면 이 오류가 발생합니다.

해결책: 시리얼화된 데이터를 테이블 내의 BLOB에 저장합니다.

퀵픽스

시리얼화된 어레이 내의 요소의 길이를 재계산합니다.단, 사용되지 않습니다(preg_replace). preg_replace_callback을 사용하는 것이 좋습니다.

편집: 새로운 버전은 잘못된 길이뿐만 아니라 줄 바꿈을 수정하고 올바른 문자를 aczent로 카운트합니다(mickmackusa 덕분에).

// New Version
$data = preg_replace_callback('!s:\d+:"(.*?)";!s', 
    function($m) {
        return "s:" . strlen($m[1]) . ':"'.$m[1].'";'; 
    }, $data
);
$badData = 'a:2:{i:0;s:16:"as:45:"d";
Is \n";i:1;s:19:"as:45:"d";
Is \r\n";}';

제안된 정규식을 사용하여 끊어진 직렬화 문자열을 수정할 수 없습니다.

$data = preg_replace('!s:(\d+):"(.*?)";!e', "'s:'.strlen('$2').':\"$2\";'", $badData);
var_dump(@unserialize($data)); // Output: bool(false)

// or

$data = preg_replace_callback(
    '/s:(\d+):"(.*?)";/',
    function($m){
        return 's:' . strlen($m[2]) . ':"' . $m[2] . '";';
    },
    $badData
);
var_dump(@unserialize($data)); // Output: bool(false)

끊어진 시리얼라이즈 문자열은 다음 regex를 사용하여 수정할 수 있습니다.

$data = preg_replace_callback(
    '/(?<=^|\{|;)s:(\d+):\"(.*?)\";(?=[asbdiO]\:\d|N;|\}|$)/s',
    function($m){
        return 's:' . strlen($m[2]) . ':"' . $m[2] . '";';
    },
    $badData
);

var_dump(@unserialize($data));

산출량

array(2) {
  [0] =>
  string(17) "as:45:"d";
Is \n"
  [1] =>
  string(19) "as:45:"d";
Is \r\n"
}

또는

array(2) {
  [0] =>
  string(16) "as:45:"d";
Is \n"
  [1] =>
  string(18) "as:45:"d";
Is \r\n"
}

이 오류는 문자 집합이 잘못되었기 때문에 발생합니다.

태그 열기 후 문자 집합 설정:

header('Content-Type: text/html; charset=utf-8');

데이터베이스에 charset utf8을 설정합니다.

mysql_query("SET NAMES 'utf8'");
public function unserializeKeySkills($string) {
    $output = array();
    $string = trim(preg_replace('/\s\s+/', ' ',$string));
    $string = preg_replace_callback('!s:(\d+):"(.*?)";!', function($m) { return 's:'.strlen($m[2]).':"'.$m[2].'";'; }, utf8_encode( trim(preg_replace('/\s\s+/', ' ',$string)) ));
    try {
        $output =  unserialize($string);
    } catch (\Exception $e) {
        \Log::error("unserialize Data : " .print_r($string,true));
    }
    return $output;
}

끊어진 시리얼라이즈 문자열은 멀티바이트 문자 처리를 사용하여 다음 함수를 사용하여 수정할 수 있습니다.

function repairSerializeString($value)
{

    $regex = '/s:([0-9]+):"(.*?)"/';

    return preg_replace_callback(
        $regex, function($match) {
            return "s:".mb_strlen($match[2]).":\"".$match[2]."\""; 
        },
        $value
    );
}

다음은 파손된 시리얼 스트링을 수정하기 위한 온라인 도구입니다.

이 문제는 DB 및 시리얼라이제이션 데이터(특히 serialization data)에 대한 검색 및 치환에 의해 주로 발생한다는 점을 덧붙이고 싶습니다.key length)는되지 않고, 「의 이 됩니다.

그러나 위의 도구는 다음 논리를 사용하여 시리얼화 데이터(여기서 복사)를 수정합니다.

function error_correction_serialise($string){
    // at first, check if "fixing" is really needed at all. After that, security checkup.
    if ( unserialize($string) !== true &&  preg_match('/^[aOs]:/', $string) ) {
         $string = preg_replace_callback( '/s\:(\d+)\:\"(.*?)\";/s',    function($matches){return 's:'.strlen($matches[2]).':"'.$matches[2].'";'; },   $string );
    }
    return $string;
} 

공식 문서는 false를 반환하고 E_NOTICE를 설정해야 한다고 합니다.

그러나 오류가 발생했기 때문에 오류 보고는 E_NOTICE에 의해 트리거되도록 설정됩니다.

에서는, 에러가 할 수 입니다.unserialize

$old_err=error_reporting(); 
error_reporting($old_err & ~E_NOTICE);
$object = unserialize($serialized_data);
error_reporting($old_err);

base64 encode/module을 사용하는 것을 검토해 주십시오.

$string=base64_encode(serialize($obj));
unserialize(base64_decode($string));

되어 있습니다.는 아마 '1'의 한 에 의해 입니다.이러한 서브스트링은 아마 를 업데이트하기 위해 귀찮은 사람이 수동으로 대체한 것 같습니다.image파일명을 지정합니다. 데이터 「OP」)를하여 아래 할 수 . ★★★★★★★★★★★★★★★★★★,C:fakepath100.jpg가 짧다19 , , , , , , , .17.

시리얼화된 문자열의 파손은 잘못된 바이트 수/문자 수로 제한되므로 다음 작업을 통해 파손된 문자열을 올바른 바이트 수 값으로 갱신할 수 있습니다.

다음 regex 기반 교체는 바이트 수를 수정하는 데만 유효하며 그 이상은 유효하지 않습니다.

이전 투고의 대부분은 다른 사람의 regex 패턴을 복사 붙여넣는 것 같습니다.교체에 사용되지 않을 경우 파손될 수 있는 바이트 수 번호를 캡처할 필요가 없습니다.또, 의 추가s패턴 수식자는 문자열 값에 줄 바꿈 또는 줄 바꿈이 포함되어 있는 경우에 적합합니다.

*시리얼라이징에 의한 멀티바이트 문자의 처리를 모르는 경우 커스텀콜백에서는 사용하지 마십시오.문자수가아닌바이트수이기때문입니다.내 출력을 참조해 주세요...

코드: (OP의 데이터로 데모)(임의 샘플 데이터로 데모)(조건 치환으로 데모)

$corrupted = <<<STRING
a:4:{i:0;s:3:"three";i:1;s:5:"five";i:2;s:2:"newline1
newline2";i:3;s:6:"garçon";}
STRING;

$repaired = preg_replace_callback(
        '/s:\d+:"(.*?)";/s',
        //  ^^^- matched/consumed but not captured because not used in replacement
        function ($m) {
            return "s:" . strlen($m[1]) . ":\"{$m[1]}\";";
        },
        $corrupted
    );

echo $corrupted , "\n" , $repaired;
echo "\n---\n";
var_export(unserialize($repaired));

출력:

a:4:{i:0;s:3:"three";i:1;s:5:"five";i:2;s:2:"newline1
Newline2";i:3;s:6:"garçon";}
a:4:{i:0;s:5:"three";i:1;s:4:"five";i:2;s:17:"newline1
Newline2";i:3;s:7:"garçon";}
---
array (
  0 => 'three',
  1 => 'five',
  2 => 'newline1
Newline2',
  3 => 'garçon',
)

토끼 굴에 한 쪽 다리를 내려놓고...위의 내용은 문자열 값에 큰따옴표가 있어도 정상적으로 동작하지만 문자열 값에 다음 값이 포함되어 있는 경우";또는 누군가를 괴롭히는 다른 도구도 있습니다. 조금 더 나아가서 "lookarounds"를 구현해야 합니다.나의 새로운 패턴

리딩이s다음과 같습니다.

  • 전체 입력 문자열의 시작 또는
  • 앞에 있는;

체크하고 있습니다.";다음과 같습니다.

  • 전체 입력 문자열의 마지막에 또는
  • 이어서}또는
  • 문자열 또는 정수 선언 뒤에 이어지는s:또는i:

모든 가능성을 테스트해 본 것은 아닙니다.실제로 시리얼화 문자열의 모든 가능성을 비교적 잘 모릅니다.시리얼화 데이터를 사용하는 것을 선택하지 않았기 때문입니다.현대 어플리케이션에서는 항상 json입니다.추가 선행 또는 후행 문자가 있으면 댓글을 남겨주시면 룩어라운드를 확장해 드리겠습니다.

확장 코드 조각: (데모)

$corrupted_byte_counts = <<<STRING
a:12:{i:0;s:3:"three";i:1;s:5:"five";i:2;s:2:"newline1
newline2";i:3;s:6:"garçon";i:4;s:111:"double " quote \"escaped";i:5;s:1:"a,comma";i:6;s:9:"a:colon";i:7;s:0:"single 'quote";i:8;s:999:"semi;colon";s:5:"assoc";s:3:"yes";i:9;s:1:"monkey";wrenching doublequote-semicolon";s:3:"s:";s:9:"val s: val";}
STRING;

$repaired = preg_replace_callback(
        '/(?<=^|;)s:\d+:"(.*?)";(?=$|}|[si]:)/s',
        //^^^^^^^^--------------^^^^^^^^^^^^^-- some additional validation
        function ($m) {
            return 's:' . strlen($m[1]) . ":\"{$m[1]}\";";
        },
        $corrupted_byte_counts
    );

echo "corrupted serialized array:\n$corrupted_byte_counts";
echo "\n---\n";
echo "repaired serialized array:\n$repaired";
echo "\n---\n";
print_r(unserialize($repaired));

출력:

corrupted serialized array:
a:12:{i:0;s:3:"three";i:1;s:5:"five";i:2;s:2:"newline1
newline2";i:3;s:6:"garçon";i:4;s:111:"double " quote \"escaped";i:5;s:1:"a,comma";i:6;s:9:"a:colon";i:7;s:0:"single 'quote";i:8;s:999:"semi;colon";s:5:"assoc";s:3:"yes";i:9;s:1:"monkey";wrenching doublequote-semicolon";s:3:"s:";s:9:"val s: val";}
---
repaired serialized array:
a:12:{i:0;s:5:"three";i:1;s:4:"five";i:2;s:17:"newline1
newline2";i:3;s:7:"garçon";i:4;s:24:"double " quote \"escaped";i:5;s:7:"a,comma";i:6;s:7:"a:colon";i:7;s:13:"single 'quote";i:8;s:10:"semi;colon";s:5:"assoc";s:3:"yes";i:9;s:39:"monkey";wrenching doublequote-semicolon";s:2:"s:";s:10:"val s: val";}
---
Array
(
    [0] => three
    [1] => five
    [2] => newline1
newline2
    [3] => garçon
    [4] => double " quote \"escaped
    [5] => a,comma
    [6] => a:colon
    [7] => single 'quote
    [8] => semi;colon
    [assoc] => yes
    [9] => monkey";wrenching doublequote-semicolon
    [s:] => val s: val
)

대조 유형을 다음과 같이 변경해야 합니다.utf8_unicode_ci그러면 문제가 해결됩니다.

제 경우는 시리얼화된 데이터를BLOB전체 값을 포함할 수 없을 정도로 크지 않은 MySQL DB의 필드입니다.이러한 문자열은 분명히 직렬화 해제할 수 없습니다.
를 ""로 하면""MEDIUMBLOB문제는 해결되었다. 옵션 할 수 .ROW_FORMAT로로 합니다.DYNAMIC ★★★★★★★★★★★★★★★★★」COMPRESSED

이 페이지에서 몇 가지 작업을 시도했지만 성공하지 못한 후 페이지 소스를 살펴보니 직렬 문자열의 모든 따옴표가 html-entity로 대체되었습니다.이러한 엔티티를 디코딩하면 많은 문제를 피할 수 있습니다.

$myVar = html_entity_decode($myVar);

이 문제의 또 다른 이유는 "payload" 세션테이블의 컬럼타입일 수 있습니다.세션에 대량의 데이터가 있는 경우 텍스트 열로는 충분하지 않습니다.MEDIUSTEXT 또는 LONGTEXT가 필요합니다.

특정 필드의 열 크기 변경(LONGTEXT)

모든 경우에 사용할 수 있습니다.

$newdata = preg_replace_callback(
    '/(?<=^|\{|;)s:(\d+):\"(.*?)\";(?=[asbdiO]\:\d|N;|\}|$)/s',
    function($m){
        return 's:' . strlen($m[2]) . ':"' . $m[2] . '";';
    },
    $badData
);

언급URL : https://stackoverflow.com/questions/10152904/how-to-repair-a-serialized-string-which-has-been-corrupted-by-an-incorrect-byte

반응형