recherche
Maisonphp教程php手册浅谈 PHP 变量可用字符

浅谈 PHP 变量可用字符

Jun 13, 2016 am 09:37 AM
aspnetprogrammation de logiciels

先来说说php变量的命名规则,百度下一抓一大把:
(1) PHP的变量名区分大小写;
(2) 变量名必须以美元符号$开始;
(3) 变量名开头可以以下划线开始;
(4) 变量名不能以数字字符开头.

其实所有编程都类似的命名规范就是:
1. 变量第一个字符最好是 字母或_,不能以数字开头
2. 第二个字符开始允许 数字,字母,_

好了,差不多就是这样了,但是这不是我们要说的重点。
今天我们说说 PHP 变量的可用字符,不仅仅是 数字,字母,_ 哦。

前几天QQ上一朋友发我一个shell,是加密过的,通篇乱码,不过上面有注释,叫做 “神盾加密” 好霸气的样子。
里面用了一些比较生僻的知识点,其中最明显的就是变量名,所以今天我们先从变量开始讲。

当然网上我也没找到权威的质料强有力的说明PHP的变量名可用字符的信息,所以我只能自己测试了。(英文不好,没办法谷歌到有利的证据)
先来看下我所用的方法,(如果你有更好的方法,希望分享下。)

<?<span php
</span><span if</span> (<span $_POST</span><span ) {
    </span><span $chr</span> = <span chr</span>(<span $_POST</span>['chr'<span ]);
    </span><span eval</span>('$'.<span $chr</span>."=1;"<span );
    </span><span echo</span> 'ok'<span ;
    </span><span exit</span><span ;
}
</span>?>
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>test</title>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.js"></script>
</head>
<body>
    <script>
    <span for</span>(<span var</span> i = 0x00; i <= 0xFF; i++) { <span //</span><span  0x00 - 0xFF  255个字符</span>
        $.ajaxSettings.async = <span false</span>; <span //</span><span  同步模式, 为了按顺序返回数据</span>
        $.post( "?", {<span chr</span>: i}, (<span function</span> (data) { <span //</span><span  post i 给 php 解析</span>
            data === 'ok' && console.<span log</span>( "\\x"+(i).toString(16) ); <span //</span><span  如果只返回 ok 说明能正常执行,否则会抛出异常</span>
<span         });
    }
    </span></script>
</body>
</html>

代码还算比较简单,PHP 部分只负责解析每一个字符当作变量名的执行结果是否会抛出溢出。
比如 字符 a 那么会解析  ('$a=1;');  这样的结果肯定没问题,所以不会抛出异常,返回结果就是 ok 字符。
如果 字符 - 那么会解析  ('$-=1;');  这明显是不对的,所以会抛出  : syntax error, unexpected '-', expecting T_VARIABLE or '$'  和 ok 字符。
而下面的 ajax 部分者正是利用返回结果是否为 'ok' 而判断是否是有效的变量名。
看看执行后的结果是什么吧:

"\x41, \x42, \x43, \x44, \x45, \x46, \x47, \x48, \x49, \x4a, \x4b, \x4c, \x4d, \x4e, \x4f, \x50, \x51, \x52, \x53, \x54, \x55, \x56, \x57, \x58, \x59, \x5a, \x5f, \x61, \x62, \x63, \x64, \x65, \x66, \x67, \x68, \x69, \x6a, \x6b, \x6c, \x6d, \x6e, \x6f, \x70, \x71, \x72, \x73, \x74, \x75, \x76, \x77, \x78, \x79, \x7a, \x7f, \x80, \x81, \x82, \x83, \x84, \x85, \x86, \x87, \x88, \x89, \x8a, \x8b, \x8c, \x8d, \x8e, \x8f, \x90, \x91, \x92, \x93, \x94, \x95, \x96, \x97, \x98, \x99, \x9a, \x9b, \x9c, \x9d, \x9e, \x9f, \xa0, \xa1, \xa2, \xa3, \xa4, \xa5, \xa6, \xa7, \xa8, \xa9, \xaa, \xab, \xac, \xad, \xae, \xaf, \xb0, \xb1, \xb2, \xb3, \xb4, \xb5, \xb6, \xb7, \xb8, \xb9, \xba, \xbb, \xbc, \xbd, \xbe, \xbf, \xc0, \xc1, \xc2, \xc3, \xc4, \xc5, \xc6, \xc7, \xc8, \xc9, \xca, \xcb, \xcc, \xcd, \xce, \xcf, \xd0, \xd1, \xd2, \xd3, \xd4, \xd5, \xd6, \xd7, \xd8, \xd9, \xda, \xdb, \xdc, \xdd, \xde, \xdf, \xe0, \xe1, \xe2, \xe3, \xe4, \xe5, \xe6, \xe7, \xe8, \xe9, \xea, \xeb, \xec, \xed, \xee, \xef, \xf0, \xf1, \xf2, \xf3, \xf4, \xf5, \xf6, \xf7, \xf8, \xf9, \xfa, \xfb, \xfc, \xfd, \xfe, \xff"

整理后发现是这样的16进制数据,当然看不懂没关系,看下转义后的结果:

"A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z, _, a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z, , ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,  , &iexcl;, &cent;, &pound;, &curren;, &yen;, &brvbar;, &sect;, &uml;, &copy;, &ordf;, &laquo;, &not;, &shy;, &reg;, &macr;, &deg;, &plusmn;, &sup2;, &sup3;, &acute;, &micro;, &para;, &middot;, &cedil;, &sup1;, &ordm;, &raquo;, &frac14;, &frac12;, &frac34;, &iquest;, &Agrave;, &Aacute;, &Acirc;, &Atilde;, &Auml;, &Aring;, &AElig;, &Ccedil;, &Egrave;, &Eacute;, &Ecirc;, &Euml;, &Igrave;, &Iacute;, &Icirc;, &Iuml;, &ETH;, &Ntilde;, &Ograve;, &Oacute;, &Ocirc;, &Otilde;, &Ouml;, &times;, &Oslash;, &Ugrave;, &Uacute;, &Ucirc;, &Uuml;, &Yacute;, &THORN;, &szlig;, &agrave;, &aacute;, &acirc;, &atilde;, &auml;, &aring;, &aelig;, &ccedil;, &egrave;, &eacute;, &ecirc;, &euml;, &igrave;, &iacute;, &icirc;, &iuml;, &eth;, &ntilde;, &ograve;, &oacute;, &ocirc;, &otilde;, &ouml;, &divide;, &oslash;, &ugrave;, &uacute;, &ucirc;, &uuml;, &yacute;, &thorn;, &yuml;"

除了前面的 A-Z_a-z 是我们熟悉的,后面的那些乱七八糟的东西竟然也能当作正常的变量名,简直不可思议。
其实只是PHP拓展了变量名的字符范围,在 A-Z_a-z 之上,将变量可用字符范围拓展到了 \x7f-\xff。
所以,第一个字符范围应该是
那么第二个字符是否也是这样能,我们继续测试下。
将上面 php 代码里的  ('$'.."=1;");  改成  ('$a'.."=1;");  保存测试、

"\x9, \xa, \xd, \x20, \x30, \x31, \x32, \x33, \x34, \x35, \x36, \x37, \x38, \x39, \x41, \x42, \x43, \x44, \x45, \x46, \x47, \x48, \x49, \x4a, \x4b, \x4c, \x4d, \x4e, \x4f, \x50, \x51, \x52, \x53, \x54, \x55, \x56, \x57, \x58, \x59, \x5a, \x5f, \x61, \x62, \x63, \x64, \x65, \x66, \x67, \x68, \x69, \x6a, \x6b, \x6c, \x6d, \x6e, \x6f, \x70, \x71, \x72, \x73, \x74, \x75, \x76, \x77, \x78, \x79, \x7a, \x7f, \x80, \x81, \x82, \x83, \x84, \x85, \x86, \x87, \x88, \x89, \x8a, \x8b, \x8c, \x8d, \x8e, \x8f, \x90, \x91, \x92, \x93, \x94, \x95, \x96, \x97, \x98, \x99, \x9a, \x9b, \x9c, \x9d, \x9e, \x9f, \xa0, \xa1, \xa2, \xa3, \xa4, \xa5, \xa6, \xa7, \xa8, \xa9, \xaa, \xab, \xac, \xad, \xae, \xaf, \xb0, \xb1, \xb2, \xb3, \xb4, \xb5, \xb6, \xb7, \xb8, \xb9, \xba, \xbb, \xbc, \xbd, \xbe, \xbf, \xc0, \xc1, \xc2, \xc3, \xc4, \xc5, \xc6, \xc7, \xc8, \xc9, \xca, \xcb, \xcc, \xcd, \xce, \xcf, \xd0, \xd1, \xd2, \xd3, \xd4, \xd5, \xd6, \xd7, \xd8, \xd9, \xda, \xdb, \xdc, \xdd, \xde, \xdf, \xe0, \xe1, \xe2, \xe3, \xe4, \xe5, \xe6, \xe7, \xe8, \xe9, \xea, \xeb, \xec, \xed, \xee, \xef, \xf0, \xf1, \xf2, \xf3, \xf4, \xf5, \xf6, \xf7, \xf8, \xf9, \xfa, \xfb, \xfc, \xfd, \xfe, \xff"

发现结果多了好多字符,其实有一部分我们是要去掉的,比如 其实就是 空格,相当于  ('$a =1;');  而已,当然是能正常执行的。
除了空格,还有 \t\r\n 都去掉因为这些也是PHP语法说允许的 \t=,\n=,\r=,所以我们要去掉结果中的前4个数据, , ,
最终得到的结果其实只是多了  \x30, \x31, \x32, \x33, \x34, \x35, \x36, \x37, \x38, \x39  熟悉 ascii 的人也许一眼就看出来了,这就是数字
所以第一个字符范围应该是 对正则不熟的也许会觉得怎么不是 ,其实 \w 就是 0-9a-zA-Z_

也许有人会说  $; ${};  这样的变量呢?
我觉得这个已脱离了变量命名的范围了,不是么。

好了,关于 php 变量可用字符的知识点分享完毕了,如果有哪说的不对的,请留言,我会及时改正以免误导大家。

我的猜测: ascii 范围 0-127(-), latin1 范围 0-255(-),也许PHP就是将范围扩充到 latin1 字符集了,当然我没看过PHP源码,只能说是个猜想而已。

 

经 @holine 提醒,我去翻了官网手册,果然找到了,好吧,我还费了这么大的劲去测试、

查看资料 http://www.php.net/manual/zh/language.variables.basics.php

Déclaration
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn

Outils d'IA chauds

Undresser.AI Undress

Undresser.AI Undress

Application basée sur l'IA pour créer des photos de nu réalistes

AI Clothes Remover

AI Clothes Remover

Outil d'IA en ligne pour supprimer les vêtements des photos.

Undress AI Tool

Undress AI Tool

Images de déshabillage gratuites

Clothoff.io

Clothoff.io

Dissolvant de vêtements AI

AI Hentai Generator

AI Hentai Generator

Générez AI Hentai gratuitement.

Article chaud

R.E.P.O. Crystals d'énergie expliqués et ce qu'ils font (cristal jaune)
3 Il y a quelques semainesBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Meilleurs paramètres graphiques
3 Il y a quelques semainesBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Comment réparer l'audio si vous n'entendez personne
3 Il y a quelques semainesBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: Comment déverrouiller tout dans Myrise
4 Il y a quelques semainesBy尊渡假赌尊渡假赌尊渡假赌

Outils chauds

Listes Sec

Listes Sec

SecLists est le compagnon ultime du testeur de sécurité. Il s'agit d'une collection de différents types de listes fréquemment utilisées lors des évaluations de sécurité, le tout en un seul endroit. SecLists contribue à rendre les tests de sécurité plus efficaces et productifs en fournissant facilement toutes les listes dont un testeur de sécurité pourrait avoir besoin. Les types de listes incluent les noms d'utilisateur, les mots de passe, les URL, les charges utiles floues, les modèles de données sensibles, les shells Web, etc. Le testeur peut simplement extraire ce référentiel sur une nouvelle machine de test et il aura accès à tous les types de listes dont il a besoin.

PhpStorm version Mac

PhpStorm version Mac

Le dernier (2018.2.1) outil de développement intégré PHP professionnel

Adaptateur de serveur SAP NetWeaver pour Eclipse

Adaptateur de serveur SAP NetWeaver pour Eclipse

Intégrez Eclipse au serveur d'applications SAP NetWeaver.

DVWA

DVWA

Damn Vulnerable Web App (DVWA) est une application Web PHP/MySQL très vulnérable. Ses principaux objectifs sont d'aider les professionnels de la sécurité à tester leurs compétences et leurs outils dans un environnement juridique, d'aider les développeurs Web à mieux comprendre le processus de sécurisation des applications Web et d'aider les enseignants/étudiants à enseigner/apprendre dans un environnement de classe. Application Web sécurité. L'objectif de DVWA est de mettre en pratique certaines des vulnérabilités Web les plus courantes via une interface simple et directe, avec différents degrés de difficulté. Veuillez noter que ce logiciel

SublimeText3 version Mac

SublimeText3 version Mac

Logiciel d'édition de code au niveau de Dieu (SublimeText3)