Commit d887af54 authored by Alex Yustasov's avatar Alex Yustasov Committed by Automatic Converter

3.7-alt1

- initial release
parents
copy: *.patch
tar.gz: shc suffix=.tgz
--- Makefile.old 2005-01-27 00:16:40 +0200
+++ Makefile 2005-01-27 00:16:58 +0200
@@ -1,29 +1,29 @@
# Makefile
#
-INSTALL_PATH = /usr/local
+INSTALL_PATH =$(RPM_BUILD_ROOT)/usr
# For SCO
-CFLAGS = -b elf -O -D_SVID
+#CFLAGS = -b elf -O -D_SVID
# For IRIX
-CFLAGS = -xansi -fullwarn -O3 -g0
+#CFLAGS = -xansi -fullwarn -O3 -g0
# For Solaris
-CFLAGS = -fast -xO4 -s -v -Xa
+#CFLAGS = -fast -xO4 -s -v -Xa
# For HPUX
-CFLAGS = -Wall -O -Ae
+#CFLAGS = -Wall -O -Ae
# For OSF1
-CFLAGS = -w -verbose -fast -std1 -g0
+#CFLAGS = -w -verbose -fast -std1 -g0
# For GNU C compiler
CFLAGS = -Wall -O6 -pedantic
-SHELL = /bin/sh
+SHELL = /bin/bash
-all: shc ask_for_test
+all: shc
shc: shc.c
$(CC) $(CFLAGS) $< -o $@
@@ -63,9 +63,8 @@
install: shc
@echo '*** Installing shc and shc.1 on '$(INSTALL_PATH)
- @echo -n '*** Do you want to continue? '; read ANS; case "$$ANS" in y|Y|yes|Yes|YES) ;; *) exit 1;; esac;
install -c -s shc $(INSTALL_PATH)/bin/
- install -c -m 644 shc.1 $(INSTALL_PATH)/man/man1/
+ install -c -m 644 shc.1 $(INSTALL_PATH)/share/man/man1/
clean:
rm -f *.o *~ *.x.c
Name: shc
Version: 3.7
Release: alt1
Summary: Generic shell script compiler
License: GPL
Group: Development/Other
URL: http://www.datsi.fi.upm.es/~frosal/
Packager: Alex Yustasov <yust@altlinux.ru>
Source0: %name-%version.tgz
Patch0: %name-3.7-makefile.patch
%description
A generic shell script compiler. shc takes a script, which is
specified on the command line and produces C source code. The
generated source code is then compiled and linked to produce a
stripped binary executable. Use with care.
%prep
%setup -q -n %name-%version
%patch0
%build
%make
%install
%__mkdir_p $RPM_BUILD_ROOT{%_bindir,%_man1dir}
%makeinstall
%files
%_bindir/*
%doc CHANGES Copying shc.README shc.html
%_mandir/man?/*
%changelog
* Wed Jan 26 2005 Alex Yustasov <yust@altlinux.ru> 3.7-alt1
- initial release
CHANGES
3.7 Wed Jun 18 16:32:26 CEST 2003
Thanks to Philipp Koller <philipp@open.ch> for:
- Removed all strings in the compiled script.
- Improved program output and error messages.
- The -m option allows to define the *complete* expiration message.
- Updated manpage shc.1.
Thanks to Bryan <bryan.hogan@dstintl.com> for:
- Fix wrong $0 on ksh.
3.6 Fri Feb 21 09:40:32 CET 2003
Two new options:
-D switch on Debug exec calls.
-T switch off unTraceable.
Bash does not need -- after -c.
3.5 Mon Jan 20 19:08:43 CET 2003
Rewrite of large strings to silence the ISO C89 compiler
warnings about strings larger than 509 characters.
3.4 Tue Apr 16 17:43:12 CEST 2002
Remove "bad alignment" problem on AIX and other systems.
Where exists, use /proc/<pid>/as in untraceable.
3.3 Thu Jan 24 21:27:07 CET 2002
Prevent to ptrace the process.
3.2 Tue Mar 9 19:03:54 CET 1999
Find ancient pclose that must be fclose.
3.1 Tue Feb 16 21:36:59 CET 1999
Fixed a misbehavior on scripts with a in-frist-line option
equal to "end of options" (i.e. #!/bin/sh -- )
(Thanks to Bernard Blundell <blundell@lts.sel.alcatel.de>)
Stupid GCC "warning: return type of `main' is not `int'" removed.
3.0 Tue Oct 14 14:20:52 MET DST 1997
Added a new option "-r" to force a relaxed security and
so make a redistributable binary.
Modified expiration day format. Now is dd/mm/yyyy.
3.0b3 Fri Jun 6 22:09:05 WET DST 1997
Yet other few bugs fixed.
Output format simplified.
-pedantic compilation.
3.0b2 Tue Jun 3 17:51:51 GMT 1997
Some explicit type conversions removed.
Fixed the bug "END_OF_FILE" when compiling the generated code.
A flush is needed before a pclose.
st_blksize and st_blocks struct stat fields does not exist on
SCO, both not used now.
3.0b1 Wed Feb 26 14:27:22 WET 1997
The main difference with 2.4 is that in it the script was
compressed an then shuffle around, now int 3.0 the script is encripted
with an inline code, so not needend any external comand to work, and been
faster at startup. Other related adventage is that the only information
not encripted in .x.c is an stamp, expiration date and provider email
address.
Something equivalent to cheksums have been used to enforced at
execution that the executing shell has not been modified from the time
the script was compiled. If anybody tries to change the excuting shell,
.x will refuse to execute.
The generated .x.c source code is now readable.
GNU GENERAL PUBLIC LICENSE
Version 2, June 1991
Copyright (C) 1989, 1991 Free Software Foundation, Inc.
675 Mass Ave, Cambridge, MA 02139, USA
Everyone is permitted to copy and distribute verbatim copies
of this license document, but changing it is not allowed.
Preamble
The licenses for most software are designed to take away your
freedom to share and change it. By contrast, the GNU General Public
License is intended to guarantee your freedom to share and change free
software--to make sure the software is free for all its users. This
General Public License applies to most of the Free Software
Foundation's software and to any other program whose authors commit to
using it. (Some other Free Software Foundation software is covered by
the GNU Library General Public License instead.) You can apply it to
your programs, too.
When we speak of free software, we are referring to freedom, not
price. Our General Public Licenses are designed to make sure that you
have the freedom to distribute copies of free software (and charge for
this service if you wish), that you receive source code or can get it
if you want it, that you can change the software or use pieces of it
in new free programs; and that you know you can do these things.
To protect your rights, we need to make restrictions that forbid
anyone to deny you these rights or to ask you to surrender the rights.
These restrictions translate to certain responsibilities for you if you
distribute copies of the software, or if you modify it.
For example, if you distribute copies of such a program, whether
gratis or for a fee, you must give the recipients all the rights that
you have. You must make sure that they, too, receive or can get the
source code. And you must show them these terms so they know their
rights.
We protect your rights with two steps: (1) copyright the software, and
(2) offer you this license which gives you legal permission to copy,
distribute and/or modify the software.
Also, for each author's protection and ours, we want to make certain
that everyone understands that there is no warranty for this free
software. If the software is modified by someone else and passed on, we
want its recipients to know that what they have is not the original, so
that any problems introduced by others will not reflect on the original
authors' reputations.
Finally, any free program is threatened constantly by software
patents. We wish to avoid the danger that redistributors of a free
program will individually obtain patent licenses, in effect making the
program proprietary. To prevent this, we have made it clear that any
patent must be licensed for everyone's free use or not licensed at all.
The precise terms and conditions for copying, distribution and
modification follow.
GNU GENERAL PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. This License applies to any program or other work which contains
a notice placed by the copyright holder saying it may be distributed
under the terms of this General Public License. The "Program", below,
refers to any such program or work, and a "work based on the Program"
means either the Program or any derivative work under copyright law:
that is to say, a work containing the Program or a portion of it,
either verbatim or with modifications and/or translated into another
language. (Hereinafter, translation is included without limitation in
the term "modification".) Each licensee is addressed as "you".
Activities other than copying, distribution and modification are not
covered by this License; they are outside its scope. The act of
running the Program is not restricted, and the output from the Program
is covered only if its contents constitute a work based on the
Program (independent of having been made by running the Program).
Whether that is true depends on what the Program does.
1. You may copy and distribute verbatim copies of the Program's
source code as you receive it, in any medium, provided that you
conspicuously and appropriately publish on each copy an appropriate
copyright notice and disclaimer of warranty; keep intact all the
notices that refer to this License and to the absence of any warranty;
and give any other recipients of the Program a copy of this License
along with the Program.
You may charge a fee for the physical act of transferring a copy, and
you may at your option offer warranty protection in exchange for a fee.
2. You may modify your copy or copies of the Program or any portion
of it, thus forming a work based on the Program, and copy and
distribute such modifications or work under the terms of Section 1
above, provided that you also meet all of these conditions:
a) You must cause the modified files to carry prominent notices
stating that you changed the files and the date of any change.
b) You must cause any work that you distribute or publish, that in
whole or in part contains or is derived from the Program or any
part thereof, to be licensed as a whole at no charge to all third
parties under the terms of this License.
c) If the modified program normally reads commands interactively
when run, you must cause it, when started running for such
interactive use in the most ordinary way, to print or display an
announcement including an appropriate copyright notice and a
notice that there is no warranty (or else, saying that you provide
a warranty) and that users may redistribute the program under
these conditions, and telling the user how to view a copy of this
License. (Exception: if the Program itself is interactive but
does not normally print such an announcement, your work based on
the Program is not required to print an announcement.)
These requirements apply to the modified work as a whole. If
identifiable sections of that work are not derived from the Program,
and can be reasonably considered independent and separate works in
themselves, then this License, and its terms, do not apply to those
sections when you distribute them as separate works. But when you
distribute the same sections as part of a whole which is a work based
on the Program, the distribution of the whole must be on the terms of
this License, whose permissions for other licensees extend to the
entire whole, and thus to each and every part regardless of who wrote it.
Thus, it is not the intent of this section to claim rights or contest
your rights to work written entirely by you; rather, the intent is to
exercise the right to control the distribution of derivative or
collective works based on the Program.
In addition, mere aggregation of another work not based on the Program
with the Program (or with a work based on the Program) on a volume of
a storage or distribution medium does not bring the other work under
the scope of this License.
3. You may copy and distribute the Program (or a work based on it,
under Section 2) in object code or executable form under the terms of
Sections 1 and 2 above provided that you also do one of the following:
a) Accompany it with the complete corresponding machine-readable
source code, which must be distributed under the terms of Sections
1 and 2 above on a medium customarily used for software interchange; or,
b) Accompany it with a written offer, valid for at least three
years, to give any third party, for a charge no more than your
cost of physically performing source distribution, a complete
machine-readable copy of the corresponding source code, to be
distributed under the terms of Sections 1 and 2 above on a medium
customarily used for software interchange; or,
c) Accompany it with the information you received as to the offer
to distribute corresponding source code. (This alternative is
allowed only for noncommercial distribution and only if you
received the program in object code or executable form with such
an offer, in accord with Subsection b above.)
The source code for a work means the preferred form of the work for
making modifications to it. For an executable work, complete source
code means all the source code for all modules it contains, plus any
associated interface definition files, plus the scripts used to
control compilation and installation of the executable. However, as a
special exception, the source code distributed need not include
anything that is normally distributed (in either source or binary
form) with the major components (compiler, kernel, and so on) of the
operating system on which the executable runs, unless that component
itself accompanies the executable.
If distribution of executable or object code is made by offering
access to copy from a designated place, then offering equivalent
access to copy the source code from the same place counts as
distribution of the source code, even though third parties are not
compelled to copy the source along with the object code.
4. You may not copy, modify, sublicense, or distribute the Program
except as expressly provided under this License. Any attempt
otherwise to copy, modify, sublicense or distribute the Program is
void, and will automatically terminate your rights under this License.
However, parties who have received copies, or rights, from you under
this License will not have their licenses terminated so long as such
parties remain in full compliance.
5. You are not required to accept this License, since you have not
signed it. However, nothing else grants you permission to modify or
distribute the Program or its derivative works. These actions are
prohibited by law if you do not accept this License. Therefore, by
modifying or distributing the Program (or any work based on the
Program), you indicate your acceptance of this License to do so, and
all its terms and conditions for copying, distributing or modifying
the Program or works based on it.
6. Each time you redistribute the Program (or any work based on the
Program), the recipient automatically receives a license from the
original licensor to copy, distribute or modify the Program subject to
these terms and conditions. You may not impose any further
restrictions on the recipients' exercise of the rights granted herein.
You are not responsible for enforcing compliance by third parties to
this License.
7. If, as a consequence of a court judgment or allegation of patent
infringement or for any other reason (not limited to patent issues),
conditions are imposed on you (whether by court order, agreement or
otherwise) that contradict the conditions of this License, they do not
excuse you from the conditions of this License. If you cannot
distribute so as to satisfy simultaneously your obligations under this
License and any other pertinent obligations, then as a consequence you
may not distribute the Program at all. For example, if a patent
license would not permit royalty-free redistribution of the Program by
all those who receive copies directly or indirectly through you, then
the only way you could satisfy both it and this License would be to
refrain entirely from distribution of the Program.
If any portion of this section is held invalid or unenforceable under
any particular circumstance, the balance of the section is intended to
apply and the section as a whole is intended to apply in other
circumstances.
It is not the purpose of this section to induce you to infringe any
patents or other property right claims or to contest validity of any
such claims; this section has the sole purpose of protecting the
integrity of the free software distribution system, which is
implemented by public license practices. Many people have made
generous contributions to the wide range of software distributed
through that system in reliance on consistent application of that
system; it is up to the author/donor to decide if he or she is willing
to distribute software through any other system and a licensee cannot
impose that choice.
This section is intended to make thoroughly clear what is believed to
be a consequence of the rest of this License.
8. If the distribution and/or use of the Program is restricted in
certain countries either by patents or by copyrighted interfaces, the
original copyright holder who places the Program under this License
may add an explicit geographical distribution limitation excluding
those countries, so that distribution is permitted only in or among
countries not thus excluded. In such case, this License incorporates
the limitation as if written in the body of this License.
9. The Free Software Foundation may publish revised and/or new versions
of the General Public License from time to time. Such new versions will
be similar in spirit to the present version, but may differ in detail to
address new problems or concerns.
Each version is given a distinguishing version number. If the Program
specifies a version number of this License which applies to it and "any
later version", you have the option of following the terms and conditions
either of that version or of any later version published by the Free
Software Foundation. If the Program does not specify a version number of
this License, you may choose any version ever published by the Free Software
Foundation.
10. If you wish to incorporate parts of the Program into other free
programs whose distribution conditions are different, write to the author
to ask for permission. For software which is copyrighted by the Free
Software Foundation, write to the Free Software Foundation; we sometimes
make exceptions for this. Our decision will be guided by the two goals
of preserving the free status of all derivatives of our free software and
of promoting the sharing and reuse of software generally.
NO WARRANTY
11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS
TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE
PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
REPAIR OR CORRECTION.
12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
POSSIBILITY OF SUCH DAMAGES.
END OF TERMS AND CONDITIONS
Appendix: How to Apply These Terms to Your New Programs
If you develop a new program, and you want it to be of the greatest
possible use to the public, the best way to achieve this is to make it
free software which everyone can redistribute and change under these terms.
To do so, attach the following notices to the program. It is safest
to attach them to the start of each source file to most effectively
convey the exclusion of warranty; and each file should have at least
the "copyright" line and a pointer to where the full notice is found.
<one line to give the program's name and a brief idea of what it does.>
Copyright (C) 19yy <name of author>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
Also add information on how to contact you by electronic and paper mail.
If the program is interactive, make it output a short notice like this
when it starts in an interactive mode:
Gnomovision version 69, Copyright (C) 19yy name of author
Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
This is free software, and you are welcome to redistribute it
under certain conditions; type `show c' for details.
The hypothetical commands `show w' and `show c' should show the appropriate
parts of the General Public License. Of course, the commands you use may
be called something other than `show w' and `show c'; they could even be
mouse-clicks or menu items--whatever suits your program.
You should also get your employer (if you work as a programmer) or your
school, if any, to sign a "copyright disclaimer" for the program, if
necessary. Here is a sample; alter the names:
Yoyodyne, Inc., hereby disclaims all copyright interest in the program
`Gnomovision' (which makes passes at compilers) written by James Hacker.
<signature of Ty Coon>, 1 April 1989
Ty Coon, President of Vice
This General Public License does not permit incorporating your program into
proprietary programs. If your program is a subroutine library, you may
consider it more useful to permit linking proprietary applications with the
library. If this is what you want to do, use the GNU Library General
Public License instead of this License.
# Makefile
#
INSTALL_PATH = /usr/local
# For SCO
CFLAGS = -b elf -O -D_SVID
# For IRIX
CFLAGS = -xansi -fullwarn -O3 -g0
# For Solaris
CFLAGS = -fast -xO4 -s -v -Xa
# For HPUX
CFLAGS = -Wall -O -Ae
# For OSF1
CFLAGS = -w -verbose -fast -std1 -g0
# For GNU C compiler
CFLAGS = -Wall -O6 -pedantic
SHELL = /bin/sh
all: shc ask_for_test
shc: shc.c
$(CC) $(CFLAGS) $< -o $@
ask_for_test:
@echo '*** Do you want to probe shc with a test script?'
@echo '*** Please try... make test'
test: make_the_test ask_for_strings
make_the_test: match.x
@echo '*** Running a compiled test script!'
@echo '*** It must show files with substring "sh" in your PATH...'
./match.x sh
match.x: match.x.c
$(CC) $(CFLAGS) $< -o $@
match.x.c: shc match
@echo '*** Compiling script "match"'
CFLAGS="$(CFLAGS)" ./shc -v -r -f match
ask_for_strings:
@echo '*** Do you want to see strings in the generated binary?'
@echo '*** Please try... make strings'
strings: make_the_strings ask_for_install
make_the_strings: match.x
@echo '*** Running: "strings -n 5 '$<'"'
@echo '*** It must show no sensible information...'
strings -n 5 $<
ask_for_install:
@echo '*** Do you want to install shc?'
@echo '*** Please try... make install'
install: shc
@echo '*** Installing shc and shc.1 on '$(INSTALL_PATH)
@echo -n '*** Do you want to continue? '; read ANS; case "$$ANS" in y|Y|yes|Yes|YES) ;; *) exit 1;; esac;
install -c -s shc $(INSTALL_PATH)/bin/
install -c -m 644 shc.1 $(INSTALL_PATH)/man/man1/
clean:
rm -f *.o *~ *.x.c
cleanall: clean
rm -f shc *.x
#
# sed script to wrapp C code as a C string.
#
# Duplicate unquoted %
s/[^\\]%/&%/g
#
# Unquote quoted %
s/\\%/%/g
#
# Duplicate \
s/\\/\\\\/g
#
# Quote "
s/"/\\"/g
#
# End each line with new-line
s/^.*$/&\\n/
#
# Enclose each line within "
s/^.*$/"&"/
#
#!/bin/sh
#
# File search script by <workexp@csc.liv.ac.uk>
# Usage: match [Filename]
#
IFS=":$IFS"
for substring in $@
do
for path in $PATH
do
for command in $path/*$substring*
do
if [ -f $command ]
then
echo $command
fi
done
done
done
# Added
echo "[$$] PAUSED... Hit return!"
read DUMMY
#! /bin/sh -x
echo "$0" "$@"
ps $$
ps wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww $$
cat /proc/$$/cmdline
touch $0.kk
read ENTER
#
# sed script to extract C code wrapped as a C string.
#
# Remove "s enclossing each line
s/^"\(.*\)"$/\1/
#
# Remove trailing new-line
s/^\(.*\)\\n$/\1/
#
# Unquote "
s/\\"/"/g
#
# Unduplicate \
s/\\\\/\\/g
#
# Quote unquoted and unduplicated %
s/\([^\\%]\)%\([^%]\)/\1\\%\2/g
#
# Unduplicate unquoted %
s/\([^\\]%\)%/\1/g
.TH shc 1 "Jun 18, 2003" "shc Version 3.7"
.UC 4
.SH "NAME"
shc - Generic shell script compiler
.SH "SYNOPSIS"
.B shc
[ \-\fBe\fP date ]
[ \-\fBm\fP addr ]
[ \-\fBi\fP iopt ]
[ \-\fBx\fP cmnd ]
.br
[ \-\fBl\fP lopt ]
[ \-\fBACDhTv\fP ]
\-\fBf\fP script
.SH "DESCRIPTION"
.B shc
creates a stripped binary executable version of the script
specified with \fI\-f\fP on the command line.
The binary version will get a \fI.x\fP extension appended
and will usually be a bit larger in size than the original ascii code.
Generated C source code is saved in a file with the extension \fI.x.c\fP
If you supply an expiration date with the \fI\-e\fP option the
compiled binary will refuse to run after the date specified.
The message \fB"Please contact your provider"\fP will be displayed instead.
This message can be changed with the \fI\-m\fP option.
You can compile any kind of shell script, but you need to supply valid
\fI\-i\fP, \fI\-x\fP and \fI\-l\fP options.
The compiled binary will still be dependent on the shell specified
in the first line of the shell code (i.e. #!/bin/sh), thus \fBshc\fP does not create
completely independent binaries.
\fBshc\fP itself is not a compiler such as cc, it rather encodes and
encrypts a shell script and generates C source code with the added expiration
capability. It then uses the system compiler to compile a stripped binary
which behaves exactly like the original script. Upon execution, the compiled binary
will decrypt and execute the code with the shell \fI-c\fP option.
Unfortunatelly, it will not give you any speed improvement as a real C program would.
\fBshc\fP's main purpose is to protect your shell scripts from modification or
inspection. You can use it if you wish to distribute your scripts but don't
want them to be easily readable by other people.
.SH "OPTIONS"
The command line options are:
.TP
.B -e date
Expiration date in dd/mm/yyyy format [none]
.TP
.B -m message
message to display upon expiration ["Please contact your provider"]
.TP
.B -f script_name
File name of the script to compile
.TP
.B -i inline_option
Inline option for the shell interpreter i.e: -e
.TP
.B -x comand
eXec command, as a printf format i.e: exec(\\\\'%s\\\\',@ARGV);
.TP
.B -l last_option
Last shell option i.e: --
.TP
.B -r
Relax security. Make a redistributable binary which executes on
different systems running the same operating system.
.TP
.B -v
Verbose compilation
.TP
.B -D
Switch on debug exec calls
.TP
.B -T
Allow binary to be traceable (using strace, ptrace, truss, etc.)
.TP
.B -C
Display license and exit
.TP
.B -A
Display abstract and exit
.TP
.B -h
Display help and exit
.SH "ENVIRONMENT VARIABLES"
.TP
.B CC
C compiler command [cc]
.TP
.B CFLAGS
C compiler flags [none]
.SH "EXAMPLES"
.SM
Compile a script which can be run on other systems with the trace
option enabled:
example% \fBshc\fP -v -r -T -f myscript
.SH "BUGS"
The maximum size of the script that could be executed once compiled is limited
by the operating system configuration parameter
.B _SC_ARG_MAX
(see
.BR sysconf ( 2 )\c
)
.SH "AUTHOR"
Francisco Rosales
<frosal@fi.upm.es>
.SH "REPORT BUGS TO"
the author.
Purpose:
A generic shell script compiler. Shc takes a script, which is
specified on the command line and produces C source code. The
generated source code is then compiled and linked to produce a
stripped binary executable. Use with care.
Building:
Just do a "make"
Testing:
Try: "make test"
or: "shc -v -f match" then "match.x sh"
Caveat emptor: see Copyright
The results look fine to me, but I havn't used this in anger, but
the author has used shc for his work widely over SunOS, Solaris and
Linux, and done some testing on Irix and HPUX.
We tested it on a few SMALL ksh scripts - big tasks should probably
be written in C in the first place (see _SC_ARG_MAX below)!
Bugs:
The one (and I hope the only) limitation using shc is the
_SC_ARG_MAX system configuration parameter.
It limits the maximum length of the arguments to the exec function,
limiting the maximum length of the runnable script of shc.
!! - CHECK YOUR RESULTS CAREFULLY BEFORE USING - !!
Archived at: ftp://hpux.csc.liv.ac.uk/hpux/Languages/shc-2.4a
Archived by: steff@csc.liv.ac.uk
Author: Francisco Rosales Garcia
--------------------------------------------------------------------
Francisco Rosales García <frosal@fi.upm.es> TEL: +341 336 73 80
http://www.datsi.fi.upm.es/~frosal FAX: +34 1 336 73 73
Departamento de Arquitectura y Tecnología de Sistemas Informáticos
Facultad de Informática. Universidad Politécnica de Madrid. España
--------------------------------------------------------------------
/* shc.c */
/**
* This software contains the 'Alleged RC4' source code.
* The original source code was published on the Net by a group of cypherpunks.
* I picked up a modified version from the news.
* The copyright notice does not apply to that code.
*/
static const char my_name[] = "shc";
static const char version[] = "Version 3.7";
static const char subject[] = "Generic Script Compiler";
static const char cpright[] = "Copyright (c) 1994-2003";
static const struct { const char * f, * s, * e; }
author = { "Francisco", "Rosales", "<frosal@fi.upm.es>" };
static const char * copying[] = {
"Copying:",
"",
" This program is free software; you can redistribute it and/or modify",
" it under the terms of the GNU General Public License as published by",
" the Free Software Foundation; either version 2 of the License, or",
" (at your option) any later version.",
"",
" This program is distributed in the hope that it will be useful,",
" but WITHOUT ANY WARRANTY; without even the implied warranty of",
" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the",
" GNU General Public License for more details.",
"",
" You should have received a copy of the GNU General Public License",
" along with this program; if not, write to the Free Software",
" Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.",
"",
" Report problems and questions to:",
"",
0};
static const char * abstract[] = {
"Abstract:",
"",
" This tool generates a stripped binary executable version",
" of the script specified at command line.",
"",
" Binary version will be saved with a .x extension.",
"",
" You can specify expiration date [-e] too, after which binary will",
" refuse to be executed, displaying \"[-m]\" instead.",
"",
" You can compile whatever interpreted script, but valid [-i], [-x]",
" and [-l] options must be given.",
"",
0};
static const char usage[] =
"Usage: shc [-e date] [-m addr] [-i iopt] [-x cmnd] [-l lopt] [-rvDTCAh] -f script";
static const char * help[] = {
"",
" -e %s Expiration date in dd/mm/yyyy format [none]",
" -m %s Message to display upon expiration [\"Please contact your provider\"]",
" -f %s File name of the script to compile",
" -i %s Inline option for the shell interpreter i.e: -e",
" -x %s eXec command, as a printf format i.e: exec('%s',@ARGV);",
" -l %s Last shell option i.e: --",
" -r Relax security. Make a redistributable binary",
" -v Verbose compilation",
" -D Switch ON debug exec calls [OFF]",
" -T Allow binary to be traceable [no]",
" -C Display license and exit",
" -A Display abstract and exit",
" -h Display help and exit",
"",
" Environment variables used:",
" Name Default Usage",
" CC cc C compiler command",
" CFLAGS <none> C compiler flags",
"",
" Please consult the shc(1) man page.",
"",
0};
#include <sys/types.h>
#include <sys/stat.h>
#include <assert.h>
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#define SIZE 4096
static char * file;
static long date;
static char * mail = "Please contact your provider";
static int relax;
static char * shll;
static char * inlo;
static char * xecc;
static char * lsto;
static char * opts;
static char * text;
static int verbose;
static const char DEBUGEXEC_line[] =
"#define DEBUGEXEC %d /* Define as 1 to debug execvp calls */\n";
static int DEBUGEXEC_flag;
static const char TRACEABLE_line[] =
"#define TRACEABLE %d /* Define as 1 to enable ptrace the executable */\n";
static int TRACEABLE_flag;
static const char * RTC[] = {
"",
"/* rtc.c */",
"",
"#include <sys/stat.h>",
"#include <sys/types.h>",
"",
"#include <errno.h>",
"#include <stdio.h>",
"#include <stdlib.h>",
"#include <string.h>",
"#include <time.h>",
"#include <unistd.h>",
"",
"/**",
" * 'Alleged RC4' Source Code picked up from the news.",
" * From: allen@gateway.grumman.com (John L. Allen)",
" * Newsgroups: comp.lang.c",
" * Subject: Shrink this C code for fame and fun",
" * Date: 21 May 1996 10:49:37 -0400",
" */",
"",
"static unsigned char state[256], indx, jndx;",
"",
"/*",
" * Reset rc4 state. ",
" */",
"void state_0(void)",
"{",
" indx = jndx = 0;",
" do {",
" state[indx] = indx;",
" } while (++indx);",
"}",
"",
"/*",
" * Set key. Can be used more than once. ",
" */",
"void key(char * str, int len)",
"{",
" unsigned char tmp, * ptr = (unsigned char *)str;",
" while (len > 0) {",
" do {",
" tmp = state[indx];",
" jndx += tmp;",
" jndx += ptr[(int)indx % len];",
" state[indx] = state[jndx];",
" state[jndx] = tmp;",
" } while (++indx);",
" ptr += 256;",
" len -= 256;",
" }",
"}",
"",
"/*",
" * Crypt data. ",
" */",
"void rc4(char * str, int len)",
"{",
" unsigned char tmp, * ptr = (unsigned char *)str;",
" jndx = 0;",
" while (len > 0) {",
" indx++;",
" tmp = state[indx];",
" jndx += tmp;",
" state[indx] = state[jndx];",
" state[jndx] = tmp;",
" tmp += state[indx];",
" *ptr ^= state[tmp];",
" ptr++;",
" len--;",
" }",
"}",
"",
"/*",
" * Key with file invariants. ",
" */",
"int key_with_file(char * file)",
"{",
" struct stat statf[1];",
" struct stat control[1];",
"",
" if (stat(file, statf) < 0)",
" return -1;",
"",
" /* Turn on stable fields */",
" memset(control, 0, sizeof(control));",
" control->st_ino = statf->st_ino;",
" control->st_dev = statf->st_dev;",
" control->st_rdev = statf->st_rdev;",
" control->st_uid = statf->st_uid;",
" control->st_gid = statf->st_gid;",
" control->st_size = statf->st_size;",
" control->st_mtime = statf->st_mtime;",
" control->st_ctime = statf->st_ctime;",
" key((char *)control, sizeof(control));",
" return 0;",
"}",
"",
"#if DEBUGEXEC",
"void debugexec(char * shll, int argc, char ** argv)",
"{",
" int i;",
" fprintf(stderr, \"shll=%s\\n\", shll ? shll : \"<null>\");",
" fprintf(stderr, \"argc=%d\\n\", argc);",
" if (!argv) {",
" fprintf(stderr, \"argv=<null>\\n\");",
" } else { ",
" for (i = 0; i <= argc ; i++)",
" fprintf(stderr, \"argv[%d]=%.60s\\n\", i, argv[i] ? argv[i] : \"<null>\");",
" }",
"}",
"#endif /* DEBUGEXEC */",
"",
"void rmarg(char ** argv, char * arg)",
"{",
" for (; argv && *argv && *argv != arg; argv++);",
" for (; argv && *argv; argv++)",
" *argv = argv[1];",
"}",
"",
"int chkenv(int argc)",
"{",
" char buff[512];",
" unsigned mask, m;",
" int l, a, c;",
" char * string;",
" extern char ** environ;",
"",
" mask = (unsigned)chkenv;",
" mask ^= (unsigned)getpid() * ~mask;",
" sprintf(buff, \"x%x\", mask);",
" string = getenv(buff);",
"#if DEBUGEXEC",
" fprintf(stderr, \"getenv(%s)=%s\\n\", buff, string ? string : \"<null>\");",
"#endif",
" l = strlen(buff);",
" if (!string) {",
" /* 1st */",
" sprintf(&buff[l], \"=%u %d\", mask, argc);",
" putenv(strdup(buff));",
" return 0;",
" }",
" c = sscanf(string, \"%u %d%c\", &m, &a, buff);",
" if (c == 2 && m == mask) {",
" /* 3rd */",
" rmarg(environ, &string[-l - 1]);",
" return 1 + (argc - a);",
" }",
" return -1;",
"}",
"",
"#if !TRACEABLE",
"",
"#define _LINUX_SOURCE_COMPAT",
"#include <sys/ptrace.h>",
"#include <sys/types.h>",
"#include <sys/wait.h>",
"#include <fcntl.h>",
"#include <signal.h>",
"#include <stdio.h>",
"#include <unistd.h>",
"",
"void untraceable(char * argv0)",
"{",
" char proc[80];",
" int pid, mine;",
"",
" switch(pid = vfork()) {",
" case 0:",
" pid = getppid();",
" /* For problematic SunOS ptrace */",
" sprintf(proc, \"/proc/%d/as\", (int)pid);",
" close(0);",
" mine = !open(proc, O_RDWR|O_EXCL);",
" if (!mine && errno != EBUSY)",
" mine = !ptrace(PTRACE_ATTACH, pid, 0, 0);",
" if (mine) {",
" kill(pid, SIGCONT);",
" } else {",
" fprintf(stderr, \"%s is being traced!\\n\", argv0);",
" kill(pid, SIGKILL);",
" }",
" _exit(mine);",
" case -1:",
" break;",
" default:",
" if (pid == waitpid(pid, 0, 0))",
" return;",
" }",
" perror(argv0);",
" _exit(1);",
"}",
"#endif /* !TRACEABLE */",
"",
"char * xsh(int argc, char ** argv)",
"{",
" char buff[512];",
" char * scrpt;",
" int ret, i, j;",
" char ** varg;",
"",
" state_0();",
" key(pswd, sizeof(pswd_t));",
" rc4(shll, sizeof(shll_t));",
" rc4(inlo, sizeof(inlo_t));",
" rc4(xecc, sizeof(xecc_t));",
" rc4(lsto, sizeof(lsto_t));",
" rc4(chk1, sizeof(chk1_t));",
" if (strcmp(TEXT_chk1, chk1))",
" return \"location has changed!\";",
" ret = chkenv(argc);",
" if (ret < 0)",
" return \"abnormal behavior!\";",
" varg = (char **)calloc(argc + 10, sizeof(char *));",
" if (!varg)",
" return 0;",
" if (ret) {",
" if (!relax && key_with_file(shll))",
" return shll;",
" rc4(opts, sizeof(opts_t));",
" rc4(text, sizeof(text_t));",
" rc4(chk2, sizeof(chk2_t));",
" if (strcmp(TEXT_chk2, chk2))",
" return \"shell has changed!\";",
" if (sizeof(text_t) < sizeof(hide_t)) {",
" /* Prepend spaces til a sizeof(hide_t) script size. */",
" scrpt = malloc(sizeof(hide_t));",
" if (!scrpt)",
" return 0;",
" memset(scrpt, (int) ' ', sizeof(hide_t));",
" memcpy(&scrpt[sizeof(hide_t) - sizeof(text_t)], text, sizeof(text_t));",
" } else {",
" scrpt = text; /* Script text */",
" }",
" } else { /* Reexecute */",
" if (*xecc) {",
" sprintf(buff, xecc, argv[0]);",
" scrpt = buff;",
" } else {",
" scrpt = argv[0];",
" }",
" }",
" j = 0;",
" varg[j++] = argv[0]; /* My own name at execution */",
" if (ret && *opts)",
" varg[j++] = opts; /* Options on 1st line of code */",
" if (*inlo)",
" varg[j++] = inlo; /* Option introducing inline code */",
" varg[j++] = scrpt; /* The script itself */",
" if (*lsto)",
" varg[j++] = lsto; /* Option meaning last option */",
" i = (ret > 1) ? ret : 0; /* Args numbering correction */",
" while (i < argc)",
" varg[j++] = argv[i++]; /* Main run-time arguments */",
" varg[j] = 0; /* NULL terminated array */",
"#if DEBUGEXEC",
" debugexec(shll, j, varg);",
"#endif",
" execvp(shll, varg);",
" return shll;",
"}",
"",
"int main(int argc, char ** argv)",
"{",
"#if DEBUGEXEC",
" debugexec(\"main\", argc, argv);",
"#endif",
"#if !TRACEABLE",
" untraceable(argv[0]);",
"#endif",
" if (date && (date < (long)time(NULL))) {",
" fprintf(stderr, \"%s has expired!\\n\", argv[0]);",
" fprintf(stderr, \"%s\\n\", mail);",
" } else {",
" argv[1] = xsh(argc, argv);",
" fprintf(stderr, \"%s%s%s: %s\\n\", argv[0],",
" errno ? \": \" : \"\",",
" errno ? strerror(errno) : \"\",",
" argv[1] ? argv[1] : \"<null>\"",
" );",
" }",
" return 1;",
"}",
0};
static int parse_an_arg(int argc, char * argv[])
{
extern char * optarg;
const char * opts = "e:m:f:i:x:l:rvDTCAh";
struct tm tmp[1];
int cnt, l;
char ctrl;
switch (getopt(argc, argv, opts)) {
case 'e':
memset(tmp, 0, sizeof(tmp));
cnt = sscanf(optarg, "%2d/%2d/%4d%c",
&tmp->tm_mday, &tmp->tm_mon, &tmp->tm_year, &ctrl);
if (cnt == 3) {
tmp->tm_mon--;
tmp->tm_year -= 1900;
date = (long)mktime(tmp);
}
if (cnt != 3 || date == -1) {
fprintf(stderr, "%s parse(-e %s): Not a valid value\n",
my_name, optarg);
return -1;
}
break;
case 'm':
mail = optarg;
break;
case 'f':
if (file) {
fprintf(stderr, "%s parse(-f): Specified more than once\n",
my_name);
return -1;
}
file = optarg;
break;
case 'i':
inlo = optarg;
break;
case 'x':
xecc = optarg;
break;
case 'l':
lsto = optarg;
break;
case 'r':
relax++;
break;
case 'v':
verbose++;
break;
case 'D':
DEBUGEXEC_flag = 1;
break;
case 'T':
TRACEABLE_flag = 1;
break;
case 'C':
fprintf(stderr, "%s %s, %s\n", my_name, version, subject);
fprintf(stderr, "%s %s %s %s %s\n", my_name, cpright, author.f, author.s, author.e);
fprintf(stderr, "%s ", my_name);
for (l = 0; copying[l]; l++)
fprintf(stderr, "%s\n", copying[l]);
fprintf(stderr, " %s %s %s\n\n", author.f, author.s, author.e);
exit(0);
break;
case 'A':
fprintf(stderr, "%s %s, %s\n", my_name, version, subject);
fprintf(stderr, "%s %s %s %s %s\n", my_name, cpright, author.f, author.s, author.e);
fprintf(stderr, "%s ", my_name);
for (l = 0; abstract[l]; l++)
fprintf(stderr, "%s\n", abstract[l]);
exit(0);
break;
case 'h':
fprintf(stderr, "%s %s, %s\n", my_name, version, subject);
fprintf(stderr, "%s %s %s %s %s\n", my_name, cpright, author.f, author.s, author.e);
fprintf(stderr, "%s %s\n", my_name, usage);
for (l = 0; help[l]; l++)
fprintf(stderr, "%s\n", help[l]);
exit(0);
break;
case -1:
if (!file) {
fprintf(stderr, "%s parse(-f): No source file specified\n", my_name);
file = "";
return -1;
}
return 0;
case ':':
fprintf(stderr, "%s parse: Missing parameter\n", my_name);
return -1;
case '?':
fprintf(stderr, "%s parse: Unknown option\n", my_name);
return -1;
default:
fprintf(stderr, "%s parse: Unknown return\n", my_name);
return -1;
}
return 1;
}
static void parse_args(int argc, char * argv[])
{
int err = 0;
int ret;
#if 0
my_name = strrchr(argv[0], '/');
if (my_name)
my_name++;
else
my_name = argv[0];
#endif
do {
ret = parse_an_arg(argc, argv);
if (ret == -1)
err++;
} while (ret);
if (err) {
fprintf(stderr, "\n%s %s\n\n", my_name, usage);
exit(1);
}
}
/**
* 'Alleged RC4' Source Code picked up from the news.
* From: allen@gateway.grumman.com (John L. Allen)
* Newsgroups: comp.lang.c
* Subject: Shrink this C code for fame and fun
* Date: 21 May 1996 10:49:37 -0400
*/
static unsigned char state[256], indx, jndx;
/*
* Reset rc4 state.
*/
void state_0(void)
{
indx = jndx = 0;
do {
state[indx] = indx;
} while (++indx);
}
/*
* Set key. Can be used more than once.
*/
void key(char * str, int len)
{
unsigned char tmp, * ptr = (unsigned char *)str;
while (len > 0) {
do {
tmp = state[indx];
jndx += tmp;
jndx += ptr[(int)indx % len];
state[indx] = state[jndx];
state[jndx] = tmp;
} while (++indx);
ptr += 256;
len -= 256;
}
}
/*
* Crypt data.
*/
void rc4(char * str, int len)
{
unsigned char tmp, * ptr = (unsigned char *)str;
jndx = 0;
while (len > 0) {
indx++;
tmp = state[indx];
jndx += tmp;
state[indx] = state[jndx];
state[jndx] = tmp;
tmp += state[indx];
*ptr ^= state[tmp];
ptr++;
len--;
}
}
/*
* Key with file invariants.
*/
int key_with_file(char * file)
{
struct stat statf[1];
struct stat control[1];
if (stat(file, statf) < 0)
return -1;
/* Turn on stable fields */
memset(control, 0, sizeof(control));
control->st_ino = statf->st_ino;
control->st_dev = statf->st_dev;
control->st_rdev = statf->st_rdev;
control->st_uid = statf->st_uid;
control->st_gid = statf->st_gid;
control->st_size = statf->st_size;
control->st_mtime = statf->st_mtime;
control->st_ctime = statf->st_ctime;
key((char *)control, sizeof(control));
return 0;
}
/*
* NVI stands for Shells that complaint "Not Valid Identifier" on
* environment variables with characters as "=|#:*?$ ".
*/
struct {
char * shll;
char * inlo;
char * lsto;
char * xecc;
} shellsDB[] = {
{ "perl", "-e", "--", "exec('%s',@ARGV);" },
{ "rc", "-c", "", "builtin exec %s $*" },
{ "sh", "-c", "", "exec '%s' \"$@\"" }, /* IRIX_nvi */
{ "bash", "-c", "", "exec '%s' \"$@\"" },
{ "bsh", "-c", "", "exec '%s' \"$@\"" }, /* AIX_nvi */
{ "Rsh", "-c", "", "exec '%s' \"$@\"" }, /* AIX_nvi */
{ "ksh", "-c", "", "exec '%s' \"$@\"" }, /* OK on Solaris, AIX and Linux (THX <bryan.hogan@dstintl.com>) */
{ "tsh", "-c", "--", "exec '%s' \"$@\"" }, /* AIX */
{ "ash", "-c", "--", "exec '%s' \"$@\"" }, /* Linux */
{ "csh", "-c", "-b", "exec '%s' $argv" }, /* AIX: No file for $0 */
{ "tcsh", "-c", "-b", "exec '%s' $argv" },
{ NULL, NULL, NULL, NULL },
};
int eval_shell(char * text)
{
int i;
char * ptr;
ptr = strchr(text, (int)'\n');
if (!ptr)
i = strlen(text);
else
i = ptr - text;
ptr = malloc(i + 1);
shll = malloc(i + 1);
opts = malloc(i + 1);
if (!ptr || !shll || !opts)
return -1;
strncpy(ptr, text, i);
ptr[i] = '\0';
*opts = '\0';
i = sscanf(ptr, " #!%s%s %c", shll, opts, opts);
if (i < 1 || i > 2) {
fprintf(stderr, "%s: invalid first line in script: %s\n", my_name, ptr);
return -1;
}
free(ptr);
shll = realloc(shll, strlen(shll) + 1);
ptr = strrchr(shll, (int)'/');
if (*ptr == '/')
ptr++;
if (verbose) fprintf(stderr, "%s shll=%s\n", my_name, ptr);
for(i=0; shellsDB[i].shll; i++) {
if(!strcmp(ptr, shellsDB[i].shll)) {
if (!inlo)
inlo = strdup(shellsDB[i].inlo);
if (!xecc)
xecc = strdup(shellsDB[i].xecc);
if (!lsto)
lsto = strdup(shellsDB[i].lsto);
}
}
if (!inlo || !xecc || !lsto) {
fprintf(stderr, "%s Unknown shell (%s): specify [-i][-x][-l]\n", my_name, ptr);
return -1;
}
if (verbose) fprintf(stderr, "%s [-i]=%s\n", my_name, inlo);
if (verbose) fprintf(stderr, "%s [-x]=%s\n", my_name, xecc);
if (verbose) fprintf(stderr, "%s [-l]=%s\n", my_name, lsto);
opts = realloc(opts, strlen(opts) + 1);
if (*opts && !strcmp(opts, lsto)) {
fprintf(stderr, "%s opts=%s : Is equal to [-l]. Removing opts\n", my_name, opts);
*opts = '\0';
} else if (!strcmp(opts, "-")) {
fprintf(stderr, "%s opts=%s : No real one. Removing opts\n", my_name, opts);
*opts = '\0';
}
if (verbose) fprintf(stderr, "%s opts=%s\n", my_name, opts);
return 0;
}
char * read_script(char * file)
{
FILE * i;
char * text;
int cnt, l;
text = malloc(SIZE);
if (!text)
return NULL;
i = fopen(file, "r");
if (!i)
return NULL;
for (l = 0;;) {
text = realloc(text, l + SIZE);
if (!text)
return NULL;
cnt = fread(&text[l], 1, SIZE, i);
if (!cnt)
break;
l += cnt;
}
fclose(i);
text = realloc(text, l + 1);
if (!text)
return NULL;
text[l] = '\0';
/* Check current System ARG_MAX limit. */
if (l > 0.80 * (cnt = sysconf(_SC_ARG_MAX))) {
fprintf(stderr, "%s: WARNING!!\n"
" Scripts of length near to (or higher than) the current System limit on\n"
" \"maximum size of arguments to EXEC\", could comprise its binary execution.\n"
" In the current System the call sysconf(_SC_ARG_MAX) returns %d bytes\n"
" and your script \"%s\" is %d bytes length.\n",
my_name, cnt, file, l);
}
return text;
}
int noise(char * ptr, unsigned min, unsigned xtra, int str)
{
if (xtra) xtra = rand() % xtra;
xtra += min;
for (min = 0; min < xtra; min++, ptr++)
do
*ptr = (char) rand();
while (str && !isalnum(*ptr));
if (str) *ptr = '\0';
return xtra;
}
void print_bytes(FILE * o, char * ptr, int l, int n)
{
int i;
for (i = 0; i < n; i++) {
if ((i & 0xf) == 0)
fprintf(o, "\n\t\"");
fprintf(o, "\\%03o", (unsigned char)(i < l ? ptr[i] : rand()));
if ((i & 0xf) == 0xf)
fprintf(o, "\"");
}
if ((i & 0xf) != 0)
fprintf(o, "\"");
}
void print_array(FILE * o, char * ptr, char * name, int l)
{
fprintf(o, "typedef char %s_t[%d];\n", name, l);
fprintf(o, "static char %s[] = ", name);
print_bytes(o, ptr, l, l + (rand() & 0xf));
fprintf(o, ";\n");
}
void dump_array(FILE * o, char * ptr, char * name, int l)
{
rc4(ptr, l);
print_array(o, ptr, name, l);
}
int write_C(char * file, char * argv[])
{
FILE * o;
char buf[SIZE];
int l;
sprintf(buf, "%s.x.c", file);
o = fopen(buf, "w");
if (!o)
return -1;
srand((unsigned)time(NULL));
fprintf(o, "#if 0\n");
fprintf(o, "\t%s %s, %s\n", my_name, version, subject);
fprintf(o, "\t%s %s %s %s\n\n\t", cpright, author.f, author.s, author.e);
for (l = 0; argv[l]; l++)
fprintf(o, "%s ", argv[l]);
fprintf(o, "\n#endif\n\n");
fprintf(o, "static long date = %ld;\n", date);
fprintf(o, "static char mail[] = \"%s\";\n", mail);
fprintf(o, "static int relax = %d;\n", relax);
l = noise(buf, 256, 256, 0);
dump_array(o, buf, "pswd", l);
state_0();
key(buf, l);
dump_array(o, strdup(shll), "shll", strlen(shll) + 1);
dump_array(o, inlo, "inlo", strlen(inlo) + 1);
dump_array(o, xecc, "xecc", strlen(xecc) + 1);
dump_array(o, lsto, "lsto", strlen(lsto) + 1);
l = noise(buf, 8, 8, 1);
fprintf(o, "#define TEXT_%s \"%s\"\n", "chk1", buf);
dump_array(o, buf, "chk1", l + 1);
if (!relax && key_with_file(shll)) {
fprintf(stderr, "%s: invalid file name: %s", my_name, shll);
perror("");
exit(1);
}
dump_array(o, opts, "opts", strlen(opts) + 1);
dump_array(o, text, "text", strlen(text) + 1);
l = noise(buf, 8, 8, 1);
fprintf(o, "#define TEXT_%s \"%s\"\n", "chk2", buf);
dump_array(o, buf, "chk2", l + 1);
fprintf(o, "typedef char %s_t[%d];\n\n", "hide", 1<<12);
fprintf(o, DEBUGEXEC_line, DEBUGEXEC_flag);
fprintf(o, TRACEABLE_line, TRACEABLE_flag);
for (l = 0; RTC[l]; l++)
fprintf(o, "%s\n", RTC[l]);
fflush(o);
fclose(o);
return 0;
}
int make(void)
{
char * cc, * cflags;
char cmd[SIZE];
cc = getenv("CC");
if (!cc)
cc = "cc";
cflags = getenv("CFLAGS");
if (!cflags)
cflags = "";
sprintf(cmd, "%s %s %s.x.c -o %s.x", cc, cflags, file, file);
if (verbose) fprintf(stderr, "%s: %s\n", my_name, cmd);
if (system(cmd))
return -1;
sprintf(cmd, "strip %s.x", file);
if (verbose) fprintf(stderr, "%s: %s\n", my_name, cmd);
if (system(cmd))
fprintf(stderr, "%s: never mind\n", my_name);
return 0;
}
void do_all(int argc, char * argv[])
{
parse_args(argc, argv);
text = read_script(file);
if (!text)
return;
if (eval_shell(text))
return;
if (write_C(file, argv))
return;
if (make())
return;
exit(0);
}
int main(int argc, char * argv[])
{
putenv("LANG=");
do_all(argc, argv);
/* Return on error */
perror(argv[0]);
exit(1);
return 1;
}
<HTML>
<HEAD>
<TITLE>shc(1)</TITLE>
<link rel=stylesheet type="text/css" href="/css/osix.css">
</HEAD>
<BODY>
<H1>Manpage for shc(1)</H1>
<PRE>
<!-- Manpage converted by man2html 3.0.1 -->
</PRE>
<H2>NAME</H2><PRE>
shc - Generic shell script compiler
</PRE>
<H2>SYNOPSIS</H2><PRE>
shc [ -e date ] [ -m addr ] [ -i iopt ] [ -x cmnd ]
[ -l lopt ] [ -ACDhTv ] -f script
</PRE>
<H2>DESCRIPTION</H2><PRE>
shc creates a stripped binary executable version of the
script specified with -<I>f</I> on the command line.
The binary version will get a .<I>x</I> extension appended and will
usually be a bit larger in size than the original ascii
code. Generated C source code is saved in a file with the
extension .<I>x</I>.<I>c</I>
If you supply an expiration date with the -<I>e</I> option the com-
piled binary will refuse to run after the date specified.
The message "Please contact your provider" will be displayed
instead. This message can be changed with the -<I>m</I> option.
You can compile any kind of shell script, but you need to
supply valid -<I>i</I>, -<I>x</I> and -<I>l</I> options.
The compiled binary will still be dependent on the shell
specified in the first line of the shell code (i.e.
#!/bin/sh), thus shc does not create completely independent
binaries.
shc itself is not a compiler such as cc, it rather encodes
and encrypts a shell script and generates C source code with
the added expiration capability. It then uses the system
compiler to compile a stripped binary which behaves exactly
like the original script. Upon execution, the compiled
binary will decrypt and execute the code with the shell -<I>c</I>
option. Unfortunatelly, it will not give you any speed
improvement as a real C program would.
shc's main purpose is to protect your shell scripts from
modification or inspection. You can use it if you wish to
distribute your scripts but don't want them to be easily
readable by other people.
</PRE>
<H2>OPTIONS</H2><PRE>
The command line options are:
-e date
Expiration date in dd/mm/yyyy format [none]
-m message
message to display upon expiration ["Please contact
your provider"]
-f script_name
File name of the script to compile
-i inline_option
Inline option for the shell interpreter i.e: -e
-x comand
eXec command, as a printf format i.e:
exec(\\'%s\\',@ARGV);
-l last_option
Last shell option i.e: --
-r Relax security. Make a redistributable binary which
executes on different systems running the same operat-
ing system.
-v Verbose compilation
-D Switch on debug exec calls
-T Allow binary to be traceable (using strace, ptrace,
truss, etc.)
-C Display license and exit
-A Display abstract and exit
-h Display help and exit
</PRE>
<H2>ENVIRONMENT VARIABLES</H2><PRE>
CC C compiler command [cc]
CFLAGS
C compiler flags [none]
</PRE>
<H2>EXAMPLES</H2><PRE>
Compile a script which can be run on other systems with the
trace option enabled:
example% shc -v -r -T -f myscript
</PRE>
<H2>BUGS</H2><PRE>
The maximum size of the script that could be executed once com­
piled is limited by the operating system configuration parameter
_SC_ARG_MAX (see <B>sysconf(2)</B>)
</PRE>
<H2>AUTHOR</H2><PRE>
Francisco Rosales &lt;frosal@fi.upm.es&gt;
</PRE>
<H2>REPORT BUGS TO</H2><PRE>
the author.
</PRE>
<P>
</BODY>
</HTML>
#!/bin/bash -x
echo "\$@ is $@"
echo "command line: $0 $*"
echo "hello world"
# Added
echo "[$$] PAUSED... Hit return!"
read DUMMY
exit 0
#!/bin/csh -x
echo Hello world
# A comment
exit 0
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment