How to use elfBinarySignature method of build Package

Best Syzkaller code snippet using build.elfBinarySignature

linux.go

Source:linux.go Github

copy

Full Screen

...30 }31 return nil32}33func (linux linux) sign(params *Params) (string, error) {34 return elfBinarySignature(filepath.Join(params.OutputDir, "obj", "vmlinux"))35}36func (linux) buildKernel(params *Params) error {37 configFile := filepath.Join(params.KernelDir, ".config")38 if err := osutil.WriteFile(configFile, params.Config); err != nil {39 return fmt.Errorf("failed to write config file: %v", err)40 }41 if err := osutil.SandboxChown(configFile); err != nil {42 return err43 }44 // One would expect olddefconfig here, but olddefconfig is not present in v3.6 and below.45 // oldconfig is the same as olddefconfig if stdin is not set.46 // Note: passing in compiler is important since 4.17 (at the very least it's noted in the config).47 if err := runMake(params.KernelDir, "oldconfig", "CC="+params.Compiler); err != nil {48 return err49 }50 // Write updated kernel config early, so that it's captured on build failures.51 outputConfig := filepath.Join(params.OutputDir, "kernel.config")52 if err := osutil.CopyFile(configFile, outputConfig); err != nil {53 return err54 }55 // We build only zImage/bzImage as we currently don't use modules.56 var target string57 switch params.TargetArch {58 case "386", "amd64":59 target = "bzImage"60 case "ppc64le":61 target = "zImage"62 }63 if err := runMake(params.KernelDir, target, "CC="+params.Compiler); err != nil {64 return err65 }66 vmlinux := filepath.Join(params.KernelDir, "vmlinux")67 outputVmlinux := filepath.Join(params.OutputDir, "obj", "vmlinux")68 if err := osutil.Rename(vmlinux, outputVmlinux); err != nil {69 return fmt.Errorf("failed to rename vmlinux: %v", err)70 }71 return nil72}73func (linux) createImage(params *Params) error {74 tempDir, err := ioutil.TempDir("", "syz-build")75 if err != nil {76 return err77 }78 defer os.RemoveAll(tempDir)79 scriptFile := filepath.Join(tempDir, "create.sh")80 if err := osutil.WriteExecFile(scriptFile, []byte(createImageScript)); err != nil {81 return fmt.Errorf("failed to write script file: %v", err)82 }83 var kernelImage string84 switch params.TargetArch {85 case "386", "amd64":86 kernelImage = "arch/x86/boot/bzImage"87 case "ppc64le":88 kernelImage = "arch/powerpc/boot/zImage.pseries"89 }90 kernelImagePath := filepath.Join(params.KernelDir, filepath.FromSlash(kernelImage))91 cmd := osutil.Command(scriptFile, params.UserspaceDir, kernelImagePath, params.TargetArch)92 cmd.Dir = tempDir93 cmd.Env = append([]string{}, os.Environ()...)94 cmd.Env = append(cmd.Env,95 "SYZ_VM_TYPE="+params.VMType,96 "SYZ_CMDLINE_FILE="+osutil.Abs(params.CmdlineFile),97 "SYZ_SYSCTL_FILE="+osutil.Abs(params.SysctlFile),98 )99 if _, err = osutil.Run(time.Hour, cmd); err != nil {100 return fmt.Errorf("image build failed: %v", err)101 }102 // Note: we use CopyFile instead of Rename because src and dst can be on different filesystems.103 imageFile := filepath.Join(params.OutputDir, "image")104 if err := osutil.CopyFile(filepath.Join(tempDir, "disk.raw"), imageFile); err != nil {105 return err106 }107 keyFile := filepath.Join(params.OutputDir, "key")108 if err := osutil.CopyFile(filepath.Join(tempDir, "key"), keyFile); err != nil {109 return err110 }111 if err := os.Chmod(keyFile, 0600); err != nil {112 return err113 }114 return nil115}116func (linux) clean(kernelDir, targetArch string) error {117 return runMake(kernelDir, "distclean")118}119func runMake(kernelDir string, args ...string) error {120 args = append(args, fmt.Sprintf("-j%v", runtime.NumCPU()))121 cmd := osutil.Command("make", args...)122 if err := osutil.Sandbox(cmd, true, true); err != nil {123 return err124 }125 cmd.Dir = kernelDir126 cmd.Env = append([]string{}, os.Environ()...)127 // This makes the build [more] deterministic:128 // 2 builds from the same sources should result in the same vmlinux binary.129 // Build on a release commit and on the previous one should result in the same vmlinux too.130 // We use it for detecting no-op changes during bisection.131 cmd.Env = append(cmd.Env,132 "KBUILD_BUILD_VERSION=0",133 "KBUILD_BUILD_TIMESTAMP=now",134 "KBUILD_BUILD_USER=syzkaller",135 "KBUILD_BUILD_HOST=syzkaller",136 "KERNELVERSION=syzkaller",137 "LOCALVERSION=-syzkaller",138 )139 _, err := osutil.Run(time.Hour, cmd)140 return err141}142// elfBinarySignature calculates signature of an elf binary aiming at runtime behavior143// (text/data, debug info is ignored).144func elfBinarySignature(bin string) (string, error) {145 f, err := os.Open(bin)146 if err != nil {147 return "", fmt.Errorf("failed to open binary for signature: %v", err)148 }149 ef, err := elf.NewFile(f)150 if err != nil {151 return "", fmt.Errorf("failed to open elf binary: %v", err)152 }153 hasher := sha256.New()154 for _, sec := range ef.Sections {155 // Hash allocated sections (e.g. no debug info as it's not allocated)156 // with file data (e.g. no bss). We also ignore .notes section as it157 // contains some small changing binary blob that seems irrelevant.158 // It's unclear if it's better to check NOTE type,...

Full Screen

Full Screen

linux_test.go

Source:linux_test.go Github

copy

Full Screen

...27 }28 if sign1 == sign3 {29 t.Errorf("signature has not changed after a change")30 }31 //func elfBinarySignature(bin string) (string, error) {32}33func sign(t *testing.T, flags []string, changed, comment bool) string {34 buf := new(bytes.Buffer)35 if err := srcTemplate.Execute(buf, SrcParams{Changed: changed, Comment: comment}); err != nil {36 t.Fatal(err)37 }38 src := buf.Bytes()39 bin, err := osutil.TempFile("syz-build-test")40 if err != nil {41 t.Fatal(err)42 }43 defer os.Remove(bin)44 cmd := osutil.Command("gcc", append(flags, "-pthread", "-o", bin, "-x", "c", "-")...)45 cmd.Stdin = buf46 out, err := cmd.CombinedOutput()47 if err != nil {48 t.Fatalf("compiler failed: %v\n%s\n\n%s", err, src, out)49 }50 sign, err := elfBinarySignature(bin)51 if err != nil {52 t.Fatal(err)53 }54 return sign55}56type SrcParams struct {57 Changed bool58 Comment bool59}60var srcTemplate = template.Must(template.New("").Parse(`61#include <stdio.h>62#include <pthread.h>63int main() {64 int x = {{if .Changed}}0{{else}}1{{end}};...

