convert:not authorized `aaaa` # error/constitute.c/ReadImage/453 - imagemagick

I want to create a captcha pic by use convert from ImageMagick.
And I follow this, but there are some problem .
Input In my linux shell:
convert -background white -fill black -font FreeSerif-Bold -pointsize 36 label:'adfgh' ./test.png
The Error is:
convert:not authorized adfgh # error/constitute.c/ReadImage/453.
convert:missing an image filename ./test.png #
error/convert.c/ConvertImageCommand/3015
My ImageMagick: Version:6.7.2-7 , I install it with yum install ImageMagick .
I'm clueless. Any advice please?

Note: the solution in this and other answers involves disabling safety measures that are there to fix arbitrary code execution vulnerabilities. See for instance this ghostscript-related and this ubuntu-related announcement. Only go forward with these solutions if the input to convert comes from a trusted source.
I use ImageMagick in php (v.7.1) to slice PDF file to images.
First I got errors like:
Exception type: ImagickException
Exception message: not authorized ..... # error/constitute.c/ReadImage/412
After some changes in /etc/ImageMagick-6/policy.xml I start getting erroes like:
Exception type: ImagickException
Exception message: unable to create temporary file ..... Permission denied # error/pdf.c/ReadPDFImage/465
My fix:
In file /etc/ImageMagick-6/policy.xml (or /etc/ImageMagick/policy.xml)
comment line
<!-- <policy domain="coder" rights="none" pattern="MVG" /> -->
change line
<policy domain="coder" rights="none" pattern="PDF" />
to
<policy domain="coder" rights="read|write" pattern="PDF" />
add line
<policy domain="coder" rights="read|write" pattern="LABEL" />
Then restart your web server (nginx, apache).

