x3x3x3x_5h3ll
— 53cur3 — 5h3ll_1d —
Linux vps-10654784.cedaps.org.br 3.10.0-1160.119.1.el7.x86_64 #1 SMP Tue Jun 4 14:43:51 UTC 2024 x86_64
  INFO SERVER : Apache PHP : 7.4.33
/lib64/perl5/vendor_perl/Filter/Util/
162.240.100.168

 
[ NAME ] [ SIZE ] [ PERM ] [ DATE ] [ ACTN ]
+FILE +DIR
Call.pm 12.61 KB -rw-r--r-- 2013-04-02 23:33 R E G D
Exec.pm 1.195 KB -rw-r--r-- 2013-04-02 23:35 R E G D
filter-util.pl 0.965 KB -rw-r--r-- 2013-03-29 13:56 R E G D
perlfilter.pod 20.559 KB -rw-r--r-- 2012-06-19 15:16 R E G D
REQUEST EXIT
=head1 NAME perlfilter - Source Filters =head1 DESCRIPTION This article is about a little-known feature of Perl called I. Source filters alter the program text of a module before Perl sees it, much as a C preprocessor alters the source text of a C program before the compiler sees it. This article tells you more about what source filters are, how they work, and how to write your own. The original purpose of source filters was to let you encrypt your program source to prevent casual piracy. This isn't all they can do, as you'll soon learn. But first, the basics. =head1 CONCEPTS Before the Perl interpreter can execute a Perl script, it must first read it from a file into memory for parsing and compilation. If that script itself includes other scripts with a C or C statement, then each of those scripts will have to be read from their respective files as well. Now think of each logical connection between the Perl parser and an individual file as a I. A source stream is created when the Perl parser opens a file, it continues to exist as the source code is read into memory, and it is destroyed when Perl is finished parsing the file. If the parser encounters a C or C statement in a source stream, a new and distinct stream is created just for that file. The diagram below represents a single source stream, with the flow of source from a Perl script file on the left into the Perl parser on the right. This is how Perl normally operates. file -------> parser There are two important points to remember: =over 5 =item 1. Although there can be any number of source streams in existence at any given time, only one will be active. =item 2. Every source stream is associated with only one file. =back A source filter is a special kind of Perl module that intercepts and modifies a source stream before it reaches the parser. A source filter changes our diagram like this: file ----> filter ----> parser If that doesn't make much sense, consider the analogy of a command pipeline. Say you have a shell script stored in the compressed file I. The simple pipeline command below runs the script without needing to create a temporary file to hold the uncompressed file. gunzip -c trial.gz | sh In this case, the data flow from the pipeline can be represented as follows: trial.gz ----> gunzip ----> sh With source filters, you can store the text of your script compressed and use a source filter to uncompress it for Perl's parser: compressed gunzip Perl program ---> source filter ---> parser =head1 USING FILTERS So how do you use a source filter in a Perl script? Above, I said that a source filter is just a special kind of module. Like all Perl modules, a source filter is invoked with a use statement. Say you want to pass your Perl source through the C preprocessor before execution. As it happens, the source filters distribution comes with a C preprocessor filter module called Filter::cpp. Below is an example program, C, which makes use of this filter. Line numbers have been added to allow specific lines to be referenced easily. 1: use Filter::cpp; 2: #define TRUE 1 3: $a = TRUE; 4: print "a = $a\n"; When you execute this script, Perl creates a source stream for the file. Before the parser processes any of the lines from the file, the source stream looks like this: cpp_test ---------> parser Line 1, C, includes and installs the C filter module. All source filters work this way. The use statement is compiled and executed at compile time, before any more of the file is read, and it attaches the cpp filter to the source stream behind the scenes. Now the data flow looks like this: cpp_test ----> cpp filter ----> parser As the parser reads the second and subsequent lines from the source stream, it feeds those lines through the C source filter before processing them. The C filter simply passes each line through the real C preprocessor. The output from the C preprocessor is then inserted back into the source stream by the filter. .-> cpp --. | | | | | <-' cpp_test ----> cpp filter ----> parser The parser then sees the following code: use Filter::cpp; $a = 1; print "a = $a\n"; Let's consider what happens when the filtered code includes another module with use: 1: use Filter::cpp; 2: #define TRUE 1 3: use Fred; 4: $a = TRUE; 5: print "a = $a\n"; The C filter does not apply to the text of the Fred module, only to the text of the file that used it (C). Although the use statement on line 3 will pass through the cpp filter, the module that gets included (C) will not. The source streams look like this after line 3 has been parsed and before line 4 is parsed: cpp_test ---> cpp filter ---> parser (INACTIVE) Fred.pm ----> parser As you can see, a new stream has been created for reading the source from C. This stream will remain active until all of C has been parsed. The source stream for C will still exist, but is inactive. Once the parser has finished reading Fred.pm, the source stream associated with it will be destroyed. The source stream for C then becomes active again and the parser reads line 4 and subsequent lines from C. You can use more than one source filter on a single file. Similarly, you can reuse the same filter in as many files as you like. For example, if you have a uuencoded and compressed source file, it is possible to stack a uudecode filter and an uncompression filter like this: use Filter::uudecode; use Filter::uncompress; M'XL(".H7/;1I;_>_I3=&E=%:F*I"T?22Q/ M6]9* ... Once the first line has been processed, the flow will look like this: file ---> uudecode ---> uncompress ---> parser filter filter Data flows through filters in the same order they appear in the source file. The uudecode filter appeared before the uncompress filter, so the source file will be uudecoded before it's uncompressed. =head1 WRITING A SOURCE FILTER There are three ways to write your own source filter. You can write it in C, use an external program as a filter, or write the filter in Perl. I won't cover the first two in any great detail, so I'll get them out of the way first. Writing the filter in Perl is most convenient, so I'll devote the most space to it. =head1 WRITING A SOURCE FILTER IN C The first of the three available techniques is to write the filter completely in C. The external module you create interfaces directly with the source filter hooks provided by Perl. The advantage of this technique is that you have complete control over the implementation of your filter. The big disadvantage is the increased complexity required to write the filter - not only do you need to understand the source filter hooks, but you also need a reasonable knowledge of Perl guts. One of the few times it is worth going to this trouble is when writing a source scrambler. The C filter (which unscrambles the source before Perl parses it) included with the source filter distribution is an example of a C source filter (see Decryption Filters, below). =over 5 =item B All decryption filters work on the principle of "security through obscurity." Regardless of how well you write a decryption filter and how strong your encryption algorithm is, anyone determined enough can retrieve the original source code. The reason is quite simple - once the decryption filter has decrypted the source back to its original form, fragments of it will be stored in the computer's memory as Perl parses it. The source might only be in memory for a short period of time, but anyone possessing a debugger, skill, and lots of patience can eventually reconstruct your program. That said, there are a number of steps that can be taken to make life difficult for the potential cracker. The most important: Write your decryption filter in C and statically link the decryption module into the Perl binary. For further tips to make life difficult for the potential cracker, see the file I in the source filters distribution. =back =head1 CREATING A SOURCE FILTER AS A SEPARATE EXECUTABLE An alternative to writing the filter in C is to create a separate executable in the language of your choice. The separate executable reads from standard input, does whatever processing is necessary, and writes the filtered data to standard output. C is an example of a source filter implemented as a separate executable - the executable is the C preprocessor bundled with your C compiler. The source filter distribution includes two modules that simplify this task: C and C. Both allow you to run any external executable. Both use a coprocess to control the flow of data into and out of the external executable. (For details on coprocesses, see Stephens, W.R., "Advanced Programming in the UNIX Environment." Addison-Wesley, ISBN 0-210-56317-7, pages 441-445.) The difference between them is that C spawns the external command directly, while C spawns a shell to execute the external command. (Unix uses the Bourne shell; NT uses the cmd shell.) Spawning a shell allows you to make use of the shell metacharacters and redirection facilities. Here is an example script that uses C: use Filter::sh 'tr XYZ PQR'; $a = 1; print "XYZ a = $a\n"; The output you'll get when the script is executed: PQR a = 1 Writing a source filter as a separate executable works fine, but a small performance penalty is incurred. For example, if you execute the small example above, a separate subprocess will be created to run the Unix C command. Each use of the filter requires its own subprocess. If creating subprocesses is expensive on your system, you might want to consider one of the other options for creating source filters. =head1 WRITING A SOURCE FILTER IN PERL The easiest and most portable option available for creating your own source filter is to write it completely in Perl. To distinguish this from the previous two techniques, I'll call it a Perl source filter. To help understand how to write a Perl source filter we need an example to study. Here is a complete source filter that performs rot13 decoding. (Rot13 is a very simple encryption scheme used in Usenet postings to hide the contents of offensive posts. It moves every letter forward thirteen places, so that A becomes N, B becomes O, and Z becomes M.) package Rot13; use Filter::Util::Call; sub import { my ($type) = @_; my ($ref) = []; filter_add(bless $ref); } sub filter { my ($self) = @_; my ($status); tr/n-za-mN-ZA-M/a-zA-Z/ if ($status = filter_read()) > 0; $status; } 1; All Perl source filters are implemented as Perl classes and have the same basic structure as the example above. First, we include the C module, which exports a number of functions into your filter's namespace. The filter shown above uses two of these functions, C and C. Next, we create the filter object and associate it with the source stream by defining the C function. If you know Perl well enough, you know that C is called automatically every time a module is included with a use statement. This makes C the ideal place to both create and install a filter object. In the example filter, the object (C<$ref>) is blessed just like any other Perl object. Our example uses an anonymous array, but this isn't a requirement. Because this example doesn't need to store any context information, we could have used a scalar or hash reference just as well. The next section demonstrates context data. The association between the filter object and the source stream is made with the C function. This takes a filter object as a parameter (C<$ref> in this case) and installs it in the source stream. Finally, there is the code that actually does the filtering. For this type of Perl source filter, all the filtering is done in a method called C. (It is also possible to write a Perl source filter using a closure. See the C manual page for more details.) It's called every time the Perl parser needs another line of source to process. The C method, in turn, reads lines from the source stream using the C