Full Screen

Full Screen

elfBinarySignature

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 cmd := exec.Command("go", "build", "-o", "2.exe", "2.go")4 if err := cmd.Run(); err != nil {5 fmt.Println(err)6 os.Exit(1)7 }8 if err := os.Chmod("2.exe", 0777); err != nil {9 fmt.Println(err)10 os.Exit(1)11 }12 file, err := os.Open("2.exe")13 if err != nil {14 fmt.Println(err)15 os.Exit(1)16 }17 defer file.Close()18 fInfo, err := file.Stat()19 if err != nil {20 fmt.Println(err)21 os.Exit(1)22 }23 buf := make([]byte, fInfo.Size())24 if _, err := file.Read(buf); err != nil {25 fmt.Println(err)26 os.Exit(1)27 }28 if string(buf[:4]) != "MZ" {29 fmt.Println("Not a PE file")30 os.Exit(1)31 }32 fmt.Println("PE file signature found")33 os.Exit(0)34}

Full Screen

Full Screen

elfBinarySignature

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fmt.Println(b.ElfBinarySignature())4}5ELF 64-bit LSB executable, x86-64, version 1 (SYSV), statically linked, not stripped6import (7func main() {8 fmt.Println(b.ElfBinarySignature())9}10ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), statically linked, not stripped11import (12func main() {13 fmt.Println(b.ElfBinarySignature())14}15ELF 64-bit LSB executable, x86-64, version 1 (SYSV), statically linked, not stripped16import (17func main() {18 fmt.Println(b.ElfBinarySignature())19}20ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), statically linked, not stripped21import (22func main() {23 fmt.Println(b.ElfBinarySignature())24}25ELF 32-bit LSB executable, ARM, EABI5 version 1 (SYSV), statically linked, not stripped

Full Screen

Full Screen

elfBinarySignature

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fmt.Println(build.Default.ElfBinarySignature)4}5ELF 64-bit LSB executable, x86-64, version 1 (SYSV), statically linked, not stripped6import (7func main() {8 fmt.Println(build.Default.BuildTags)9}10import (11func main() {12 fmt.Println(build.Default.CgoEnabled)13}14import (15func main() {16 fmt.Println(build.Default.Compiler)17}18import (19func main() {20 fmt.Println(build.Default.GOARCH)21}

Full Screen

Full Screen

elfBinarySignature

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 cwd, err := os.Getwd()4 if err != nil {5 fmt.Println(err)6 os.Exit(1)7 }8 path := filepath.Join(cwd, "2.go")9 build := exec.Command("go", "build", path)10 out, err := build.Output()11 if err != nil {12 fmt.Println(err)13 os.Exit(1)14 }15 output := string(out)16 index := strings.Index(output, "hash")

Full Screen

Full Screen

elfBinarySignature

Using AI Code Generation

copy

Full Screen

1import java.io.File;2import java.io.IOException;3import java.util.ArrayList;4import java.util.List;5import org.apache.commons.io.FileUtils;6import org.apache.commons.io.FilenameUtils;7import com.google.common.collect.ImmutableList;8public class elfBinarySignature {9 public static void main(String[] args) throws IOException {10 List<String> files = new ArrayList<String>();11 File directory = new File("/home/rajesh/Downloads/test");12 String[] extensions = new String[] { "elf" };13 files = (List<String>) FileUtils.listFiles(directory, extensions, true);14 for (String file : files) {15 System.out.println("File is " + file);16 String filename = FilenameUtils.getBaseName(file);17 String command = "objdump -x " + file + " | grep -i elf";18 System.out.println("Command is " + command);19 String output = executeCommand(command);20 System.out.println("Output is " + output);21 String[] parts = output.split(":");22 String part2 = parts[1];23 String[] parts2 = part2.split(" ");24 String part3 = parts2[1];25 System.out.println("Part3 is " + part3);26 String[] parts3 = part3.split(" ");27 String part4 = parts3[0];28 System.out.println("Part4 is " + part4);29 String[] parts4 = part4.split(" ");30 String part5 = parts4[0];31 System.out.println("Part5 is " + part5);32 String command2 = "objdump -x " + file + " | grep -i machine";33 System.out.println("Command2 is " + command2);34 String output2 = executeCommand(command2);35 System.out.println("Output2 is " + output2);36 String[] parts5 = output2.split(":");37 String part6 = parts5[1];38 System.out.println("Part6 is " + part6);39 String[] parts6 = part6.split(" ");40 String part7 = parts6[1];41 System.out.println("Part7 is " + part7);42 String[] parts7 = part7.split(" ");43 String part8 = parts7[0];44 System.out.println("Part8 is " + part8);45 String[] parts8 = part8.split(" ");46 String part9 = parts8[0];

Full Screen

Full Screen

elfBinarySignature

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 elfSignature := build.elfBinarySignature()4 for _, arg := range os.Args[1:] {5 file, err := os.Open(arg)6 if err != nil {7 fmt.Fprintf(os.Stderr, "%s: error: %s8", filepath.Base(os.Args[0]), err)9 }10 defer file.Close()11 buf := make([]byte, len(elfSignature))12 _, err = file.Read(buf)13 if err != nil {14 fmt.Fprintf(os.Stderr, "%s: error: %s15", filepath.Base(os.Args[0]), err)16 }17 if strings.HasPrefix(string(buf), elfSignature) {18 fmt.Printf("%s: ELF binary19 } else {20 fmt.Printf("%s: not an ELF binary21 }22 }23}

Full Screen

Full Screen

elfBinarySignature

Using AI Code Generation

copy

Full Screen

1import "fmt"2import "os"3import "os/exec"4import "log"5func main() {6 path, err := os.Executable()7 if err != nil {8 log.Fatal(err)9 }10 cmd := exec.Command(path, "-version")11 out, err := cmd.Output()12 if err != nil {13 log.Fatal(err)14 }15 fmt.Println(string(out))16}17import (18func main() {19 path, err := os.Executable()20 if err != nil {21 log.Fatal(err)22 }23 cmd := exec.Command(path, "-version")24 out, err := cmd.Output()25 if err != nil {26 log.Fatal(err)27 }28 fmt.Println(string(out))29}30import (31func main() {32 path, err := os.Executable()33 if err != nil {34 log.Fatal(err)35 }36 cmd := exec.Command(path, "-version")37 out, err := cmd.Output()38 if err != nil {39 log.Fatal(err)40 }41 fmt.Println(string(out))42}43import (44func main() {45 path, err := os.Executable()

Full Screen

Full Screen

elfBinarySignature

Using AI Code Generation

copy

Full Screen

1import java.io.*;2import java.util.*;3import java.lang.*;4import java.util.Scanner;5import java.util.ArrayList;6import java.util.List;7import java.util.Arrays;8import java.util.stream.Collectors;9import java.util.stream.Stream;10import java.util.stream.IntStream;11import java.util.stream.*;12import java.util.function.*;13import java.util.*;14public class elfBinarySignature {15 public static void main(String[] args) {16 Scanner sc = new Scanner(System.in);17 String s = sc.nextLine();18 String[] s1 = s.split(" ");19 int[] arr = new int[s1.length];20 for (int i = 0; i < s1.length; i++) {21 arr[i] = Integer.parseInt(s1[i]);22 }23 int[] arr1 = new int[s1.length];24 arr1 = arr;25 int[] arr2 = new int[s1.length];26 for (int i = 0; i < s1.length; i++) {27 arr2[i] = arr1[i];28 }29 int[] arr3 = new int[s1.length];30 for (int i = 0; i < s1.length; i++) {31 arr3[i] = arr1[i];32 }33 int[] arr4 = new int[s1.length];34 for (int i = 0; i < s1.length; i++) {35 arr4[i] = arr1[i];36 }37 int[] arr5 = new int[s1.length];38 for (int i = 0; i < s1.length; i++) {39 arr5[i] = arr1[i];40 }41 int[] arr6 = new int[s1.length];42 for (int i = 0; i < s1.length; i++) {43 arr6[i] = arr1[i];44 }45 int[] arr7 = new int[s1.length];46 for (int i = 0; i < s1.length; i++) {47 arr7[i] = arr1[i];48 }49 int[] arr8 = new int[s1.length];50 for (int i = 0; i < s1.length; i++) {51 arr8[i] = arr1[i];52 }53 int[] arr9 = new int[s1.length];54 for (int i = 0; i < s1.length; i++) {

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful