It's possible to loop mount a binary file that contains a filesystem image. I'd like to put that binary file into a C static variable, and then mount that. Is this possible? If so, what C API magic do I need?
There are several steps we'll want to take
Create a file system image
Embed this image into the binary
Mount the embedded image as a read-only file system.
It sounds like you already know how to perform stems 1 and 2, but not how to do step 3.
I prepared ab.sqfs image and a.out which contains that image at offset 0x3010. Here are the commands to mount this filesystem:
# optional, look at the bytes of the filesystem from step 1
xxd -l 16 -g1 ab.sqfs
00000000: 68 73 71 73 07 00 00 00 6c 61 ce 60 00 00 02 00 hsqs....la.`....
# optional: confirm that we have the correct file offset to the start of FS image
xxd -l 16 -g1 -s 0x3010 a.out
00003010: 68 73 71 73 07 00 00 00 6c 61 ce 60 00 00 02 00 hsqs....la.`....
# create a loop device which "points" into the file:
sudo losetup -r -o 0x3010 loop0 a.out
losetup: a.out: Warning: file does not fit into a 512-byte sector; the end of the file will be ignored.
# optional: confirm that (just created) /dev/loop0 contains expected bytes
sudo xxd -l 16 -g1 /dev/loop0
00000000: 68 73 71 73 07 00 00 00 6c 61 ce 60 00 00 02 00 hsqs....la.`....
# create directory on which the FS will be mounted
mkdir /tmp/mnt
# finally mount the FS:
sudo mount -oro /dev/loop0 /tmp/mnt
# optional: verify contents of /tmp/mnt
ls -lR /tmp/mnt
... has exactly the files I've put into it.
what C API magic do I need?
You can run the losetup and mount commands under strace to observe what they do. The key steps for losetup are:
openat(AT_FDCWD, "/tmp/a.out", O_RDONLY|O_CLOEXEC) = 3
openat(AT_FDCWD, "/dev/loop0", O_RDONLY|O_CLOEXEC) = 4
ioctl(4, LOOP_SET_FD, 3) = 0
ioctl(4, LOOP_SET_STATUS64, {lo_offset=0x3010, lo_number=0, lo_flags=LO_FLAGS_READ_ONLY, lo_file_name="/tmp/a.out", ...}) = 0
And for mount:
mount("/dev/loop0", "/tmp/mnt", "squashfs", MS_RDONLY, NULL) = 0
These calls can be performed by the application itself, or by "shelling out" to external losetup and mount commands.
Related
I want to convert a BPF assembly into executable.
For example, I got
entrypoint:
div32 r1, 1768515945
exit
Can I get its executable? It should be loaded and executed by the bpf vm.
Thanks.
Each instruction is 64 bits. This should assemble to:
00: 69 69 69 69 00 00 01 34
08: 00 00 00 00 00 00 00 90
The first instruction is from BPF_DIV | BPF_K | BPF_ALU | (1 << 8) | (1768515945 << 32). The second is just BPF_EXIT. For more information, see the kernel documentation. Note that exit expects r0 to contain a return code, but you haven't explicitly set any. It should default to 0.
I'm experimenting. I created a .txt file with the word hi and run gzip test.txt to compress it.
This gives me a file test.txt.gz with the following bytes:
1F 8B 08 08 E6 E8 3F 60 00 03 62 2E 74 78 74 00
CB C8 04 00 AC 2A 93 D8 02 00 00 00
With the software 101 editor, I found out that the first line is the header.
CB C8 04 00 are the compressed data
AC 2A 93 D8 is "CRC of the data section"
02 00 00 00 is the "size of the uncompressed input"
What I'm trying to do (I don't know if it is even possible): I want to have my own characters as "compressed" data but want the .gz file to be still valid.
I tried replacing CB C8 04 00 with 62 62 62 62 (letter 'b' 4 times) but the file is invalid then. Then I tried to replace AC 2A 93 D8, too with the CRC32 value of "bbbb", but the file is still invalid. I can't decompress it. Running gzip -d test.txt.gz returns "unexpected end of file".
Is it possible what I'm trying to do? If yes: what am I doing wrong?
CB C8 04 00 is a valid deflate stream. 62 62 62 62 is not. A gzip member is a gzip header, a valid deflate stream, and a gzip trailer.
Deflate streams are defined in RFC 1951.
I have a simple uint8_t* IP packet buffer like
45 0 0 34 0 0 40 0 40 6 6B 53 C0 A8 FF 6 AC D9 1C EE 0 4D 0 50 0 0 0 0 0 0 0 0 80 2 FD E8 A5 20 0 0 2 4 5 B4 3 3 0 4 2 0 0 0
I want to use Wireshark to view it. I saw that I can import an hex dump on Wireshark, but how can I save this buffer as a hex dump for wireshark to open?
Is it possible to concatenate lots of IP packets together?
If you can modify the data to match the format expected by text2pcap, you can use that tool to convert the data into a pcap (or pcapng) file. For example:
Here's the data you provided in a format acceptable to text2pcap:
0000 45 00 00 34 00 00 40 00 40 06 6B 53 C0 A8 FF 06
0010 AC D9 1C EE 00 4D 00 50 00 00 00 00 00 00 00 00
0020 80 02 FD E8 A5 20 00 00 02 04 05 B4 03 03 00 04
0030 02 00 00 00
0034
Since this appears to start with an IPv4 header, you can generate a pcap file with a link layer header type set to LINKTYPE_RAW, the value of which is obtained from https://www.tcpdump.org/linktypes.html, as referenced in the text2pcap man page. Alternatively, you can choose to add a dummy Ethernet header to the data, in which case you can omit the link layer header type option as LINKTYPE_ETHERNET is the default value; however you do need to add the option to add the dummy Ethernet header. Here I demonstrate both methods:
Method 1: Raw IP
text2pcap -l 101 file.hex file.pcap
Method 2: Add dummy Ethernet header
text2pcap -e 0x0800 file.hex file.pcap
The text2pcap tool is capable of processing any arbitrary number of packets from a file, but note the required format from the man page, i.e. "Note the last byte must either be followed by the expected next offset value as in the example above or a space or a line-end character(s)."
By the way, Wireshark itself is also capable of converting the hex data into a pcap file as well using the File -> Import from Hex Dump... feature, although Wireshark will always import the hex data as a pcapng file. You can choose to save the file as pcap though, but there'll always be that intermediate pcapng file generated. Wireshark should just provide a checkbox to allow the user to select which format to use, just like text2pcap does. I have filed Wireshark Bug 16724 to address this.
ATM is Wincor/Nixdorf model ProCash 1500xe USB.
i use CDM320.exe tool to test the dispense of the ATM. when i run Reset command under ExecuteAdmin menu i get the following output:
[10:23:54] WFSAsyncExecute ( WFS_CMD_CDM_RESET (321) ) returned WFS_SUCCESS (0) [ReqID: 87]
[10:24:04] Event WFS_SYSE_HARDWARE_ERROR (2) arrived
RequestID: 0
hService: 2
tsTimestamp: [MON 14.04.2014 06:54:04,156]
szLogicalName: CDM30
szPhysicalName: CDM30
szWorkstationName: ATM
szAppID: CDM320
dwAction: WFS_ERR_ACT_NOACTION (0x0000)
dwSize: 33
bDescription
StClass: 0x0000001D
StCode: 0x20001814
StWarn: 0x00000000
sDescription: CscCngReset; SCOD=14
szDescription HEX: 1D 00 00 00 14 18 00 20 00 00 00 00 43 73 63 43 6E 67 52 65 73 65 74 3B 20 53 43 4F 44 3D 31 34 00
[10:24:04] WFSAsyncExecute ( WFS_CMD_CDM_RESET (321) ) completed with WFS_ERR_HARDWARE_ERROR (-14) [ReqID: 87]
what's the problem and how can i resolve it?
This is probably a configuration issue or a genuine HW error like foreign object blocking sensor or a faulty sensor. Do you have a Wincor diagnostic software and if you do what it tells about the CDM?
PS. As I do not have experience from Wincor HW, this might be totally off the mark.
I am currentlly on a project that requires me to load a JavaCard application Applet ( a .cap ) file to a JavaCard. Our framework is based on Visual C++ and PCSC, so we need to load the same applet to a series of JavaCards. Does anyone know how this can be processed? I mean, where should I start. Thanks!
You are correct that this is not a trivial job.
There are differences between different javacards, but generally you need to do 4 things:
initialize secure communications with the card (because many javacards are "global platform" cards they require a secure channel)
send a command saying "i wanna install an applet"
send the binary data for the applet to be installed
send a command to "instantiate" the applet after the binary data is sent
I'd recommend using the eclipse plugin to install the applet initially, because you can see the APDUs generated by the plugin to do the steps above. Once you know the APDU commands you must send to install your applet, you can directly send these commands using the PCSC interface from your C++ code to automate installation on a large number of cards.
My company makes a web browser plugin called Card Boss for doing this kind of thing (card communications via pcsc) from a browser - there's a web page you can use where you can type your own APDUs and send them to the card at the follwing URL:
https://cardboss.cometway.com/content.agent?page_name=Card+Boss+Lab
If you use our tool, your applet installation script should look something like this (note that this is a script for a JCOP card using the default jcop keys)
MESSAGE BOX Installing applets...
INIT CHANNEL 40 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f, 40 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f
// INSTALL CAP:
SEND 80 E6 02 00 1D 10 A0 00 00 00 09 00 03 FF FF FF FF 89 10 71 00 01 08 A0 00 00 00 03 00 00 00 00 00 00
// LOADING CAP:
SEND 80 E8 00 00 FA C4 82 01 03 01 00 25 DE CA FF (snip, I removed a bunch
of binary data representing the cap file to shorten this post, and you might
need multiple SEND commands because of limits on the size of APDUS)
// INSTANTIATING Applet
SEND 80 E6 0C 00 1E 05 63 6F 6D 65 74 07 63 6F 6D 65 74 00 01 05 00 00 00 00 00 01 00 06 C9 04 68 2C 00 03 00 00