I use many times the ImageMagic convert command to convert *.tif files to *.pdf files.
I don't know why but today I began to receive the following error:
convert: not authorized `a.pdf' # error/constitute.c/WriteImage/1028.
After issuing the command:
convert a.tif a.pdf
After reading the above answers I edited the file /etc/ImageMagick-6/policy.xml
and changed the line:
policy domain="coder" rights="none" pattern="PDF"
to
policy domain="coder" rights="read|write" pattern="PDF"
and now everything works fine.
I have "ImageMagick 6.8.9-9 Q16 x86_64 2018-09-28"
on "Ubuntu 16.04.5 LTS".

Note: this solution and any other "edit the policy.xml" solution disables safety measures against arbitrary code execution vulnerabilities in ImageMagick. If you need to process input that you do not control 100%, you should use a different program (not ImageMagick).
If you're still here, you are trying to edit images that you have complete control over, know are safe, and cannot be edited by users.
There is an /etc/ImageMagick/policy.xml file that is installed by yum. It disallows almost everything (for security and to protect your system from getting overloaded with ImageMagick calls).
If you're getting a ReadImage error as above, you can change the line to:
<policy domain="coder" rights="read" pattern="LABEL" />
which should fix the issue.
The file has a bunch of documentation in it, so you should read that. For example, if you need more permissions, you can combine them like:
<policy domain="coder" rights="read|write" pattern="LABEL" />
...which is preferable to removing all permissions checks (i.e., deleting or commenting out the line).

If someone need to do it with one command after install, run this !
sed -i 's/<policy domain="coder" rights="none" pattern="PDF" \/>/<policy domain="coder" rights="read|write" pattern="PDF" \/>/g' /etc/ImageMagick-6/policy.xml

Just delete /etc/ImageMagick/policy.xml file. E.g.
rm /etc/<ImageMagick_PATH>/policy.xml
For ImageMagick 6, it's:
sudo rm /etc/ImageMagick-6/policy.xml

The answer with highest votes (I have not enough reputation to add comment there) suggests to comment out the MVG line, but have in mind this:
CVE-2016-3714
ImageMagick supports ".svg/.mvg" files which means that attackers can
craft code in a scripting language, e.g. MSL (Magick Scripting
Language) and MVG (Magick Vector Graphics), upload it to a server
disguised as an image file and force the software to run malicious
commands on the server side as described above. For example adding the
following commands in a file and uploading it to a webserver that uses
a vulnerable ImageMagick version will result in running the command
"ls -la" on the server.
exploit.jpg:
push graphic-context viewbox 0 0 640 480 fill
'url(https://website.com/image.png"|ls "-la)' pop graphic-context
And
Any version below 7.0.1-2 or 6.9.4-0 is potentially vulnerable and
affected parties should as soon as possible upgrade to the latest
ImageMagick version.
Source

After a recent update on my Ubuntu 16.04 system I have also started getting this error when trying to run convert on .ps files to convert them into pdfs.
This fix worked for me:
In a terminal run:
sudo gedit /etc/ImageMagick-6/policy.xml
This should open the policy.xml file in the gedit text editor. If it doesn't, your image magick might be installed in a different place.
Then change
rights="none"
to
rights="read | write"
for PDF, EPS and PS lines near the bottom of the file. Save and exit, and image magick should then work again.

I had this error when trying to convert a pdf to jpg and solved it using this method:
sudo vi /etc/ImageMagick*/policy.xml
change:
policy domain="coder" rights="none" pattern="PDF"
to:
policy domain="coder" rights="read|write" pattern="PDF"
Source: http://realtechtalk.com/ImageMagick_Convert_PDF_Not_Authorized-2217-articles

If you don't need to handle raster files and PDF/PS/EPS through the same tool, don't loosen ImageMagick's security.
Instead, keep your defense in depth for your web applications intact, check that your Ghostscript has been patched for all known -dSAFER vulnerabilities and then invoke it directly.
gs -dSAFER -r300 -sDEVICE=png16m -o document-%03d.png document.pdf
-dSAFER opts you out of the legacy-compatibility "run Postscript will full permission to interact with the outside world as a turing-complete programming language" mode.
-r300 sets the desired DPI to 300 (the default is 72)
-sDEVICE specifies the output format (See the Devices section of the manual for other choices.)
-o is a shorthand for -dBATCH -dNOPAUSE -sOutputFile=
This section of the Ghostscript manual gives some example formats for for multi-file filename output but, for the actual syntax definition, it points you at the documentation for the C printf(3) function.
If you're rendering EPS files, add -dEPSCrop so it won't pad your output to page size and use -sDEVICE=pngalpha to get transparent backgrounds.

After reading several suggestions here and combining the ideas, for me following changes in /etc/ImageMagick-6/policy.xml were necessary:
<policy domain="coder" rights="read|write" pattern="PDF" />
... rights="none" did not help. ...pattern="LABEL" was not neccessary.
Although I do not work with big png files (only ~1 Mb) some changes in memory limits were also necessary:
<policy domain="resource" name="memory" value="2GiB"/>
(instead of 256Mib), and
<policy domain="resource" name="area" value="2GB"/>
(instead of 128 MB)

I also had the error error/constitute.c/ReadImage/453 when trying to convert an eps to a gif with image magick. I tried the solution proposed by sNICkerssss but still had errors (though different from the first one)e error/constitute.c/ReadImage/412
What solved the problem was to put read to other entries
<policy domain="coder" rights="read" pattern="PS" />
<policy domain="coder" rights="read" pattern="EPS" />
<policy domain="coder" rights="read" pattern="PDF" />
<policy domain="coder" rights="read" pattern="XPS" />
<policy domain="coder" rights="read|write" pattern="LABEL" />

Related

how to convert Pdf to image via imagemagick command?

I am using following environment:
Ubunut : 11.0 (virtual machine)
Imagemagick : ImageMagick-6.9.3-7
I have execut following command:
wget http://www.imagemagick.org/download/ImageMagick.tar.gz
tar xzvf ImageMagick.tar.gz
ls
cd ImageMagick<version number here>/
./configure
make clean
make
sudo make install
ldconfig /usr/local/lib
After executing below command:
make check
40 test case 39 pass only 1 is fail (FAIL: tests/wandtest.tap 1)
Now i want to convert pdf file to jpg via command:
convert test.pdf test.jpg
convert: no decode delegate for this image format PNG' # error/constitute.c/ReadImage/501.
convert: no images definedtest.jpg' # error/convert.c/ConvertImageCommand/3252
convert test.pdf test.png
convert: no decode delegate for this image format PNG' # error/constitute.c/ReadImage/501.
convert: no images definedtest.png' # error/convert.c/ConvertImageCommand/3252.
anyone can suggest me how can i do ?
ImageMagick uses assorted external libraries to do its dirty work as far as input and output handling goes for various formats. In your case:
For reading PDFs, you need to have Ghostscript installed. Also it needs to be in your path as gs in order to work, so don't use that for some other shell alias. For more details see: https://superuser.com/q/819277/33767
For writing PNGs you'll need libpng.
Once those things are on your system (and in the case you compiled IM yourself, on your system at the time of compile) you should be golden.
It may be that the issue is not with the writing but with the reading of the PDF itself.
Check the ImageMagick policy.xml.
sudo nano /etc/ImageMagick-6/policy.xml
Find the PDF and change to this:
<policy domain="coder" rights="read|write" pattern="PDF" />
This solved for me.
Same problem. Solved it with:
apt-get -y install ghostscript

cache resources exhausted Imagemagick

I'm using Imagemagick on a rails app with Minimagick and I generate some pictogram with it.
When I launch my process I have this error and I don't find a solution:
MiniMagick::Error (`convert -limit memory 2GiB -limit map 2GiB -limit disk 4GiB -background none -fill #000000 -font ttf/SELIS006N.ttf -pointsize 300 label: S public/pictogram_images/RECINTAN-EL-064-layer-1.png` failed with error:
convert.im6: cache resources exhausted ` S' # error/cache.c/OpenPixelCache/4078.
convert.im6: no images defined `public/pictogram_images/RECINTAN-EL-064-layer-1.png' # error/convert.c/ConvertImageCommand/3044.
):
My process is simple, I have some .tff file and each character is a pictogram. I just want to generate all preview of this character in png.
Find the policy.xml with find / -name "policy.xml"
something like /etc/ImageMagick-6/policy.xml
and change
<policy domain="resource" name="disk" value="1GiB"/>
to
<policy domain="resource" name="disk" value="8GiB"/>
refer to convert fails due to resource limits
Memory issues
The error probably occurs because you run out of memory. You can check for the resources using the following command:
convert -list resource
The output will be somewhat like this:
Resource limits:
Width: 16KP
Height: 16KP
Area: 128MP
Memory: 256MiB
Map: 512MiB
Disk: 1GiB
File: 768
Thread: 8
Throttle: 0
Time: unlimited
Here, you can see that the assigned amounts of disk space and memory are very small. So, in order to modify it you need to change the policy.xml file located somewhere in /etc/ImageMagick-6 directory.
Change <policy domain="resource" name="disk" value="1GiB"/> to <policy domain="resource" name="disk" value="4GiB"/> in the policy.xml file.
sed -i '/disable ghostscript format types/,+6d' /etc/ImageMagick-6/policy.xml //this one is just to solve convertion from .tiff to pdf, you may need it some day
sed -i -E 's/name="memory" value=".+"/name="memory" value="8GiB"/g' /etc/ImageMagick-6/policy.xml
sed -i -E 's/name="map" value=".+"/name="map" value="8GiB"/g' /etc/ImageMagick-6/policy.xml
sed -i -E 's/name="area" value=".+"/name="area" value="8GiB"/g' /etc/ImageMagick-6/policy.xml
sed -i -E 's/name="disk" value=".+"/name="disk" value="8GiB"/g' /etc/ImageMagick-6/policy.xml
Not sure exactly which item is causing you the problem, but it is probably one of these:
1) You need to put your font into ImageMagick's XML-based font file rather than specify a file.ttf in your convert command. To get the list of available fonts, use
identify -list font | more
Path: /Users/mark/.magick/type.xml <--- Edit your font into here
Font: ACaslonPro
family: unknown
style: Undefined
stretch: Undefined
weight: 0
glyphs: /Library/Fonts/ACaslonPro-Regular.otf
Font: ACaslonPro-Semibold
family: unknown
style: Undefined
stretch: Undefined
weight: 0
glyphs: /Library/Fonts/ACaslonPro-Semibold.otf
...
...
At the beginning you will see the path to the config file for your fonts and you need to edit that to include your TTF file you mentioned. If you have lots of fonts to add, you may like to automate the process - see my other post here.
2) You may need to escape the # in your -fill option, or at least surround it by single, or double quotes to hide it from the shell, if your MiniMagick invokes via shell - I don't know the ins and outs of MiniMagick.
3) You may need to quote the letter S that you wish to output inside single or double quotes.
4) You may need to remove the space after the colon following label.
What I am getting at is that your command should maybe be more like this:
convert -limit memory 2GiB -limit map 2GiB -limit disk 4GiB -background none -fill "#000000" -font "TimesNewRoman" -pointsize 300 label:"S" output.png

Sign a launch4j executable in ant with sign4j and jsign

I have an application in a Jar and I wrap it in a exe with launch4j so is easy for the user to launch it (in windows). I have a certificate, so I sign the jar (I don't know if this is really necessary because it will be wrapped inside the exe) and I want to sign the exe but it corrupt the executable.
I use ant to make all the process and look like:
<signjar jar="${jar.location}" alias="${key.alias}" storetype="pkcs12" keystore="${key.file}" storepass="${key.password}" tsaurl="https://timestamp.geotrust.com/tsa" />
<launch4j configFile="launch4j_configuration.xml" fileversion="${version}.0" txtfileversion="${build}" productversion="${version}.0" txtproductversion="${build}" outfile="${exe.location}" jar="${jar.location}" />
<signexe file="${exe.location}" alias="${key.alias}" storetype="pkcs12" keystore="${key.file}" storepass="${key.password}" tsaurl="http://timestamp.verisign.com/scripts/timstamp.dll" />
I have found that is because when you sign the exe it broke the jar structure or something like this. But what I have also seen is that inside the launch4j folder is a sign4j folder that contains what I think is a program that solve this problem.
My problem now is how is used this program? And how can I integrate it in the ant script to sign the exe?
The README.txt file in the folder doesn't helped to me. Sorry if this so obvious but isn't clear for me. Also note that I'm using Ubuntu.
What I have found is that you must execute the sign4j command with the signing command as its argument. Something like:
sign4j jsign -s keyfile.p12 -a "(codesign_1091_es_sw_kpsc)" --storepass AVERYGOODPASSWORD --storetype pkcs12 -n MyProgram -u https://www.example.com MyProgram.exe
So, to integrate it into ant, you need to create an exec task. For example, something like:
<exec executable="sign4j">
<arg line="java -jar jsign-1.2.jar -s ${key.file} -a ${key.alias} --storepass ${key.password} --storetype pkcs12 ${exe.location}"/>
</exec>
It works also with other signing tools like for example authenticode from Microsoft, too ...
<exec executable="launch4j/sign4j/sign4j.exe">
<arg line="signtool.exe sign /fd SHA256 /f mycert.pfx /p foobar /t http://timestamp.verisign.com/scripts/timstamp.dll dist\myapp.exe"/>
</exec>
I use ant target as below to sign exe generated out of a jar file
<target name="signexe" depends="createExe" description="Signing Exe">
<exec executable="C:\Tools\Launch4j\sign4j\sign4j.exe">
<arg line="java -jar C:\3rdParty\jsign\jsign-3.1.jar
--keystore ${keystore.location} --alias ${key.alias} --storepass ${store.password}
--name 'Application Name'
--tsaurl http://timestamp.verisign.com/scripts/timstamp.dll
AppLauncher.exe"/>
</exec>
</target>

copy and unzip files to remote machine - ant

I need to copy the zip files from local machine and paste in remote machine and unzip those files in remote machine.
I know the first part can be done using the scp (copy zip files from local and paste in remote machine) but how to do the second part using ant?
Thanks in advance
You could use the sshexec task to call the command line unzip command on the remote machine (assuming the remote machine has unzip installed).
<!-- local directory containing the files to copy -->
<property name="archives" location="C:\path\to\zipfiles" />
<property name="archives.destination" value="/home/testuser/archives" />
<property name="unzip.destination" value="/home/testuser/unpacked" />
<fileset id="zipfiles.to.copy" dir="${archives}" includes="*.zip" />
<!-- copy the archives to the remote server -->
<scp todir="${user}:${password}#host.example.com:${archives.destination}">
<fileset refid="zipfiles.to.copy" />
</scp>
<!-- Build the command line for unzip - the idea here is to turn the local
paths into the corresponding paths on the remote, i.e. to turn
C:\path\to\zipfiles\file1.zip;C:\path\to\zipfiles\file2.zip... into
/home/testuser/archives/file1.zip /home/testuser/archives/file2.zip
For this to work there must be no spaces in any of the zipfile names.
-->
<pathconvert dirsep="/" pathsep=" " property="unzip.files" refid="zipfiles.to.copy">
<map from="${archives}" to="${archives.destination}" />
</pathconvert>
<!-- execute the command. Use the "-d" option to unzip so it will work
whatever the "current" directory on the remote side -->
<sshexec host="host.example.com" username="${user}" password="${password}"
command="/bin/sh -c '
for zipfile in ${unzip.files}; do
/usr/bin/unzip -d ${unzip.destination} $$zipfile ; done '" />
The unzip command can take a number of other options, see its man page for full details. For example the -j option will ignore any directory hierarchy inside the zip files and put all the extracted files directly in the target directory. And -o will force overwrite existing files in the target directory without prompting.

Ant sshexec commandResource example

I'm having trouble figuring out what a commandResource actually looks like. The only example i've seen is listed below for Apache's page (not very helpful)
<sshexec host="somehost"
username="dude"
keyfile="${user.home}/.ssh/id_dsa"
commandResource="to_run"/>
So my peice looks like this
<sshexec host="${host}"
trust="yes"
username="${username}"
password="${password}"
commandResource="Commands.txt"/>
How do I design the format of Commands.txt? What i'd like to do is the following:
Command 1: sudo user runscript.sh
Command 2: "${password}" (password needs to be entered in the script as well)
An example can be found in the relevant bug
Commands.txt:
touch 1.txt
mv 1.txt 2.txt
mv 2.txt 3.txt
Each line has to contain a command, that has to be executed.
What you are trying to do won't work. You can't set the password that way, this would not be a command but a console input.
Maybe you should just give the user the sudo rights to execute this script.

Resources